identity函数中seed能不能用变量?

Johnny_Depp 2008-03-28 06:22:09
identity(int,seed,1)

这个函数的seed能用@tmp这样的变量的代替吗?
我现在调试中,出错

指点下?

不行的话,我趁早想其他方法了??

select  placeid = identity(int,@p,1),* into #newtmp 
from #itmp
...全文
186 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2008-07-12
  • 打赏
  • 举报
回复
declare @t int 
set @t=100

if object_id('tempdb..#t1') is not null
drop table #t1
create table #t1(ID int identity,col1 int)
dbcc checkident('Tempdb..#t1',reseed,@t)

insert #T1
select top 5 ID from sysobjects

select * from #t1


/*
ID col1
----------- -----------
100 4
101 5
102 7
103 8
104 13


*/
fcuandy 2008-07-12
  • 打赏
  • 举报
回复
不能用变量。
改用动态语句又产生了临时表的生命周期问题,并发发生了问题。
qinpeng136 2008-07-12
  • 打赏
  • 举报
回复
看了liangck的代码,帮了我大忙了
Limpire 2008-03-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 Johnny_Depp 的帖子:]
identity(int,seed,1)

这个函数的seed能用@tmp这样的变量的代替吗?
[/Quote]
不能

[Quote=引用 Johnny_Depp 回复:]
@p 是来自存储过程的参数
[/Quote]
临时表一律用identity(int,0,1),@p当成位移参数(placeid+@p)处理。
Johnny_Depp 2008-03-28
  • 打赏
  • 举报
回复
学习下这段代码先.....
liangCK 2008-03-28
  • 打赏
  • 举报
回复
declare @seed int
declare @sql varchar(8000)
set @seed=10

--由于作用域的问题.所以用全局临时表.
set @sql='select id=identity(int,'+cast(@seed as varchar)+',1),* into ## from BOM'
exec (@sql)
select * from ##

drop table ##
liangCK 2008-03-28
  • 打赏
  • 举报
回复
动态sql
Johnny_Depp 2008-03-28
  • 打赏
  • 举报
回复
@p 是来自存储过程的参数
@p int

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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