How to generate random data in SQL server -
i want create stored procedure insert random data in 'video' table. have generated 30,000 record data userprofile table.
note: username 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
it's not difficult generate random data, in sql
for example, random username userprofile table.
begin -- random row table declare @username varchar(50) select @username = [username] ( select row_number() over(order [username]) [row], [username] [userprofile] ) t t.row = 1 + (select cast(rand() * count(*) int) [userprofile]) print(@username) end
to generate random integer...
begin -- random integer between 3 , 7 (3 + 5 - 1) declare @totalviews int select @totalviews = cast(rand() * 5 + 3 int) print(@totalviews) end
to generate random varchar string
begin -- random varchar ascii char 32 128 declare @videoname varchar(160) declare @length int select @videoname = '' set @length = cast(rand() * 160 int) while @length <> 0 begin select @videoname = @videoname + char(cast(rand() * 96 + 32 int)) set @length = @length - 1 end print(@videoname) end
and finally, random date
begin -- random datetime +/- 365 days declare @uploadtime datetime set @uploadtime = getdate() + (365 * 2 * rand() - 365) print(@uploadtime) end
Comments
Post a Comment