我在SP中有一个临时表。
CREATE TABLE #bclist (
bcastid INT
,userid INT
,etype INT
,articles0 BIGINT
,seq INT identity(1, 1)
)我还有一个变量@nextseq,我想要的是像下面这样的东西
CREATE TABLE #bclist (
bcastid INT
,userid INT
,etype INT
,articles0 BIGINT
,seq INT identity(@nextseq, 1)
)但是SQl服务器不允许这个.Any解决方案?
发布于 2011-11-12 12:48:14
您可以使用
DBCC CHECKIDENT (#bclist, RESEED, @nextseq)但是,如果您的SP不是从dbowner运行的,则它将不起作用。您可以做的另一件事是对表重新设定种子。
if @nextseq > 1
begin
TRUNCATE table #bclist
SET IDENTITY_INSERT #bclist ON
INSERT INTO #bclist (seq)
VALUES (@nextseq-1 )
SET IDENTITY_INSERT #bclist OFF
DELETE FROM #bclist
End不要忘记if条件,否则在你的序列中,你将从2而不是1得到值。
发布于 2011-11-12 13:06:40
全局临时表怎么样?
DECLARE @foo varchar(5);
SET @foo = '4';
DECLARE @query nvarchar(max);
-- a global temp table will exist for the lifetime of
-- the connection
SET @query = replace(N'
CREATE TABLE ##bclist (
bcastid INT
,userid INT
,etype INT
,articles0 BIGINT
,seq INT identity(@seed, 1)
)', '@seed', @foo)
EXECUTE(@query)
INSERT INTO
##bclist
SELECT 0,1,2,3
select * from ##bclist
drop table ##bclist结果
bcastid userid etype articles0 seq
0 1 2 3 4https://stackoverflow.com/questions/8102612
复制相似问题