各位大牛,请教表变量问题,有段sql语句,功能是循环利用表变量@tableVar进行相关的操作,如select * from @tableVar:
declare @temp int
set @temp=1
while @temp<=2 --循环2次select * 操作
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
insert into @tableVar(CollectorNo )
select 'abc' union all
select '123' union all
select 'rst'
select * from @tableVar
set @temp=@temp+1
end
结果显示为:
第一次循环显示结果为:
id CollectorNo
---------------
1 'abc'
2 '123'
3 'rst'
---------------第二次循环显示结果为:
id CollectorNo
---------------
1 'abc'
2 '123'
3 'rst'
4 'abc'
5 '123'
6 'rst'
---------------
我的问题一是:我在每次循环内都进行 declare @tableVar 操作,按理说第二次循环内 declare @tableVar 后应该为空呀,但他却依然存在原来的数据:
---------------
1 'abc'
2 '123'
3 'rst'
---------------
是不是Sqlserver的重复资源利用设计,使得@tableVar可以重复利用,就如java的字符串重复利用一样?考虑到上述问题,我打算在每次循环结尾处删掉@tableVar,sql语句如下:
declare @temp int
set @temp=1
while @temp<=2
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
insert into @tableVar(CollectorNo )
select 'abc' union all
select '123' union all
select 'rst'
select * from @tableVar
delete @tableVar----------------------
set @temp=@temp+1
end
结果显示为:
第一次循环显示结果:
id CollectorNo
---------------
1 'abc'
2 '123'
3 'rst'
---------------
第二次循环显示结果为:
id CollectorNo
---------------
4 'abc'
5 '123'
6 'rst'
---------------
问题二:第一次循环的数据没有再在第一次存在(应该是delete @tableVar的效果),但是delete操作貌似只是清空@tableVar的内容而已,因为第二次循环的 ID 值是继上次循环的4,5,6。曾样才能有效的删除表变量@tableVar?为此,我又尝试重设id的初始值:
declare @temp int
set @temp=1
while @temp<=2
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
set identity_insert @tableVar on
insert into @tableVar(id,CollectorNo) select 1,'nul'
insert into @tableVar(CollectorNo )
select 'abc' union all
select '123' union all
select 'rst'
select * from @tableVar
delete @tableVar
set @temp=@temp+1
end
但是,运行不通过:错误提示:set identity_insert @tableVar on 的@tableVar附近有语法错误。问题三:如何有效设置表变量的“自增量字段”的插入功能?
叙述有些长,谢谢您的阅读!!
declare @temp int
set @temp=1
while @temp<=2 --循环2次select * 操作
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
insert into @tableVar(CollectorNo )
select 'abc' union all
select '123' union all
select 'rst'
select * from @tableVar
set @temp=@temp+1
end
结果显示为:
第一次循环显示结果为:
id CollectorNo
---------------
1 'abc'
2 '123'
3 'rst'
---------------第二次循环显示结果为:
id CollectorNo
---------------
1 'abc'
2 '123'
3 'rst'
4 'abc'
5 '123'
6 'rst'
---------------
我的问题一是:我在每次循环内都进行 declare @tableVar 操作,按理说第二次循环内 declare @tableVar 后应该为空呀,但他却依然存在原来的数据:
---------------
1 'abc'
2 '123'
3 'rst'
---------------
是不是Sqlserver的重复资源利用设计,使得@tableVar可以重复利用,就如java的字符串重复利用一样?考虑到上述问题,我打算在每次循环结尾处删掉@tableVar,sql语句如下:
declare @temp int
set @temp=1
while @temp<=2
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
insert into @tableVar(CollectorNo )
select 'abc' union all
select '123' union all
select 'rst'
select * from @tableVar
delete @tableVar----------------------
set @temp=@temp+1
end
结果显示为:
第一次循环显示结果:
id CollectorNo
---------------
1 'abc'
2 '123'
3 'rst'
---------------
第二次循环显示结果为:
id CollectorNo
---------------
4 'abc'
5 '123'
6 'rst'
---------------
问题二:第一次循环的数据没有再在第一次存在(应该是delete @tableVar的效果),但是delete操作貌似只是清空@tableVar的内容而已,因为第二次循环的 ID 值是继上次循环的4,5,6。曾样才能有效的删除表变量@tableVar?为此,我又尝试重设id的初始值:
declare @temp int
set @temp=1
while @temp<=2
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
set identity_insert @tableVar on
insert into @tableVar(id,CollectorNo) select 1,'nul'
insert into @tableVar(CollectorNo )
select 'abc' union all
select '123' union all
select 'rst'
select * from @tableVar
delete @tableVar
set @temp=@temp+1
end
但是,运行不通过:错误提示:set identity_insert @tableVar on 的@tableVar附近有语法错误。问题三:如何有效设置表变量的“自增量字段”的插入功能?
叙述有些长,谢谢您的阅读!!
解决方案 »
- 这语句怎么写?
- 在线等待...SQL查询语句。
- 这样的case语句能否用if。。。else替换??
- 为将要出世的儿子,未来的花朵,姓名征集活动
- error \Program Files\Microsoft SQL Server\90\Shared\sqlmgmproviderxpsp2up.mof
- @@identity 并发量很高时,是否安全?
- 请教:我在一个表的字段建立索引,可是...
- 寻找一个可以根据数据库表生成DELPHI和VB类的软件(现在一下找不到了,以前下载过)
- SQL的Export问题?
- Why在建立JOB時﹐有一個數據庫竟然沒有出現在數據庫清單中??
- 如何仅允许指定的ip连接SQL Server2005服务器
- MS-SQL SERVER数据表更改排序字段
我试过:
declare @sql nvarchar(1000)
set @sql='declare @temp int
set @temp=1
while @temp<=2
begin
declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null)
set identity_insert @tableVar on
insert into @tableVar(id,CollectorNo) select 1,''n'' insert into @tableVar(CollectorNo )
select ''abc'' union all
select ''123'' union all
select ''rst''
select * from @tableVar
delete @tableVar
set @temp=@temp+1
end '
exec(@sql)
但提说 declare @tableVar table(id int identity(1,1) not null, CollectorNo char(3) not null) 的@tableV处有错。
怎么说呢?应为我用的第一个Sqlserver 就是2008的,无法跟以前版本比较。
但用起来的话未打sp1补丁前,对我的512M内存,2.68Ghz的cpu断续地很卡;昨天打了补丁后到现在没卡过;整体不错,但是就2008在技术、性能等方面我就不敢说了,因为我太菜了。但是,,,,,,貌似Sqlserver2008以前的版本也有表变量一回事哟。