if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].db
o.[##tmp0]'))
drop table [dbo].[##tmp0]
if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].db
o.[##tmp1]'))
drop table [dbo].[##tmp1]
这两句是判断数据库里是否存在表##tmp0嗖##tmp1,如果存在,则drop即删除
o.[##tmp0]'))
drop table [dbo].[##tmp0]
if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].db
o.[##tmp1]'))
drop table [dbo].[##tmp1]
这两句是判断数据库里是否存在表##tmp0嗖##tmp1,如果存在,则drop即删除
解决方案 »
- sql中in后面变量的问题
- 向sqlserver中写入1万条记录,用15秒,写5万记录用72秒;向access写入1万记录用14秒,写5万记录用75秒;请问...
- 请教问题
- 游标里取一标志怎么取
- "错误1222:已超过了锁请求超时时段"是什么意思
- 求数据库同步方案SQL SERVER2005和ORACLE10G
- 急,MS-SQL SERVER远程telnet ip 1433连接失败
- 统计
- ##temp 和 #temp 有什么区别
- 一个表其中一个字段为datetime类型,令其默认getdate(),生成sql文件看不到此默认
- 关于时间函数转换的问题?
- sQL6.5里的事务处理???????????
drop table [dbo].[##tmp0] --如果存在临时表##tmp0,则删掉
if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].dbo.[##tmp1]'))
drop table [dbo].[##tmp1] --如果存在临时表##tmp0,则删掉
select a.listno,a.name,b.no,b.name as namedetail
into ##Tmp0
from test1 as a,test1detail as b
where a.listno=b.rela and b.itemid='001' --把test1 和test1detail两个表中test1detail 的itemid ='001'
--同时两个表的字段相等的a.listno=b.rela记录插入到临时表##tmp0中select distinct a.listno,a.name,space(200) as Nonew,space(200) as namenew
into ##tmp1
from test1 as a,test1detail as b
where a.listno=b.rela --把test1 和test1detail两个表中字段相等的a.listno=b.rela记录插入到临时表##tmp1中
Declare @Num1 int --声明一个变量
select @Num1=max(no) from ##Tmp0 --把临时表##Tmp0中最大的no赋值给变量@Num1update ##tmp1 set ##tmp1.Nonew=' ',##tmp1.namenew=' ' --修改临时表##tmp1,使得这两个字段为空。
P_Agin: --跳转标签
if not exists(select * from ##Tmp0) goto P_exit --如果临时表##Tmp0中没有数据,则跳转到 P_exit
update ##tmp1 set
##tmp1.Nonew=##tmp1.Nonew+','+convert(nvarchar(2),##tmp0.no),
##tmp1.namenew=##tmp1.namenew+','+##tmp0.namedetail
from ##tmp0
where ##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1 --把临时表##tmp1中和临时表##tmp0中字段
--##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1
--相等的记录的值
--替换成本身加逗号加##tmp0中的字段值delete from ##tmp0 where ##tmp0.no=@Num1 --删除临时表##tmp0中最大no的记录
set @Num1=@Num1-1 --@Num1值减1
goto P_Agin --跳转到P_Agin,继续作以上工作
P_exit: --跳转标签
update ##tmp1 set
nonew=right(nonew,len(nonew)-2),
namenew=right(namenew,len(namenew)-2) --修改临时表##tmp1,让这两个字段取回原来的值。select * from ##tmp1 --筛选出临时表##tmp1中的记录
1detail as b
where a.listno=b.rela and b.itemid='001'
把符合a.listno=b.rela and b.itemid='001'条件的数据(a.listno,a.name,b.no,b.name as namedetail )插入到##tmp0表select distinct a.listno,a.name,space(200) as Nonew,space(200) as namenew into ##
tmp1 from test1 as a,test1detail as b
where a.listno=b.rela
把符合a.listno=b.rela条件的数据(a.listno,a.name,space(200) as Nonew,space(200) as namenew )插入到##tmp1表
o.[##tmp0]')) --判断是否存在全局临时表##tmp0
drop table [dbo].[##tmp0]
--存在则删除
if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].db
o.[##tmp1]'))
--判断是否存在全局临时表##tmp1
drop table [dbo].[##tmp1]
--存在则删除
select a.listno,a.name,b.no,b.name as namedetail into ##Tmp0 from test1 as a,test
1detail as b
where a.listno=b.rela and b.itemid='001'
--把表test1中符合条件的数据及列插入到##Tmp0,插入过程中系统会根据查询列自动创建##tmp0
select distinct a.listno,a.name,space(200) as Nonew,space(200) as namenew into ##
tmp1 from test1 as a,test1detail as b
where a.listno=b.rela--把表test1中符合条件的数据及列插入到##Tmp1,插入过程中系统会根据查询列自动创建##tmp1Declare @Num1 intselect @Num1=max(no) from ##Tmp0
--取最大值并付给@num1
update ##tmp1 set ##tmp1.Nonew=' ',##tmp1.namenew=' '
--付值
P_Agin:
if not exists(select * from ##Tmp0) goto P_exit
--定义子过程
update ##tmp1 set ##tmp1.Nonew=##tmp1.Nonew+','+convert(nvarchar(2),##tmp0.no),##
tmp1.namenew=##tmp1.namenew+','+##tmp0.namedetail
from ##tmp0 where ##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1
delete from ##tmp0 where ##tmp0.no=@Num1
set @Num1=@Num1-1goto P_Agin --执行子过程P_exit: --定义子过程
update ##tmp1 set nonew=right(nonew,len(nonew)-2),namenew=right(namenew,len(namen
ew)-2)select * from ##tmp1 --返回结果
P_Agin: --标签
if not exists(select * from ##Tmp0) goto P_exit --如果##tmp0不存在记录则跳转到 P_exit标签
update ##tmp1 set ##tmp1.Nonew=##tmp1.Nonew+','+convert(nvarchar(2),##tmp0.no),##
tmp1.namenew=##tmp1.namenew+','+##tmp0.namedetail
from ##tmp0 where ##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1
delete from ##tmp0 where ##tmp0.no=@Num1
set @Num1=@Num1-1
goto P_Agin
P_exit:
update ##tmp1 set nonew=right(nonew,len(nonew)-2),namenew=right(namenew,len(namen
ew)-2)
select * from ##tmp1
o.[##tmp0]')) --判断tempDB库中是否存在临时表##tmp0
drop table [dbo].[##tmp0] --如果存在临时表##tmp0则删除它.
if exists(select * from tempdb.dbo.sysobjects where id = object_id(N'[tempdb].db
o.[##tmp1]')) --判断tempDB库中是否存在临时表##tmp1
drop table [dbo].[##tmp1] --如果存在临时表##tmp1则删除它.
select a.listno,a.name,b.no,b.name as namedetail into ##Tmp0 from test1 as a,test
1detail as b --从表test1和test1detail中查出字段listno,name,no,name的值并插入临
where a.listno=b.rela and b.itemid='001'--时表##Tmp0中
select distinct a.listno,a.name,space(200) as Nonew,space(200) as namenew into ##
tmp1 from test1 as a,test1detail as b --从表test1和test1detail中查出不重复的字段
--istno,name的值,并生成含有200个空格的新字段Nonew和nameNew.
--同时插入临##tmp1中.
where a.listno=b.rela
Declare @Num1 int --定义一个整形的@Num1变量。select @Num1=max(no) from ##Tmp0 --从临时表##Tmp0中查出最大值的No并赋给@Num1变量。update ##tmp1 set ##tmp1.Nonew=' ',##tmp1.namenew=' '
--将临时表##tmp1中所有Nonew和namenew字段置为空串。
P_Agin: --定义一个标签。
if not exists(select * from ##Tmp0) goto P_exit
--如果临时表##Tmp0中没有记录则转到标签P_exit。
update ##tmp1 set ##tmp1.Nonew=##tmp1.Nonew+','+convert(nvarchar(2),##tmp0.no),##
tmp1.namenew=##tmp1.namenew+','+##tmp0.namedetail
from ##tmp0 where ##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1
--修改##tmp1字段 Nonew为Nonew+','+(##tmp01.no的前两个字符).
delete from ##tmp0 where ##tmp0.no=@Num1 --删除临时表##tmp0中所有N0=@Num1的记录。
--delete语句应该写成这样 delete ##tmp0 where ##tmp0.no=@Num1
set @Num1=@Num1-1 --变量@Num1-1.
goto P_Agin --转到标签P_Agin .
P_exit: --定义标签P_exit.
update ##tmp1 set nonew=right(nonew,len(nonew)-2),namenew=right(namenew,len(namen
ew)-2) --修改表##tmp1的nonew字段为 nonew的除前两个字符外的所有字符。namenew一样。
select * from ##tmp1 --查询表##tmp1所有记录。
o.[##tmp0]'))
--删除全局临时表select a.listno,a.name,b.no,b.name as namedetail into ##Tmp0 from test1 as a,test
1detail as b
where a.listno=b.rela and b.itemid='001'
把满足条件的数据插入新生成的临时表 ##Tmp0中select @Num1=max(no) from ##Tmp0 选出最大的no赋予变量 @num1update ##tmp1 set ##tmp1.Nonew=' ',##tmp1.namenew=' '
更新表的nonew namenew列为空if not exists(select * from ##Tmp0) goto P_exit 如果##tmp0不存在数据 跳转到p_exit
update ##tmp1
set ##tmp1.Nonew=##tmp1.Nonew+','+convert(nvarchar(2),##tmp0.no),##
tmp1.namenew=##tmp1.namenew+','+##tmp0.namedetail
from ##tmp0 where ##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1更新表##tmp1 满足##tmp0.listno=##tmp1.listno and ##tmp0.no=@Num1的条件
列nonew为##tmp1的nonew值加上##tmp0.no值
namenew为##tmp1的namenew值加上##tmp0.namedetail值
P_exit:
update ##tmp1
set nonew=right(nonew,len(nonew)-2),namenew=right(namenew,len(namen
ew)-2)
更新表##tmp1
列nonew取nonew从第三个字符开始的子串 如果原来为oeien 更新为ien
namenew同上