日期:2014-05-18  浏览次数:20699 次

求一句简单的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