我想写个循环 将这个Code 的值修改一下 ,第一个值是 “000000” ,后面的值 以此往上加,如:000001,000002等等
谁个会帮忙写一个,谢谢!
10 个解决方案
#1
大家帮帮忙? 先在这里谢过了 坐等
#2
declare @i int
update tb set col=right(100000+@i,5),@i=@i+1
#3
小爱威猛,+1
#4
应该有个条件吧 当code 为null的时候才修改的吧
#5
-- 建表
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
select * from tb;
结果
1 1111
2 2222
3 NULL
4 333
5 444
6 NULL
update tb set tt=a.r
from (
select
r= right('000000'+rtrim(row_number() over (order by id)-1),6),
id
from tb where tt is null
) a
where tb.id=a.id
结果
1 1111
2 2222
3 000000
4 333
5 444
6 000001
#6
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
--select * from tb
/*
id tt
1 1111
2 2222
3 NULL
4 333
5 444
6 NULL
*/
declare @i int
set @i=0
update tb set tt=right(100000+@i,5),@i=@i+1 where tt is null
/*
id tt
1 1111
2 2222
3 00001
4 333
5 444
6 00002
*/
#7
鼠标右键点击表->修改->字段名->列属性->标识规范里面设置标识增量设置为1,是标识设置为 是
#8
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
select * from tb;
declare @i int
declare @num varchar(200)
set @i = 0
set @num = '000000'
update tb set tt=(@num+CONVERT(varchar(200), @i)),@i=@i+1 --where tt is null
select * from tb;
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
select * from tb;
declare @i int
declare @num varchar(200)
set @i = 0
set @num = '000000'
update tb set tt=(@num+CONVERT(varchar(200), @i)),@i=@i+1 --where tt is null
select * from tb;
#9
这个不错。
#10
oracle中比较简单一点:
update tb set tt=substr(
'000000',0,6-length(ROWNUM-1)
)||(ROWNUM-1) WHERE tt IS NULL;
update tb set tt=substr(
'000000',0,6-length(ROWNUM-1)
)||(ROWNUM-1) WHERE tt IS NULL;
#1
大家帮帮忙? 先在这里谢过了 坐等
#2
declare @i int
update tb set col=right(100000+@i,5),@i=@i+1
#3
小爱威猛,+1
#4
应该有个条件吧 当code 为null的时候才修改的吧
#5
-- 建表
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
select * from tb;
结果
1 1111
2 2222
3 NULL
4 333
5 444
6 NULL
update tb set tt=a.r
from (
select
r= right('000000'+rtrim(row_number() over (order by id)-1),6),
id
from tb where tt is null
) a
where tb.id=a.id
结果
1 1111
2 2222
3 000000
4 333
5 444
6 000001
#6
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
--select * from tb
/*
id tt
1 1111
2 2222
3 NULL
4 333
5 444
6 NULL
*/
declare @i int
set @i=0
update tb set tt=right(100000+@i,5),@i=@i+1 where tt is null
/*
id tt
1 1111
2 2222
3 00001
4 333
5 444
6 00002
*/
#7
鼠标右键点击表->修改->字段名->列属性->标识规范里面设置标识增量设置为1,是标识设置为 是
#8
IF OBJECT_ID('tb') IS NOT NULL
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
select * from tb;
declare @i int
declare @num varchar(200)
set @i = 0
set @num = '000000'
update tb set tt=(@num+CONVERT(varchar(200), @i)),@i=@i+1 --where tt is null
select * from tb;
DROP TABLE tb;
GO
CREATE TABLE tb
( id INT identity(1,1),
tt VARCHAR(500)
);
GO
INSERT INTO tb(tt) VALUES('1111');
INSERT INTO tb(tt) VALUES('2222');
INSERT INTO tb(tt) VALUES(null);
INSERT INTO tb(tt) VALUES('333');
INSERT INTO tb(tt) VALUES('444');
INSERT INTO tb(tt) VALUES(null);
select * from tb;
declare @i int
declare @num varchar(200)
set @i = 0
set @num = '000000'
update tb set tt=(@num+CONVERT(varchar(200), @i)),@i=@i+1 --where tt is null
select * from tb;
#9
这个不错。
#10
oracle中比较简单一点:
update tb set tt=substr(
'000000',0,6-length(ROWNUM-1)
)||(ROWNUM-1) WHERE tt IS NULL;
update tb set tt=substr(
'000000',0,6-length(ROWNUM-1)
)||(ROWNUM-1) WHERE tt IS NULL;