ALTER PROCEDURE [dbo].[StatisticTelNumsByUsers]
-- Add the parameters for the stored procedure here
@tbName varchar(50), --订单信息表
@beginTime datetime, --开始时间
@endTime datetime --统计结束时间
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
delete from tab_UserStatisticsTels
declare @strSql varchar(200)
set @strSql=''
set @strSql='select userId,count(*) as telNums into #temp1 from '+@tbName+
' where OrderDateTime>='+
''''+dbo.GetLongStrDateTime(@beginTime,0)+''''+' and OrderDateTime<='+
''''+dbo.GetLongStrDateTime(@endTime,0)+''''+' group by all userid';
print(@strSql);
--删除员工电召统计表内容
exec(@strSql)
set @strSql=''
set @strSql='select userId,count(*) as HaveCarsNums into #temp2 from '+@tbName+
' where OrderDateTime>='+
''''+dbo.GetLongStrDateTime(@beginTime,0)+''''+' and OrderDateTime<='+
''''+dbo.GetLongStrDateTime(@endTime,0)+''''+' and state=1 group by all userid';
print(@strSql)
exec(@strSql)
select * from #temp1
select * from #temp2
select t1.userId,t1.telNums,t2.HaveCarsNums into #temp from #temp1 t1,#temp2 t2
where t1.userId=t2.userId
insert into tab_UserStatisticsTels(userid,telNums,HaveCarsNums) select * from #temp
drop table #temp1
drop table #temp2
drop table #temp
END
提示 temp1,temp2对象不存在
-- Add the parameters for the stored procedure here
@tbName varchar(50), --订单信息表
@beginTime datetime, --开始时间
@endTime datetime --统计结束时间
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON
delete from tab_UserStatisticsTels
declare @strSql varchar(200)
set @strSql=''
set @strSql='select userId,count(*) as telNums into #temp1 from '+@tbName+
' where OrderDateTime>='+
''''+dbo.GetLongStrDateTime(@beginTime,0)+''''+' and OrderDateTime<='+
''''+dbo.GetLongStrDateTime(@endTime,0)+''''+' group by all userid';
print(@strSql);
--删除员工电召统计表内容
exec(@strSql)
set @strSql=''
set @strSql='select userId,count(*) as HaveCarsNums into #temp2 from '+@tbName+
' where OrderDateTime>='+
''''+dbo.GetLongStrDateTime(@beginTime,0)+''''+' and OrderDateTime<='+
''''+dbo.GetLongStrDateTime(@endTime,0)+''''+' and state=1 group by all userid';
print(@strSql)
exec(@strSql)
select * from #temp1
select * from #temp2
select t1.userId,t1.telNums,t2.HaveCarsNums into #temp from #temp1 t1,#temp2 t2
where t1.userId=t2.userId
insert into tab_UserStatisticsTels(userid,telNums,HaveCarsNums) select * from #temp
drop table #temp1
drop table #temp2
drop table #temp
END
提示 temp1,temp2对象不存在
解决方案 »
- 求助一个财务成本合算(不用 Function)
- 有关sql server 2005用openrowset功能向数据库中存入图片的问题
- 急!!需SQL server 2005的下载地址
- 送分,顺便问个小问题。
- 请(无枪狙击手)(老龟) 求教一个问题,谢谢。
- 在WIN2000个人版能安装MS SQL 6.5吗?
- SQL语句支持的最大长度为?
- 做过数据库辅助设计系统的达人请进!!!
- sql sever 2000 不支持事务处理是怎么回事?
- ORACLE的DATE类型既包括日期又包括时间吗?ORACLE还有其它时间类型吗?在DELPHI中怎样把当前日期时间存到DATE类型字段中?
- 求救!烦人的查找数据问题!
- 大侠给看看用sql取当前年的前后10年的一个查询,急!
SELECT userId, COUNT(*) AS telNums,
SUM(CASE state WHEN 1 THEN 1 END) AS HaveCarsNums
FROM @tbName
WHERE OrderDateTime BETWEEN @beginTime AND @endTime
GROUP BY userId
select * from #temp1
select * from #temp2
select t1.userId,t1.telNums,t2.HaveCarsNums into #temp from #temp1 t1,#temp2 t2
where t1.userId=t2.userId
而上面这些语句,说明#temp1,#temp2在数据中是存在的
实际上他是不存在的
解决办法:把这些语句全放到@strSql中,只用一个exec(@strSql)
第一次exec(@strSql)当这个语句结束时,#temp1的生命期就结束了,所以找不到#temp1,同样,第二次批执行后找不到#temp2,要想找到他们最好全部用动态执行
declare @sql varchar(1000)set @sql='INSERT INTO #t VALUES (2)'
exec(@sql)
SELECT Test2Col = x FROM #tGO