当前位置: 代码迷 >> Sql Server >> 求一句简单的sql语句!该怎么解决
  详细解决方案

求一句简单的sql语句!该怎么解决

热度:59   发布时间:2016-04-27 16:38:08.0
求一句简单的sql语句!
insert   into   t1   (number,name)  
(select   name   from   employee     )
我的number是从123到223的数字
name共有100条,该怎么写啊1
使得插入的数据的number从123开始到223结束?
高手指点一下啊!


------解决方案--------------------
更正下:假设你的employee表的主键为employeeid,是自增量
DECLARE @employeeid int
SET @employeeid = 1
WHILE (SELECT number FROM t1) <= 223
BEGIN
INSERT INTO t1 (number,name)
SELECT IDentity(int,123,1) 'RowOrder ',name
FROM employee
WHERE employeeid=1
SELECT MAX(number) FROM t1
IF (SELECT MAX(number) FROM t1) > 223
BREAK
ELSE
SET @employeeid = employeeid + 1
CONTINUE
END
------解决方案--------------------
假设你的employee表的主键为employeeid,是自增量
DECLARE @employeeid int
SET @employeeid = 1
WHILE (SELECT number FROM t1) <= 223
BEGIN
INSERT INTO t1 (number,name)
SELECT IDentity(int,123,1) 'RowOrder ',name
FROM employee
WHERE employeeid = @employeeid
SELECT MAX(number) FROM t1
IF (SELECT MAX(number) FROM t1) > 223
BREAK
ELSE
SET @employeeid = employeeid + 1
CONTINUE
END

------解决方案--------------------
select id=identity(int,123,1),name into #t1 from employee where id <=223
go

insert into t1 (number,name) select id,name from #t1
drop table #t1
  相关解决方案