存储过程中随机临时表名问题
CREATE procedure dep_huif
(@dep nvarchar(200),
@Mendian nvarchar(200),
@startdate nvarchar(200),
@enddate nvarchar(200),
@all varchar(20),
@slist varchar(200))
AS
exec('select ts_name,ts_id into ##tmp_huifsubtype from ts_selections a where a.ts_id in ('+@slist+')')
if @all=1
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into ##tmp_huif from usr_incidents where (TS_workflow_type =5311) and ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=2
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into ##tmp_huif from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=3
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into ##tmp_huif from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and ts_mendian='+@Mendian+' and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
declare @s varchar(8000)
set @s=''
select
@s=@s+',"'+b.ts_name+'|完成" =sum(case when ts_hfsubtype='+rtrim(b.ts_ID)+' and ts_activeinactive=1 then 1 else 0 end)'
+',"'+b.ts_name+'|未完成"=sum(case when ts_hfsubtype='+rtrim(b.ts_ID)+' and ts_activeinactive=0 then 1 else 0 end)'
from ##tmp_huifsubtype b
set @s='select '+stuff(@s,1,1,'')+' from ##tmp_huif '
print @s
exec(@s)
drop table ##tmp_huifsubtype,##tmp_huif
GO
我想将以上存储过程中的临时表名,换成随机临时表名,但是SQL字符串凑不出来,请高手指点
='tmp_huifsubtype'+cast(@i as nvarchar(200))
set @tablename1='tmp_huif'+cast(@i as nvarchar(200))
set @droptable='drop table '+@tablename
set @droptable1='drop table '+@tablename1
set @sqlstr='select ts_name,ts_id into '+@tablename+' from ts_selections a where a.ts_id in ('+@slist+')'
exec(@sqlstr)
--exec('select ts_name,ts_id into ##tmp_huifsubtype from ts_selections a where a.ts_id in ('+@slist+')')
if @all=1
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into '+@tablename1+' from usr_incidents where (TS_workflow_type =5311) and ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=2
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into '+@tablename1+' from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=3
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into '+@tablename1+' from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and ts_mendian='+@Mendian+' and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
declare @aa varchar(8000),@sql_str varchar(8000)
--set @s=''
set @aa='declare @s varchar(8000),@sql_str varchar(8000) '+'set @s='''''+
' set @sql_str=''select''+
@s+''=''+@s+''''+'''',"''''+b.ts_name+''''|完成" =sum(case when ts_hfsubtype=''''+rtrim(b.ts_ID)+'''' and ts_activeinactive=1 then 1 else 0 end)''''
+'''',"''''+b.ts_name+''''|未完成"=sum(case when ts_hfsubtype=''''+rtrim(b.ts_ID)+'''' and ts_activeinactive=0 then 1 else 0 end)''''
from '''+@tablename+''' b '
declare @exes varchar(200)
set @exes=' exec(@s)'
set @aa=@aa+'exec(@sql_str) '+'set @s=''select ''+stuff(@s,1,1,'''')+'' from ''+@tablename1'+' exec(@s)'
print @aa
exec(@aa)
exec(@droptable)
exec(@droptable1)
GO
(@dep nvarchar(200),
@Mendian nvarchar(200),
@startdate nvarchar(200),
@enddate nvarchar(200),
@all varchar(20),
@slist varchar(200))
AS
exec('select ts_name,ts_id into ##tmp_huifsubtype from ts_selections a where a.ts_id in ('+@slist+')')
if @all=1
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into ##tmp_huif from usr_incidents where (TS_workflow_type =5311) and ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=2
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into ##tmp_huif from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=3
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into ##tmp_huif from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and ts_mendian='+@Mendian+' and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
declare @s varchar(8000)
set @s=''
select
@s=@s+',"'+b.ts_name+'|完成" =sum(case when ts_hfsubtype='+rtrim(b.ts_ID)+' and ts_activeinactive=1 then 1 else 0 end)'
+',"'+b.ts_name+'|未完成"=sum(case when ts_hfsubtype='+rtrim(b.ts_ID)+' and ts_activeinactive=0 then 1 else 0 end)'
from ##tmp_huifsubtype b
set @s='select '+stuff(@s,1,1,'')+' from ##tmp_huif '
print @s
exec(@s)
drop table ##tmp_huifsubtype,##tmp_huif
GO
我想将以上存储过程中的临时表名,换成随机临时表名,但是SQL字符串凑不出来,请高手指点
='tmp_huifsubtype'+cast(@i as nvarchar(200))
set @tablename1='tmp_huif'+cast(@i as nvarchar(200))
set @droptable='drop table '+@tablename
set @droptable1='drop table '+@tablename1
set @sqlstr='select ts_name,ts_id into '+@tablename+' from ts_selections a where a.ts_id in ('+@slist+')'
exec(@sqlstr)
--exec('select ts_name,ts_id into ##tmp_huifsubtype from ts_selections a where a.ts_id in ('+@slist+')')
if @all=1
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into '+@tablename1+' from usr_incidents where (TS_workflow_type =5311) and ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=2
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into '+@tablename1+' from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
if @all=3
begin
exec('select ts_hftype,ts_hfsubtype,ts_activeinactive into '+@tablename1+' from usr_incidents where (TS_SUBDEP ='+@dep+') AND ts_workflow_type=5311 and ts_mendian='+@Mendian+' and
ts_submitdate between '''+@StartDate+''' and '''+@EndDate+'''')
end
declare @aa varchar(8000),@sql_str varchar(8000)
--set @s=''
set @aa='declare @s varchar(8000),@sql_str varchar(8000) '+'set @s='''''+
' set @sql_str=''select''+
@s+''=''+@s+''''+'''',"''''+b.ts_name+''''|完成" =sum(case when ts_hfsubtype=''''+rtrim(b.ts_ID)+'''' and ts_activeinactive=1 then 1 else 0 end)''''
+'''',"''''+b.ts_name+''''|未完成"=sum(case when ts_hfsubtype=''''+rtrim(b.ts_ID)+'''' and ts_activeinactive=0 then 1 else 0 end)''''
from '''+@tablename+''' b '
declare @exes varchar(200)
set @exes=' exec(@s)'
set @aa=@aa+'exec(@sql_str) '+'set @s=''select ''+stuff(@s,1,1,'''')+'' from ''+@tablename1'+' exec(@s)'
print @aa
exec(@aa)
exec(@droptable)
exec(@droptable1)
GO
...全文
相关推荐
Oracle存储过程中使用临时表
Oracle存储过程中使用临时表 会话级临时表 事务级临时表
存储过程中的临时表使用方法
创建临时表。临时表与永久表相似,但临时表存储在 tempdb 中,当不再使用时会自动删除。
Mysql 存储过程(输入,返回),函数,临时表
Mysql 存储过程,函数,临时表 存储过程包括输入返回,可供项目中遇到难题的人解决一下,我曾为此费了好大精力,最后总结出来共大家分享
mysql临时表用法分析【查询结果可存在临时表中】
主要介绍了mysql临时表用法,结合实例形式分析了MySQL将查询结果存储在临时表中的相关操作技巧,需要的朋友可以参考下
mysql复杂存储过程实例(游标、临时表、循环、递归)
本资源结合实例实现一个复杂的存储过程,存储过程中有用到游标、临时表、循环、递归等知识,sql文件附有实例数据表创建的sql语句。


疑难问题
