2
ALTER PROC [dbo].[Usp_SelectQuestion]
   @NoOfQuestion int
AS
BEGIN
  Declare @CNT int
  Declare @test int
  Declare @x int
  Declare @y int

  set @x = 1;
  set @y = 1; 
  set @CNT=(Select Count(*) from (select Distinct(setno)from onlin) AS A)
  set @test=@NoOfQuestion/@CNT

  while (@x <= @CNT)  
  begin
    select top (@test) * from onlin where setno = @x order by NEWID()
      set @x = @x + 1
  end
END

在这个存储过程中,我将每个循环的输出作为单个表,因此我将多个表作为输出,但我希望单个表中的所有行我认为通过联合我们可以实现,但我不知道如何使用。

4

3 回答 3

1

用这个:

CREATE TEMPORARY TABLE tmp SELECT * FROM tableName;

然后插入表

INSERT INTO tableName SELECT * FROM tmp;
于 2012-12-20T09:34:26.853 回答
0

创建一个temp table并插入到循环中,然后结果将是:

SELECT * FROM tempTable
于 2012-12-20T09:05:10.713 回答
0
declare @temptable as table(col1 int, col2 varchar(50),... etc)

  while (@x <= @CNT)  
  begin
     insert into @temptable   'temptable columns need to match columns in the SELECT statement
        select top (@test) * from onlin where setno = @x order by NEWID()

     set @x = @x + 1
  end

  select * from @temptable
于 2012-12-20T09:34:08.437 回答