update 人员情况表 set yhzh=yhzh+1 WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null) and yhzh = (select max(yzzh) from tableA)
declare @idno int set @idno = (select max(yhzh) from tableA where (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null) ) update set @idno = @idno+1,yhzh = @idno from tableA where (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
update 人员情况表 set yhzh=yhzh+1 WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null) and yhzh = (select max(yzzh) from 人员情况表)
SQL SERVER企业管理器-->右键你的表-->设计表-->选中你要设置标识的字段(必须是int或其他数字类型)-->下面有个属性叫“标识”的,选“是”,你可以按你的要求改一下标识种子或标识递增量,然后保存该表的设计。create table 表(id int identity(1,1),name varchar(100)) insert 表 values('J9988') insert 表 values('CSDNM') select * from 表 drop table 表
insert shbx2.人员情况表 (yhzh,其他列1,其他列2) select max(yhzh)+1,'其他列1的值','其他列2的值' from shbx2.人员情况表 WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
--如果是的话,就要这样更新:declare @yhzh int SELECT @yhzh=max(yhzh) FROM shbx2.人员情况表 WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)if @yhzh is null set @yhzh=0update shbx2.人员情况表 set @yhzh=@yhzh+1,yhzh=@yhzh WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
create table #aa(id int,name varchar(10)) insert into #aa values(1,'aa') insert #aa values(3,'bv') insert #aa values(NULL,'bdfa') insert #aa values(NULL,'bfg') insert #aa values(NULL,'wer') select * from #aa declare @i int set @i=0 update #aa set @i=@i+1,id=(select max(id) from #aa)+@i where id is null select * from #aa
: zjcxc(邹建) ( 你说得比我想得都清楚
结果为: id name ----------- ---------- 1 aa 3 bv NULL bdfa NULL bfg NULL wer ---------------- id name ----------- ---------- 1 aa 3 bv 4 bdfa 5 bfg 6 wer
declare @i int set @i=0 update 人员情况表 set @i=@i+1,yhzh=(select max(yhzh) from 人员情况表 where (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null))+@i WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
insert shbx2.人员情况表 (yhzh,其他列1,其他列2) select isnull(max(yhzh),0)+1,'其他列1的值','其他列2的值' from shbx2.人员情况表 WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null) 一句可以
wzh1215(四脚蛇) 你多了一行
呵呵,好明白啊,你说的应该是新加的数据吧, 先Select MAX(yhzh) from 表 然后再加上1 再在新写入的数据的yhzh字段给定这个数值就应该可以了, ASP很好写的 Select max(??) from ?? New??=Rs("??")+1 insert into [??] values('" & New?? & "'.......)
declare @i int set @i=0 update 人员情况表 set @i=@i+1,yhzh=(select max(yhzh) from 人员情况表 where (if_shff = '是') and (gzzk='离休' or gzzk='退休'))+@i WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
and yhzh = (select max(yzzh) from tableA)
set @idno = (select max(yhzh) from tableA where (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
)
update set @idno = @idno+1,yhzh = @idno from tableA where (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
and yhzh = (select max(yzzh) from 人员情况表)
eg:
问题:在实际业务处理中有些单号需要自动增长,但又不能用自增列代替
eg: P031106001 -- 第一位P表示采购单,031106表示日期,后三位是流水号。
作者:txlicenhe/* 1:创建测试用的表结构 */
create table tablename(pono char(10),b int)
go
/* 2:创建一个得到当前日期的视图,为下面的自字义函数作准备 */
create view vGetdate
as
select getdate() as today
go
/* 3:用自定义函数来得到单号(因自定函数内不能用getdate()来得到当前日期,要用到上面的视图) */
create function getDH()
returns char(10)
As
begin
declare @dh1 char(10),@dh2 char(10)
select @dh1 = max(pono) from tableName
Set @dh1 = IsNull(@dh1,'P000000000')
select @dh2 = Left(@dh1,1) + right(convert(varchar(8),today,112),6) + '001' from vGetdate
if @dh1 >= @dh2
begin
set @dh2 = left(@dh1,7) + right('000'+ cast(cast(right(@dh1,3) as int)+1 as varchar),3)
end
return(@dh2)
end
go
/* 4:在字段默认值中填入 dbo.getdh() */
alter table tablename add constraint df_tablename_1 default(dbo.getdh()) for pono
-/* 5:测试:*/
insert tablename(b) values(1)
insert tablename(b) values(2)
Select * from tablename
-- 测试结果
pono b
---------- -----------
P031115001 1
P031115002 2
总结:此方法运用到了一些小技巧
1:用字段默认值来实现单号自增
2:用自定义函数来得到字段的默认值
3:因在自定义函数中不能用getdate()之类非确定的函数,用视图来得到当前日期
yhzh if_shff gzzk
1 是 离休
null 是 离休
null 是 离休
2 是 离休
3 是 离休想更新NULL的,最终得到的结果是:yhzh if_shff gzzk
1 是 离休
4 是 离休
5 是 离休
2 是 离休
3 是 离休
insert 表 values('J9988')
insert 表 values('CSDNM')
select * from 表
drop table 表
SELECT @yhzh=max(yhzh)
FROM shbx2.人员情况表
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)if @yhzh is null set @yhzh=0update shbx2.人员情况表 set @yhzh=@yhzh+1,yhzh=@yhzh
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
1、 在人员情况表里,有yhzh不空的行, 我要把最大的取出来2、 同时有yhzh为空的行,我要把所有yyzh为空的字段,接着刚才的最大值递增3、 把增加后的结果还保存到表里 就是原来的行 我觉得说得清楚了
insert into #aa values(1,'aa')
insert #aa values(3,'bv')
insert #aa values(NULL,'bdfa')
insert #aa values(NULL,'bfg')
insert #aa values(NULL,'wer')
select * from #aa
declare @i int
set @i=0
update #aa
set @i=@i+1,id=(select max(id) from #aa)+@i
where id is null
select * from #aa
id name
----------- ----------
1 aa
3 bv
NULL bdfa
NULL bfg
NULL wer
----------------
id name
----------- ----------
1 aa
3 bv
4 bdfa
5 bfg
6 wer
set @i=0
update 人员情况表
set @i=@i+1,yhzh=(select max(yhzh) from 人员情况表 where (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null))+@i
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)
一句可以
先Select MAX(yhzh) from 表
然后再加上1
再在新写入的数据的yhzh字段给定这个数值就应该可以了,
ASP很好写的
Select max(??) from ??
New??=Rs("??")+1
insert into [??] values('" & New?? & "'.......)
set @i=0
update 人员情况表
set @i=@i+1,yhzh=(select max(yhzh) from 人员情况表 where (if_shff = '是') and (gzzk='离休' or gzzk='退休'))+@i
WHERE (if_shff = '是') and (gzzk='离休' or gzzk='退休') and (yhzh is null)