Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. Create PROCEDURE [dbo].[GenerateandExecuteInsertStatmentsforTables]
  2. @NoOfRows int
  3. AS
  4. BEGIN
  5.  
  6. Declare @icount int
  7. declare @qry varchar(max)
  8. set @qry = ' '
  9. set @icount = 0
  10. while @icount < @NoOfRows
  11. begin
  12. select @qry = @qry
  13. +
  14. case when c.column_id = 1 then
  15. 'insert into ' + SCHEMA_NAME(t.schema_id) + '.[' + t.name + '] values('
  16. else
  17. ''
  18. end
  19. +
  20. -- incase the column is identity, i dont include it in the insert
  21. case when c.is_identity = 0 then
  22. case when ty.name in ('bit','bigint','int','smallint','tinyint','float','decimal','numeric','money','smallmoney','real') then
  23. substring(CAST( round(RAND() * 1000,0) AS varchar),1,c.max_length)
  24. when ty.name in ('binary','varbinary') then
  25. substring('0x546869732069732044756D6D792044617461',1,c.max_length)
  26. when ty.name In ('varchar','char','text') then
  27. '''' + substring('Dummy This is Dummy Data',1,c.max_length) + ''''
  28. when ty.name In ('nchar','nvarchar','ntext') then
  29. '''' + substring('Dummy This is Dummy Data',1,c.max_length / 2) + ''''
  30. when ty.name in('date','time','datetime','datetime2','smalldatetime','datetimeoffset') then
  31. '''' + convert(varchar(50),dateadd(D,Round(RAND() * 1000,1),getdate()),121) + ''''
  32. when ty.name in ('uniqueidentifier') then
  33. cast(NEWID() as varchar(33))
  34. else
  35. ''
  36. end
  37. +
  38. case when c.column_id = (Select MAX(insc.column_id) from sys.columns insc where insc.OBJECT_ID = c.OBJECT_ID) then
  39. ');'
  40. else
  41. ','
  42. end
  43. else
  44. ''
  45. end
  46.  
  47. FROM sys.tables AS t
  48. INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
  49. INNER JOIN sys.types AS ty ON c.user_type_id=ty.user_type_id
  50. ORDER BY t.name,c.column_id;
  51. set @icount = @icount + 1
  52.  
  53. --execute the insert statments
  54. --Select (@qry)
  55. exec (@qry)
  56. Set @qry = ' '
  57. end
  58.  
  59. USE [DBNAME]
  60. EXEC [dbo].[GenerateandExecuteInsertStatmentsforTables] 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement