在T-SQL中执行“Foreach更新”的最佳方法是什么?

时间:2015-07-06 12:48:26

标签: sql-server sql-server-2008 tsql

我通过SSMS2014使用SQL Server 2008数据库

我正在尝试更新一个包含测试firstname和lastname值的表,这些值存储在“randomnames”表中,其中包含:

Id (Identity)
firstname
lastname

我目前的代码是:

update TestTable
set 
FirstName = (select firstname from randomnames where ID = ABS(Checksum(NewID()) % 4) + 1),
LastName = (select lastname from randomnames where ID = ABS(Checksum(NewID()) % 4) + 1)
;

但是,上述内容会将所有记录设置为“相同”的随机名字和姓氏。我希望所有记录都有不同的随机名字和姓氏,即我需要以上内容为每条记录而不是所有记录运行。

我可能遗漏了一些简单的东西?我需要通过SSMS中的一些SQL来做到这一点。

非常感谢。

感谢。

修改

create table testtable
(
id INT IDENTITY(1,1) PRIMARY KEY,
firstname VARCHAR(100),
lastname VARCHAR(100)
)

创建表randomtab    (     名字VARCHAR(100),     姓氏VARCHAR(100)     )

insert into testtable(firstname,lastname) 
values (NULL,NULL),(NULL,NULL),(NULL,NULL),(NULL,NULL),(NULL,NULL),      
(NULL,NULL),(NULL,NULL)
insert into randomtab(firstname,lastname)
values ('first1','last1'),('first2','last2'),('first3','last3'),   
('first4','last4'),('first5','last5'),('first6','last6'),('first7','last7')


update testtable
set firstname = (select top 1 firstname from randomtab order by NEWID()),
lastname = (select top 1 lastname from randomtab order by NEWID())

SELECT * FROM testtable

update testtable
set firstname = (select top 1 firstname from randomtab order by NEWID()),
lastname = (select top 1 lastname from randomtab order by NEWID())

SELECT * FROM testtable

3 个答案:

答案 0 :(得分:3)

你也可以使用像这样的东西。

SQL Fiddle

设置和数据

CREATE TABLE TestTable
(
    Id INT IDENTITY(1,1),
    firstname VARCHAR(20) DEFAULT(''),
    lastname VARCHAR(20) DEFAULT('')
);


CREATE TABLE randomnames
(
    Id INT IDENTITY(1,1),
    firstname VARCHAR(20),
    lastname  VARCHAR(20)
);

 insert into TestTable DEFAULT VALUES;
 insert into TestTable DEFAULT VALUES;
 insert into TestTable DEFAULT VALUES;
 insert into TestTable DEFAULT VALUES;
 insert into TestTable DEFAULT VALUES;
 insert into randomnames VALUES('F1','L1'),('F2','L2'),('F3','L3'),('F4','L4');

<强>查询

;WITH CTE as
(
    SELECT *,ABS(Checksum(NewID()) % 4) + 1 as fnameid,ABS(Checksum(NewID()) % 4) + 1 as lnameid
    FROM TestTable
)
update CTE
set 
FirstName = (select firstname from randomnames where ID =fnameid),
LastName = (select lastname from randomnames where ID =lnameid);

<强>输出

Id  firstname   lastname
1   F4  L1
2   F3  L3
3   F4  L2
4   F3  L2
5   F2  L1

答案 1 :(得分:2)

我认为当您按随机数订购时,它应该有效: 将您的代码更改为以下内容:

update TestTable
set 
FirstName = (select top 1 firstname from randomnames order by NEWID()),
LastName = (select top1 lastname from randomnames order by NEWID())
;

编辑:为了向您展示我的本地主机是如何工作的 - 这是我的测试批次:

declare @testtable table 
(
    id INT IDENTITY(1,1) PRIMARY KEY,
    firstname VARCHAR(100),
    lastname VARCHAR(100)
)

declare @randomtab table 
(
    firstname VARCHAR(100),
    lastname VARCHAR(100)
)

insert into @testtable(firstname,lastname) 
values (NULL,NULL),(NULL,NULL),(NULL,NULL),(NULL,NULL),(NULL,NULL),(NULL,NULL),(NULL,NULL)
insert into @randomtab(firstname,lastname)
values ('first1','last1'),('first2','last2'),('first3','last3'),('first4','last4'),('first5','last5'),('first6','last6'),('first7','last7')


update @testtable
set firstname = (select top 1 firstname from @randomtab order by NEWID()),
    lastname = (select top 1 lastname from @randomtab order by NEWID())

SELECT * FROM @testtable

update @testtable
set firstname = (select top 1 firstname from @randomtab order by NEWID()),
    lastname = (select top 1 lastname from @randomtab order by NEWID())

SELECT * FROM @testtable

输出:

output

答案 2 :(得分:2)

您是否考虑将ID列的值传递给某个随机数生成函数?你可以有像

这样的东西
update tt
   set FirstName = (select top 1 firstname from randomnames r where r.Id = MyRandomNumberFunction(tt.Id)),
       LastName  = (select top 1 lastname from randomnames r where r.Id = MyRandomNumberFunction(tt.Id)),
  from TestTable tt;

这将确保TestTable中的每一行都具有基于特定参数生成的值。