andrew4582

TestData Table with While

Feb 22nd, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.67 KB | None | 0 0
  1. -- SeveralRows -table
  2. IF OBJECT_ID ( 'TestData', 'U' ) IS NOT NULL
  3. DROP TABLE TestData;
  4. GO
  5.  
  6. -- Create the table
  7. CREATE TABLE TestData (
  8.    Id         int        NOT NULL IDENTITY(1,1)PRIMARY KEY,
  9.    InsertTime time       NOT NULL DEFAULT (GETDATE()),
  10.    Category    int       NOT NULL
  11. );
  12. GO
  13.  
  14. SET NOCOUNT ON
  15. DECLARE @max int;
  16. DECLARE @counter int;
  17. DECLARE @rand_number int;
  18.  
  19. SET @max = 10;
  20.  
  21. BEGIN
  22.    SET @counter = 0;
  23.    WHILE @counter < @max BEGIN
  24.       SET @rand_number = RAND() * 10000;
  25.      
  26.       INSERT INTO TestData (Category)
  27.       VALUES (CONVERT(varchar, @rand_number));
  28.  
  29.       SET @counter = @counter + 1;
  30.    END;
  31. END;
  32. GO
  33. SELECT * FROM TestData
Advertisement
Add Comment
Please, Sign In to add comment