日期:2014-05-19  浏览次数:20464 次

怎么让 SQL Server 中第一列自动增加?能否用代码实现?
比如这样增加:
YG001
YG002
YG003
。。。。。。

SE001
SE002
SE003
。。。。。。

就是前面有两个字符在干扰

------解决方案--------------------
数据库里没法设,可以用代码:不知你用什么语言,
思路是这样:
1、 如果取SE, select max(convert(int,right(这段,3))) from 表,取出最大,
2、值加1
------解决方案--------------------
YG跟SE是由什么决定的?
------解决方案--------------------
declare @t table(id int identity(1,1),bs as 'YG '+left( '000 ',3-len(id))+cast(id as varchar),value varchar(10))
insert into @t select 'a '
union all select 'b '
union all select 'c '

select * from @t


/*
id bs value
----------- ----------------------------------- ----------
1 YG001 a
2 YG002 b
3 YG003 c

(所影响的行数为 3 行)
*/

------解决方案--------------------
DECLARE @CNT INT,@s varchar(10)
set @s= 'SE '
SELECT @CNT=COUNT(1) FROM dbo.test WHERE CHARINDEX(COL,@s)> 0
INSERT INTO dbo.test (COL) SELECT @s+RIGHT( '000 '+CAST(@CNT+1 AS VARCHAR),3)
------解决方案--------------------
楼主的意思是不是在插入数据的时候进行处理,那么可以用触发器。
可以从第三位开始获取数据来进行自增处理。
------解决方案--------------------
http://blog.csdn.net/roy_88/archive/2006/12/01/1424370.aspx
这里有个列子
楼上在建表时定义计数列不推荐
因为计算列是随ID列变化的,不利于维护数据库
------解决方案--------------------
用触发器不行吗?
------解决方案--------------------
sql= "select student_isput from 表 "
if 有记录 then
record=记录总数
redim type_id(record)
typeid=0
for i=0 to ubound(type_id)
type_id(i)=myarray1(i,0)
if cint(type_id(i)) <> cint(i+1) then
typeid=i+1
exit for
end if
next

if typeid=0 then typeid=type_id(i-1)+1
else
typeid=1
Record=0
end if