Her er mit Forum om alt indenfor Sql Server. Hvis du vil yde et bidrag i form af en ny tråd eller bare en kommentar skal du logge på. Er du ikke registreret tager det kun få sekunder at gøre.

SQL Forum

 
SQL ForumSQL ForumSQL serverSQL serverUdvikling og pr...Udvikling og pr...A test table with Random numbersA test table with Random numbers
Previous Previous
 
Next
 Disabled
New Post
 7/13/2010 2:04 PM
 
 Modified By Niels-Jørgen Hvidberg  on 7/13/2010 2:11:41 PM

Here is a little script to create a table filled with random numbers. Sometimes this is usefull when you are testing functions, views or other things.

/* Check if allready created, then delete */
USE [>>your-database-name<<]
GO
SET NOCOUNT ON;
IF OBJECT_ID('dbo.Nums', 'U') IS NOT NULL DROP TABLE dbo.Nums;

/* Create test table. 
This example is with two colomns, IdNumber and  RandomNumber, 
change this for your own need */
CREATE TABLE dbo.Nums(
   IdNumber     INT NOT NULL PRIMARY KEY,
   RandomNumber INT NOT NULL);
DECLARE @max AS INT, @rc AS INT;
SET @max = 100000; /* Change this to as many rows you want */
SET @rc = 1;

/* Fill the table, the '1000' is a multiplying factor */
WHILE @rc <= @max
BEGIN
  INSERT INTO dbo.Nums
    VALUES(@rc, DATEPART(ms,GETDATE())*RAND()*1000);
  SET @rc = @rc + 1;
END

/* show the contents */
SELECT [IdNumber],[RandomNumber]
  FROM [InsideTSQL2008Test].[dbo].[Nums];

The RAND() function together with the milliseconds from DATEPART() gives the RandomNumber. Because the milliseconds are repeating themselves the result is not 100% random, there might be two equal values and there are some cyclic values each 1000 milliseconds.


Man går i skole hele livet
Previous Previous
 
Next
 Disabled
SQL ForumSQL ForumSQL serverSQL serverUdvikling og pr...Udvikling og pr...A test table with Random numbersA test table with Random numbers