高分请教,关于临时表头痛的问题,急,请高手帮我看看.

postfxj 2009-02-11 04:44:50
部分代码如下:

declare @dc char(2),@createtable varchar(8000),@insertsql varchar(8000),
@updatesql varchar(8000), @select varchar(1000),@bmmc varchar(100),@startrq char(10),@endrq char(10),
@xt char(7),@i integer,@yscz varchar(100),@sl decimal(18,0),@num varchar(2),@tmpxt char(7)

--{一、***********************************動態創建全局臨時表*******************************************
set ansi_warnings off
set nocount on

if object_id('tempdb..##temp_cpgzdc') is not null drop table ##temp_cpgzdc
if object_id('tempdb..##temp') is not null drop table ##temp
set @createtable='CREATE TABLE ##temp_cpgzdc (
[xt] [char] (7) NOT NULL ,
[pp_mc] [varchar] (50) NOT NULL ,
[xlb_mc] [varchar] (50) NOT NULL,
[xt_dj] [decimal](18, 2) default 0 ,
[zjhl] [decimal](18, 0) default 0 ,
[zxl] [decimal](18, 0) default 0 ,
[zjg] [decimal](18, 2) default 0 ,
[weekxl] [decimal](18, 0) NULL ,
[weekjg] [decimal](18, 2) default 0 '

declare cur_dc cursor for
select b_dm from bsc where b_dm>'01'
open cur_dc
fetch next from cur_dc into @dc
while @@fetch_status=0
begin
set @createtable=@createtable+ ',dr'+@dc+' decimal(10, 0) NULL ,dc'+@dc+' decimal(10, 0) NULL ,'
+'zxl'+@dc+' decimal(10, 0) NULL ,zjg'+@dc+' decimal(10, 0) NULL ,'
+'weekxl'+@dc+' decimal(10, 0) NULL ,weekjg'+@dc+' decimal(18, 2) NULL ,'
+'kc'+@dc+' decimal(10, 0) default 0 ,zt'+@dc+' decimal(10, 0) NULL,'
+'yhrq'+@dc+' datetime NULL ,sgrq'+@dc+' datetime NULL,'
+'ysczxl1'+@dc+' decimal(10, 0) default 0,ysczxl2'+@dc+' decimal(10, 0) default 0,'
+'ysczxl3'+ @dc+' decimal(10, 0) default 0,ysczxl4'+@dc+' decimal(10, 0) default 0,'
+'ysczxl5'+ @dc+' decimal(10, 0) default 0,ysczxl6'+@dc+' decimal(10, 0) default 0,'
+'ysczxl7'+ @dc+' decimal(10, 0) default 0,ysczxl8'+@dc+' decimal(10, 0) default 0,'
+'yscz1'+@dc+' varchar(60) NULL,yscz2'+@dc+' varchar(60) NULL,'
+'yscz3'+ @dc+' varchar(60) NULL,yscz4'+@dc+' varchar(60) NULL,'
+'yscz5'+ @dc+' varchar(60) NULL,yscz6'+@dc+' varchar(60) NULL,'
+'yscz7'+ @dc+' varchar(60) NULL,yscz8'+@dc+' varchar(60) NULL'

fetch next from cur_dc into @dc
end
close cur_dc
deallocate cur_dc
set @createtable=@createtable+') '
exec(@createtable)

--***********************************動態創建全局臨時表******************************************* 一}

------------------------二、插入符合條件的型體資料-----------------
set @insertsql=' insert into ##temp_cpgzdc (xt,pp_mc,xlb_mc,xt_dj) '
+' select distinct xt_dm,pp_mc,xlb_mct,xt_dj '
+' from xt,pp,xlb '
+' where xt_pp=pp_dm and xt_xlb=xlb_dm '
+' and substring(xt_ksly,1,4)='''+@year+''''
+' and xt_ks='''+@ks+''''
+@conditionPP
exec(@insertsql)


由于我要动态生成我sql语句,我的sql语句要生成一个临时表,我原本是生成一个本地临时表,但通过exec@sql()生成的,在后面的代码中不访问它,所以我创建了全局临时表
解决了此问题,但现在新的问题又来了,当多用户同时运行此过程时会出现冲突呀,由于现在运行此过程时间有点长,此矛盾就更体现出来了.
如何解决此矛盾呢,有碰到过的指点一下.
...全文
117 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
postfxj 2009-02-13
  • 打赏
  • 举报
回复
如何加锁呀。
zlb_x 2009-02-12
  • 打赏
  • 举报
回复
顶 。
claro 2009-02-12
  • 打赏
  • 举报
回复
帮顶。
hwr_hwr 2009-02-12
  • 打赏
  • 举报
回复
试试用事务来变相处理呢
postfxj 2009-02-12
  • 打赏
  • 举报
回复
把全局临时表后面加用户代码.
postfxj 2009-02-12
  • 打赏
  • 举报
回复
看来用全局临时表指定是不行了,用本地临时表更不行.
要是能把全局表加锁就好了,用完后释放它.同时用的让它等人家释放后才能用,但这个方法一时办法还找不到好办法,各位用办法吗?
看来是没有什么好办法了.要是oracle就不存在这样的问题.
用用户来控制是最简单的方法了,以前我也有这样弄过.不知有没有更高级点的办法.
肥龙上天 2009-02-12
  • 打赏
  • 举报
回复
加上锁,
全局临时表用完之后手动删除。
dobear_0922 2009-02-11
  • 打赏
  • 举报
回复
--exec(@createtable)

--***********************************動態創建全局臨時表******************************************* 一}

------------------------二、插入符合條件的型體資料-----------------
set @insertsql=' insert into ##temp_cpgzdc (xt,pp_mc,xlb_mc,xt_dj) '
+' select distinct xt_dm,pp_mc,xlb_mct,xt_dj '
+' from xt,pp,xlb '
+' where xt_pp=pp_dm and xt_xlb=xlb_dm '
+' and substring(xt_ksly,1,4)='''+@year+''''
+' and xt_ks='''+@ks+''''
+@conditionPP
exec (@createtable+@insertsql)
nzperfect 2009-02-11
  • 打赏
  • 举报
回复
那就用7楼办法,将 @insertsql 和 @createtable合并后再 exec
sinomote 2009-02-11
  • 打赏
  • 举报
回复
可以考虑在临时表中加一列,用来记录每个用户的编号(保证每次访问的编号都不一样),在操作的时候根据编号来确定用户自己需要的数据。
水族杰纶 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 perfectaction 的回复:]
SQL code那就别用全局临时表

用局部临时表,在exec()里应该是可以用的.如:
create table #(id int)
declare @sql varchar(200)
set @sql='insert into # select 1'
exec(@sql)
select * from #
[/Quote]
DECLARE @S VARCHAR(8000)
SET @S='SELECT * INTO # FROM TB SELECT * FROM #'
EXEC(@S)
shaqing_0928 2009-02-11
  • 打赏
  • 举报
回复
同意2楼的说法
postfxj 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wufeng4552 的回复:]
用實表代替全局臨時看看~~
[/Quote]
用实表和全局的效果是一样的.
postfxj 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 perfectaction 的回复:]
SQL code那就别用全局临时表

用局部临时表,在exec()里应该是可以用的.如:
create table #(id int)
declare @sql varchar(200)
set @sql='insert into # select 1'
exec(@sql)
select * from #
[/Quote]
你的那表不是通过exec(@sql)创建的,所以可以,我的表的栏位是动态的,所以要动态创建.
postfxj 2009-02-11
  • 打赏
  • 举报
回复
如果用局部临时表,后面想查询一下它,就查不到了呀 exec(@)完后局部临时表就没有了呀
nzperfect 2009-02-11
  • 打赏
  • 举报
回复
那就别用全局临时表

用局部临时表,在exec()里应该是可以用的.如:
create table #(id int)
declare @sql varchar(200)
set @sql='insert into # select 1'
exec(@sql)
select * from #
水族杰纶 2009-02-11
  • 打赏
  • 举报
回复
用實表代替全局臨時看看~~

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧