This is one of my first tries to code something. I'm trying to develop a code, to add first name
, last name
, and date
, randomly.
I have this for now:
declare @date date,
@val1 int,
@val2 int
set @date = convert(date,dateadd(day, (abs(CHECKSUM(newid())) % 3650) * -1, getdate()))
insert into tblexecution ( fname,lname,brtday )
values ('name1','Name2',@date)
This way, I need to change manually the name1
and name2
.
is there a way to use, values of another table, in a variable, for this table? I don't know how to explain it but I have a table called Adventureworks2008.person.person
(you guys know this database) with a lot of random names ( Fields > FirstName
,LastName
), and I want to use this values, in my @val1
and @val2
variables, to be something like this:
declare @date date,
@val1 int,
@val2 int
set @date = convert(date,dateadd(day, (abs(CHECKSUM(newid())) % 3650) * -1,
getdate()))
insert into tblexecution ( fname,lname,brtday )
values (@val1,@val2,@date)
so I just loop ( or press f5 a lot ).
Edit1:
With this, I have at least a random int:
select right(rand(),2)
Edit2:
I could do this, But I think this is a poor job:
insert into execution..tblexecution
select distinct firstname, lastname,
convert(date,dateadd(day, (abs(CHECKSUM(newid())) % 3650) * -1,getdate())) as brtday
from adventureworks2008.person.person
Edit3:
I could make this:
declare @date date,
@val1 nvarchar(200),
@val2 nvarchar(200)
set @date = convert(date,dateadd(day, (abs(CHECKSUM(newid())) % 3650) * -1, getdate()));
set @val1=(select top 1 firstname
from adventureworks2008.person.person
order by newid() );
set @val2=(select top 1 lastname
from adventureworks2008.person.person
order by newid() )
insert into tblexecution ( fname,lname,brtday )
values (@val1,@val2,@date)
is this the best way to use random variables in a query?