22

我想创建一个存储过程以在“视频”表中插入随机数据。我已经为 UserProfile 表生成了 30,000 条记录数据。

注意:用户名是视频表中的 FK 元素。

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

4 回答 4

27

生成随机数据并不太难,即使是在 SQL 中

例如,从您的 userprofile 表中获取随机用户名。

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

要生成一个随机整数...

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

生成随机 varchar 字符串

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

最后,随机日期

BEGIN
-- get a random datetime +/- 365 days
DECLARE @uploadtime DATETIME
SET @uploadtime = GETDATE() + (365 * 2 * RAND() - 365)
print(@uploadtime)
END
于 2013-07-31T18:09:57.770 回答
10

正如 Aaron 在他的评论中已经建议的那样——我不会真的重新发明轮子。

去寻找一些已经可用的工具。

是一篇关于如何在 VS 中执行此操作的文章,并在 StackOverflow 上讨论。

SQL 服务器的数据生成器?

于 2013-08-01T09:34:47.223 回答
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
于 2017-03-23T13:00:09.613 回答
2

http://www.generatedata.com/#generator 这很棒。将您限制为 300 行左右。但是,效果很好。付费版本生成更多数据。

于 2020-10-20T23:51:06.133 回答