在 SQL 值中插入随机名称
Inserting Random Names in as SQL values
所以我创建了一个数据库,它是一个银行,里面有客户;但在该数据库中,我编写了一个函数 returns 50 个客户(或者在我的例子中,50 个 John Smiths)
...因为我在该代码中硬编码了我客户的姓名 John Smith 和他的电子邮件地址。现在我想找到一种方法来替换我用硬编码的名字和姓氏的变量,该变量将仅 return 随机名称。像 James Doe、Sally Fields 等...
请指出正确的方向,可以正确地用随机名称替换 John Smiths。
这是我的函数的代码,因此您可以了解我在说什么以及我在寻找什么。
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
你可以使用这样的东西
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
首先,您需要创建一组要使用的名称
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');
现在我们可以 select 从创建的列表中随机取名
INSERT INTO Customer (FirstName, LastName, Email, SSN)
SELECT FirstName,LastName,Email, RAND()*(999999998)+1
FROM SampleDate
WHERE Id=CAST(RAND()*100 as int)%50
所以我创建了一个数据库,它是一个银行,里面有客户;但在该数据库中,我编写了一个函数 returns 50 个客户(或者在我的例子中,50 个 John Smiths)
...因为我在该代码中硬编码了我客户的姓名 John Smith 和他的电子邮件地址。现在我想找到一种方法来替换我用硬编码的名字和姓氏的变量,该变量将仅 return 随机名称。像 James Doe、Sally Fields 等...
请指出正确的方向,可以正确地用随机名称替换 John Smiths。
这是我的函数的代码,因此您可以了解我在说什么以及我在寻找什么。
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
你可以使用这样的东西
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
首先,您需要创建一组要使用的名称
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');
现在我们可以 select 从创建的列表中随机取名
INSERT INTO Customer (FirstName, LastName, Email, SSN)
SELECT FirstName,LastName,Email, RAND()*(999999998)+1
FROM SampleDate
WHERE Id=CAST(RAND()*100 as int)%50