日期:2014-05-17  浏览次数:20539 次

sql2000自增ID问题
想要@t增加编从1开始,SQL要怎么写呢?比如:
1
2
3
4
5
........

declare @t int
select @t=1
INSERT s_djs  select 660000773,sx#,jj,jj,0,0,lsj,lsj=cast
 (htj/0.7 as numeric(8,1)),'',66,@t ,null,null,null,0,0,0
 from jsptab  where hs=100

------解决方案--------------------
s_djs表中,编号这列设为自增长,insert的时候,值设为null
------解决方案--------------------
这样写法你接受吗?
----------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-04-17 21:42:24
-- Version:
--      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86) 
-- Jun 17 2011 00:57:23 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([条码] bigint,[品名] varchar(30))
insert [huang]
select 6935938850134,'富贵鸟弹力平角裤' union all
select 6935938851438,'富贵鸟拉毛印花女士保暖内衣' union all
select 6935938851445,'富贵鸟拉毛男士保暖内衣' union all
select 6935938851452,'富贵鸟拉毛印花男士保暖内衣' union all
select 6935938851469,'富贵鸟420G拉毛提花男士保暖内衣' union all
select 6935938867415,'富贵鸟男士休闲袜2双装'
--------------开始查询--------------------------

select *, IDENTITY(INT,1,1)id INTO #t
from [huang] a 

SELECT * FROM #t

----------------结果----------------------------
/* 
条码                   品名                             id
-------------------- ------------------------------ -----------
6935938850134        富贵鸟弹力平角裤                       1
6935938851438        富贵鸟拉毛印花女士保暖内衣                  2
6935938851445        富贵鸟拉毛男士保暖内衣                    3
6935938851452        富贵鸟拉毛印花男士保暖内衣                  4
6935938851469        富贵鸟420G拉毛提花男士保暖内衣              5
6935938867415        富贵鸟男士休闲袜2双装                    6
*/