将随机名称插入为SQL值

时间:2018-11-05 09:49:04

标签: sql-server random names

因此,我创建了一个数据库,该数据库是一个拥有客户的银行。但在该数据库中,我编写了一个函数,该函数可以返回50个客户(在我的情况下为50个John Smiths)

...因为我在该代码中用硬编码了我的客户的姓名John Smith和他的电子邮件地址。现在,我想找到一种方法,用一个用于姓氏和姓氏的变量替换我硬编码的名称,该变量将仅返回随机名称。像James Doe,Sally Fields等...

请指出正确的方向,以正确的名称替换随机的约翰·史密斯。

这是我的函数代码,因此您可以了解我在说什么以及我在寻找什么。

BEGIN
     DECLARE @loopCount tinyint 
     DECLARE @tableCount tinyint
     DECLARE @randomSSN int
     SELECT @loopCount = 1
     WHILE (@loopCount <= 50)
        BEGIN
            SELECT @randomSSN = RAND()*(999999998)+1
            SELECT @tableCount = COUNT(*) FROM Customer WHERE SSN = @randomSSN
            IF @tableCount = 0
                 INSERT INTO Customer (FirstName, LastName, Email, SSN) VALUES ('John', 'Smith', 'JohnSmith@gmail.com', @randomSSN)
                SELECT @loopCount = @loopCount +1
        END
END

2 个答案:

答案 0 :(得分:1)

您可以使用类似的东西

BEGIN
 DECLARE @loopCount tinyint 
 DECLARE @tableCount tinyint
 DECLARE @randomSSN int
 SELECT @loopCount = 1
 WHILE (@loopCount <= 50)
    BEGIN
        SELECT @randomSSN = RAND()*(999999998)+1
        SELECT @tableCount = COUNT(*) FROM Customer WHERE SSN = @randomSSN
        IF @tableCount = 0

             WITH cte as (
                  SELECT *
                   FROM (VALUES ('Ann','smith'),('Sarah','Paulson'),('Phil','Coulson'),('John','Palmer'),
                                ('Steve','Jobs'),('Bill','Gates')) AS t(first_name,Last_name)
                  )
             INSERT INTO Customer (FirstName, LastName, Email, SSN) 
             VALUES (
             (select top 1 first_name from cte order by newid()), 
             (select top 1 Last_name from cte order by newid()), 
             'JohnSmith@gmail.com', 
              @randomSSN)

            SELECT @loopCount = @loopCount +1
    END
END

答案 1 :(得分:1)

首先,您需要创建一组要使用的名称

  CREATE TABLE [SampleData] 
   ( [Id] INTEGER NOT NULL IDENTITY(1, 1),
     [FirstName] VARCHAR(255) NULL,
     [LastName] VARCHAR(255) NULL,
     [Email] VARCHAR(255) NULL,
     PRIMARY KEY ([SampleDataID]) );

 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Hop','Elijah','egestas.Aliquam.nec@velvenenatis.org'),('Darius','Aquila','est.congue.a@urna.edu'),('Matthew','Murphy','ac.feugiat@dapibusligulaAliquam.ca'),('Myles','Ross','non@ornaresagittisfelis.edu'),('Zephania','Martin','quam.vel@Crasdictum.co.uk'),('Wang','Hammett','nisi@nuncsitamet.ca'),('Jameson','Christopher','tellus@faucibusorci.com'),('Nicholas','Abdul','pharetra.Quisque@non.edu'),('Clark','Cyrus','elit.erat@purusaccumsan.org'),('Sebastian','Octavius','sed.dictum.eleifend@interdumligulaeu.edu');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Hu','Zeph','eu.odio@diamluctus.co.uk'),('Clark','Caleb','et.magnis@vulputate.com'),('Cadman','Allistair','montes.nascetur@arcuCurabitur.com'),('Mark','Michael','Integer@rhoncusid.net'),('Nehru','Jelani','Suspendisse@ad.com'),('Carlos','Brody','lacus.Quisque@placeratvelit.net'),('Wayne','Joel','amet.risus.Donec@arcu.com'),('Seth','Malik','Integer.eu@eueleifend.net'),('Adam','Rooney','ligula.Aliquam@tortordictum.co.uk'),('Lucian','Zahir','quis.arcu@tinciduntnibh.co.uk');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Jerry','Evan','ullamcorper@consectetueradipiscing.com'),('Victor','Micah','dui@egetipsum.com'),('Reuben','Connor','hendrerit.id@urna.org'),('Coby','Stuart','purus@velitCraslorem.org'),('Raja','Calvin','Sed@ligula.edu'),('Linus','Honorato','cursus@nectempus.com'),('Felix','Octavius','porttitor.scelerisque.neque@Inatpede.com'),('Stephen','Preston','sapien@MaurisnullaInteger.edu'),('Andrew','Arsenio','hendrerit.Donec@iaculisneceleifend.ca'),('Alan','Eagan','Phasellus.dolor@sodales.edu');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Elmo','Igor','orci@euismodestarcu.ca'),('Nicholas','Burton','sem.eget@feugiat.com'),('John','Fletcher','Nullam.vitae@rutrumnonhendrerit.edu'),('Cyrus','Noble','primis.in@Maecenas.ca'),('Gary','Noble','Nulla@Fuscealiquetmagna.net'),('Ira','Darius','elit@tincidunt.co.uk'),('Chadwick','Octavius','ipsum@loremipsumsodales.com'),('Ethan','Keaton','Sed.nunc@egetlaoreet.ca'),('Reuben','Kenyon','orci.Ut.sagittis@Inscelerisquescelerisque.edu'),('Ian','Erich','eu.nibh.vulputate@tellusPhaselluselit.org');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Yoshio','Ezekiel','a.malesuada@lacinia.ca'),('Nero','Cedric','malesuada.fringilla.est@et.com'),('Shad','Conan','facilisis@libero.org'),('Yardley','Julian','elit@Sed.co.uk'),('Lawrence','Caleb','at.risus.Nunc@at.net'),('Jacob','Wallace','consectetuer.euismod@ridiculusmusProin.org'),('Barry','Reuben','ridiculus@Sednecmetus.ca'),('Erich','Bruce','Nullam@uteratSed.net'),('Charles','Damian','ac.orci.Ut@Pellentesqueultriciesdignissim.org'),('Noah','Buckminster','elit@semperpretium.org');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Porter','Randall','tempus.risus@purussapiengravida.org'),('Troy','Grady','ac@ipsumportaelit.co.uk'),('Neil','Abdul','interdum.feugiat@leoCras.ca'),('Lane','Phelan','eu.nibh@Morbi.net'),('Sylvester','Keegan','neque.Morbi@euodioPhasellus.com'),('Patrick','Jackson','dis@Donec.ca'),('Jesse','Dillon','Donec@augueid.com'),('Myles','Xavier','ridiculus@nasceturridiculusmus.net'),('Kasimir','Ray','sed.est.Nunc@pedemalesuada.org'),('Noble','Judah','Etiam@velit.edu');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Omar','Wayne','a.scelerisque.sed@nisiMauris.edu'),('James','Patrick','commodo.ipsum@tellus.co.uk'),('Damian','Hasad','Nunc.mauris.elit@euelitNulla.edu'),('Lucius','Leroy','auctor@eleifendnunc.org'),('Uriel','Amos','Curabitur@nullaIn.com'),('Eric','Benedict','non.justo@atnisiCum.edu'),('Stephen','Harlan','Donec@eratvelpede.net'),('Fitzgerald','Nathaniel','euismod.urna.Nullam@rutrummagna.ca'),('Ishmael','Logan','ullamcorper@eu.edu'),('Keefe','Dylan','quis.massa.Mauris@Proinnonmassa.net');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Dennis','Aquila','sagittis.semper.Nam@ipsum.net'),('Dieter','George','erat@dictum.net'),('Kane','Ronan','lectus@vitaeodio.net'),('Jameson','Edward','nibh@erosNam.co.uk'),('Hamish','Fuller','id.risus@risusMorbimetus.edu'),('Elliott','Levi','dapibus.ligula@Nullatemporaugue.org'),('Michael','Thomas','eu.nulla@euturpisNulla.com'),('Hoyt','Coby','Phasellus.at.augue@sedfacilisis.co.uk'),('Cedric','Tad','Vestibulum@ipsumnunc.org'),('Amal','Vernon','eu.ligula.Aenean@dictum.net');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Addison','Craig','semper@elitafeugiat.net'),('Malik','Micah','magnis.dis.parturient@vitae.net'),('Josiah','Cedric','nonummy@vulputateposuerevulputate.ca'),('Hayes','Blaze','sem@Aliquam.com'),('James','Henry','laoreet.posuere.enim@ametnulla.com'),('Thaddeus','Cairo','eros@eliteratvitae.edu'),('Nathaniel','Ishmael','molestie.Sed@auctorMaurisvel.co.uk'),('Wade','Kenyon','vel.arcu@Pellentesque.net'),('Gabriel','Kenyon','iaculis@eliterat.com'),('Gavin','Michael','fringilla.porttitor.vulputate@ipsumCurabitur.org');
 INSERT INTO SampleData([First_Name],[Last_Name],[Email]) VALUES('Devin','Emery','ligula.eu.enim@elitdictum.net'),('Dean','Herrod','elit@Donec.edu'),('Zachery','Bernard','posuere@velitegestaslacinia.org'),('Abdul','Dexter','lorem@nibhAliquamornare.com'),('Norman','Tad','Suspendisse@quisarcuvel.co.uk'),('Eaton','Colin','quis@sit.ca'),('Christian','Daniel','mauris.id@Morbiaccumsan.edu'),('Harding','Daniel','eu.tellus@hendrerit.ca'),('Matthew','Cullen','Sed.eu@ultricessitamet.net'),('Charles','Erich','dictum.eleifend@eu.org');

现在我们可以从创建的列表中选择随机名称

  INSERT INTO Customer (FirstName, LastName, Email, SSN) 
  SELECT FirstName,LastName,Email, RAND()*(999999998)+1 
  FROM SampleDate
  WHERE Id=CAST(RAND()*100 as int)%50