SQL:在While循环中循环

时间:2017-01-17 13:38:38

标签: sql sql-server loops while-loop

我试图在Parent表中插入10K名称,在后面添加数字时使用10个描述的名称。但是只能写第一千。我需要你的眼睛才能看到我失败的地方。我明白@i不会增加,但不知道为什么。

DECLARE @TempNameTable table (ID int , Name varchar (50))
DECLARE @i int = 1,
    @tempNameValue varchar(50),
    @randNumber int = 1

INSERT INTO @TempNameTable VALUES 
     (1,'Jonas'), (2,'Petras'),(3,'Antanas')
   , (4,'Stasys'), (5,'Dainius'), (6,'Giedrius')
   , (7,'Mindaugas'), (8,'Povilas'), (9,'Kestutis')
   , (10,'Darius')


WHILE ((SELECT COUNT(Name) FROM Parent) < 10000)
BEGIN
WHILE @i < 11
    BEGIN
        SET @tempNameValue = CASE
            WHEN @i = 1 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            WHEN @i = 2 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            WHEN @i = 3 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            WHEN @i = 4 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            WHEN @i = 5 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            WHEN @i = 6 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            WHEN @i = 7 THEN
                (SELECT Name from @TempNameTable WHERE ID = @i)
            END
        WHILE @randNumber < 1000
        BEGIN
            INSERT INTO Parent VALUES 
             (@tempNameValue + CAST(@randNumber as varchar(1000)))
            SET @randNumber = @randNumber + 1
        END
    SET @i = @i + 1
    END

 END

3 个答案:

答案 0 :(得分:3)

我想你的查询会变成:

DECLARE @TempNameTable table (ID int , Name varchar (50))
DECLARE @tempNameValue varchar(50),
@randNumber int = 1

INSERT INTO @TempNameTable VALUES
(1,'Jonas'), (2,'Petras'), (3,'Antanas'),
(4,'Stasys'), (5,'Dainius'), (6,'Giedrius'),
(7,'Mindaugas'), (8,'Povilas'), (9,'Kestutis'), (10,'Darius')

WHILE ((SELECT COUNT(Name) FROM Parent) < 10000)
BEGIN
    SET @randNumber = 1
    WHILE @randNumber <= 1000
    BEGIN
        INSERT INTO Parent 
        SELECT Name + CAST(@randNumber as varchar(1000))
        FROM @TempNameTable
        SET @randNumber = @randNumber + 1
    END
END

答案 1 :(得分:0)

I'd do it like the below. Just tested and it outputs each name * 1,000 rows. It's more verbose but its obvious what it's doing and it works.

Declare @Jonas varchar(20) = 'Jonas'
Declare @Petras varchar(20) = 'Petras'
Declare @Antanas varchar(20) = 'Antanas'
Declare @Stasys varchar(20) = 'Stasys'
Declare @Dainius varchar(20) = 'Dainius'
Declare @Giedrius varchar(20) = 'Giedrius'
Declare @Mindaugas varchar(20) = 'Mindaugas'
Declare @Povilas varchar(20) = 'Povilas'
Declare @Kestutis varchar(20) = 'Kestutis'
Declare @Darius varchar(20) = 'Darius'

Declare @TempJonas varchar(20)
Declare @TempPetras varchar(20)
Declare @TempAntanas varchar(20)
Declare @TempStasys varchar(20)
Declare @TempDainius varchar(20)
Declare @TempGiedrius varchar(20)
Declare @TempMindaugas varchar(20)
Declare @TempPovilas varchar(20)
Declare @TempKestutis varchar(20)
Declare @TempDarius varchar(20)

Declare @NameIncrement int = 0

WHILE @NameIncrement <= 1000
BEGIN
    Set @TempJonas = @Jonas + CONVERT(varchar(6),@NameIncrement)
    Set @TempPetras = @Petras + CONVERT(varchar(6),@NameIncrement)
    Set @TempAntanas = @Antanas + CONVERT(varchar(6),@NameIncrement)
    Set @TempStasys = @Stasys + CONVERT(varchar(6),@NameIncrement)
    Set @TempDainius = @Dainius + CONVERT(varchar(6),@NameIncrement)
    Set @TempGiedrius = @Giedrius + CONVERT(varchar(6),@NameIncrement)
    Set @TempMindaugas = @Mindaugas + CONVERT(varchar(6),@NameIncrement)
    Set @TempPovilas = @Povilas + CONVERT(varchar(6),@NameIncrement) 
    Set @TempKestutis = @Kestutis + CONVERT(varchar(6),@NameIncrement)
    Set @TempDarius = @Darius + CONVERT(varchar(6),@NameIncrement)   

    Insert Into Parent
    Select @TempJonas

    Insert Into Parent
    Select @TempPetras

    Insert Into Parent
    Select @TempAntanas

    Insert Into Parent
    Select @TempStasys

    Insert Into Parent
    Select @TempDainius

    Insert Into Parent
    Select @TempGiedrius

    Insert Into Parent
    Select @TempMindaugas

    Insert Into Parent
    Select @TempPovilas

    Insert Into Parent
    Select @TempKestutis

    Insert Into Parent
    Select @TempDainius

    Set @NameIncrement = @NameIncrement + 1     
END

答案 2 :(得分:0)

设置无循环的答案:

rextester:http://rextester.com/EBM78452

declare @TempNameTable table (id int , Name varchar (50))
insert into @TempNameTable values
    (1,'Jonas'), (2,'Petras'), (3,'Antanas')
  , (4,'Stasys'), (5,'Dainius'), (6,'Giedrius')
  , (7,'Mindaugas'), (8,'Povilas'), (9,'Kestutis')
  , (10,'Darius');

if object_id('tempdb..#r') is not null drop table #r;
create table #r (n int not null primary key, r int not null);

with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, d as (
  select n=row_number() over (order by (select 1))
       , r=convert(int,((rand(checksum(newid())) * 10)+1))
    from         n as deka
      cross join n as hecto
      cross join n as kilo   
      cross join n as tenK
    )
insert into #r (n, r)
select n, r from d;
--insert into Parent
select Name=Name+convert(varchar(13),n)
  from #r as r
    inner join @TempNameTable t on r.r=t.id

这使用convert(int,((rand(checksum(newid())) * 10)+1))生成1到10的随机数,并使用它将临时表#r加入@TempNameTable