23

I want to create a stored procedure to insert random data in 'Video' table. I have already generated 30,000 record data for UserProfile table.

Note: The username is FK element in Video table.

CREATE TABLE UserProfile 
(
  Username VARCHAR(45)  NOT NULL  ,
  UserPassword VARCHAR(45)  NOT NULL  ,
  Email VARCHAR(45)  NOT NULL  ,
  FName VARCHAR(45)  NOT NULL  ,
  LName VARCHAR(45)  NOT NULL  ,
  Birthdate DATE   ,
  Genger VARCHAR(10)  NOT NULL  ,
  ZipCode INT    ,
  Image VARCHAR(50)      ,

  PRIMARY KEY(Username)
);
GO

CREATE TABLE Video 
(
  VideoId INT  NOT NULL DEFAULT 1000 ,
  Username VARCHAR(45)  NOT NULL  ,
  VideoName VARCHAR(160)  NOT NULL  ,
  UploadTime DATE    ,
  TotalViews INT    ,
  Thumbnail VARCHAR(100)      ,

  PRIMARY KEY(VideoId),
  FOREIGN KEY(Username)
    REFERENCES UserProfile(Username)
);
GO
4
  • 3
    Article here : mitchelsellers.com/blogs/2008/09/12/… Commented Jul 31, 2013 at 17:53
  • 4
    Why not use a tool instead of re-inventing the wheel? Commented Jul 31, 2013 at 17:59
  • 4
    @AaronBertrand If this was a free tool I would agree with you, but it's nearly $400! Commented May 25, 2016 at 17:54
  • 1
    @PhillipCopley That's only a problem, really, if you and/or your employer don't place any value on your time and you are fully capable of solving the same problem in an equal or better way. There are reasons people buy software - usually it's because that's more cost effective than writing their own and/or the free versions don't fulfill all requirements. Please read: The cost of reinventing the wheel. Commented May 25, 2016 at 18:01

4 Answers 4

30

It's not too difficult to generate random data, even in SQL

For example, to get a random username from your userprofile table.

BEGIN
-- get a random row from a table
DECLARE @username VARCHAR(50)
SELECT @username = [Username] FROM (
    SELECT ROW_NUMBER() OVER(ORDER BY [Username]) [row], [Username]
    FROM [UserProfile]
) t 
WHERE t.row = 1 + (SELECT CAST(RAND() * COUNT(*) as INT) FROM [UserProfile])

print(@username)
END

To generate a random integer...

BEGIN
-- get a random integer between 3 and 7 (3 + 5 - 1)
DECLARE @totalviews INT
SELECT @totalviews = CAST(RAND() * 5 + 3 as INT)
print(@totalviews)
END

To generate a random varchar string

BEGIN
-- get a random varchar ascii char 32 to 128
DECLARE @videoname VARCHAR(160)
DECLARE @length INT
SELECT @videoname = ''
SET @length = CAST(RAND() * 160 as INT)
WHILE @length <> 0
    BEGIN
    SELECT @videoname = @videoname + CHAR(CAST(RAND() * 96 + 32 as INT))
    SET @length = @length - 1
    END
print(@videoname)
END

And finally, a random date

BEGIN
-- get a random datetime +/- 365 days
DECLARE @uploadtime DATETIME
SET @uploadtime = GETDATE() + (365 * 2 * RAND() - 365)
print(@uploadtime)
END
Sign up to request clarification or add additional context in comments.

Comments

10

As Aaron already suggested in his comment – I wouldn’t really reinvent the wheel.

Just go and find some of the already available tools for this.

Here is an article on how to do this in VS and thread here on StackOverflow.

Data generators for SQL server?

Comments

3
    declare @i int
    set @i = 0 
    while (@i < 7)


    Begin

    BEGIN
    -- get a random row from a table
    DECLARE @username VARCHAR(50)
    DECLARE @length INT
    SELECT @username = ''
    SET @length = CAST(RAND() * 50 as INT)
    WHILE @length <> 0
        BEGIN
        SELECT @username = @username + CHAR(CAST(RAND() * 96 + 32 as INT))
        SET @length = @length - 1
        END
    END


    BEGIN
    -- get a random integer between 3 and 7 (3 + 5 - 1)
    DECLARE @totalviews INT
    SELECT @totalviews = CAST(RAND() * 5 + 3 as INT)
    print(@totalviews)
    END

    BEGIN
    -- get a random varchar ascii char 32 to 128
    DECLARE @videoname VARCHAR(160)

    SELECT @videoname = ''
    SET @length = CAST(RAND() * 160 as INT)
    WHILE @length <> 0
        BEGIN
        SELECT @videoname = @videoname + CHAR(CAST(RAND() * 96 + 32 as INT))
        SET @length = @length - 1
        END
    END

    BEGIN
    -- get a random datetime +/- 365 days
    DECLARE @uploadtime DATETIME
    SET @uploadtime = GETDATE() + (365 * 2 * RAND() - 365)
    END

    insert into table_1 values(@videoname, @username, @totalviews, @length, @uploadtime)
    SET @i = @i + 1
    end

1 Comment

please add some description explaining answer.
2

http://www.generatedata.com/#generator This is great. Limits you to 300 rows or so. But, works well. The paid version generates more data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.