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

关于字段插入的简单问题
有一个空表table1,只有2列:   id1,   id2
其中id1是key。

我想把这个空表插入10000行如下数据:
id1,   id2
1       X100
2       X102
3       X103
4       X104

....

10000       X10099

请问怎么写sql?



------解决方案--------------------
CREATE TABLE table1(ID1 INT IDENTITY(1,1),ID2 VARCHAR(20))
DECLARE @I INT
SET @I = 100
WHILE @I <= 10099 BEGIN
INSERT INTO table1 SELECT 'X ' + RTRIM(@I)
SET @I = @I + 1
END;
------解决方案--------------------
DECLARE @t INT
SET @t = 1
WHILE @I <= 10000
INSERT INTO table1 SELECT @t, 'x ' + cast(@t*100 as varchar(5))
SET @t = @t + 1

------解决方案--------------------
declare @table1 table(id1 int identity, id2 varchar(10))
declare @num int
select @num=102
insert @table1 select 'X100 '
while @num <=10099
begin
insert @table1 select 'X '+convert(varchar(10),@num)
set @num=@num+1
end

select * from @table1
------解决方案--------------------

CREATE TABLE #TABLE(id1 int identity, id2 varchar(10))
DECLARE @NUM INT
SET @NUM = 100
INSERT INTO #TABLE SELECT 'X '+RTRIM(@NUM)

SET @NUM = 102
WHILE @NUM <= 110
BEGIN
INSERT INTO #TABLE SELECT 'X '+RTRIM(@NUM)
SET @NUM = @NUM+1
END

SELECT * FROM #TABLE
DROP TABLE #TABLE