Advertisement
aragonmelvin

Random Name Generator

Oct 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.54 KB | None | 0 0
  1. /*
  2. My first personal challenge.  
  3. Randomly get firstName and lastName from Person.Person table and insert into a new table.  
  4. First name and last names do not have to be from the same businessEntityID.  
  5. Used loop and RAND() to generate random businessEntityID where first and last names are derived.
  6. */
  7. USE AdventureWorks2012
  8. GO
  9. --Create new table
  10. CREATE TABLE tblRandomNames
  11. (businessEntityID int primary key not null identity(1,1), FirstName nvarchar(50) NOT NULL, LastName nvarchar(50) NOT NULL)
  12. GO
  13. -- INSERT NAMES INTO tblRandomNames.  Pick firstNames and lastNames randomly from person.person
  14. DECLARE @FirstName nvarchar(50),
  15.         @LastName nvarchar(50),
  16.         @maxBusinessEntityID int,
  17.         @minBusinessEntityID int,
  18.         @counter int = 1,
  19.         @businessEntityIDPicked int,
  20.         @numberOfNames int = 5000; --<Set the number of first-lastname pairs you want to generate>
  21. SELECT @maxBusinessEntityID = MAX(businessEntityID) FROM Person.Person
  22. SELECT @minBusinessEntityID = MIN(businessEntityID) FROM Person.Person
  23. WHILE (@counter<=@numberOfNames)
  24. BEGIN
  25.     -- Generate random Firstname
  26.     SELECT @FirstName = FirstName
  27.     FROM Person.Person
  28.     WHERE BusinessEntityID = FLOOR(RAND()*(@maxBusinessEntityID - @minBusinessEntityID+1)+1)
  29.     --Generate random LastName
  30.     SELECT @LastName = LastName
  31.     FROM Person.Person
  32.     WHERE BusinessEntityID = FLOOR(RAND()*(@maxBusinessEntityID - @minBusinessEntityID+1)+1)
  33.     INSERT INTO tblRandomNames
  34.     VALUES (@FirstName, @LastName)
  35.     PRINT @FirstName + ' ' + @LastName
  36.     SET @counter = @counter +1
  37. END
  38. select * from tblRandomNames
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement