Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- My first personal challenge.
- Randomly get firstName and lastName from Person.Person table and insert into a new table.
- First name and last names do not have to be from the same businessEntityID.
- Used loop and RAND() to generate random businessEntityID where first and last names are derived.
- */
- USE AdventureWorks2012
- GO
- --Create new table
- CREATE TABLE tblRandomNames
- (businessEntityID int primary key not null identity(1,1), FirstName nvarchar(50) NOT NULL, LastName nvarchar(50) NOT NULL)
- GO
- -- INSERT NAMES INTO tblRandomNames. Pick firstNames and lastNames randomly from person.person
- DECLARE @FirstName nvarchar(50),
- @LastName nvarchar(50),
- @maxBusinessEntityID int,
- @minBusinessEntityID int,
- @counter int = 1,
- @businessEntityIDPicked int,
- @numberOfNames int = 5000; --<Set the number of first-lastname pairs you want to generate>
- SELECT @maxBusinessEntityID = MAX(businessEntityID) FROM Person.Person
- SELECT @minBusinessEntityID = MIN(businessEntityID) FROM Person.Person
- WHILE (@counter<=@numberOfNames)
- BEGIN
- -- Generate random Firstname
- SELECT @FirstName = FirstName
- FROM Person.Person
- WHERE BusinessEntityID = FLOOR(RAND()*(@maxBusinessEntityID - @minBusinessEntityID+1)+1)
- --Generate random LastName
- SELECT @LastName = LastName
- FROM Person.Person
- WHERE BusinessEntityID = FLOOR(RAND()*(@maxBusinessEntityID - @minBusinessEntityID+1)+1)
- INSERT INTO tblRandomNames
- VALUES (@FirstName, @LastName)
- PRINT @FirstName + ' ' + @LastName
- SET @counter = @counter +1
- END
- select * from tblRandomNames
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement