好的,首先我要显示的查询非常粗糙。关于如何优化它的指针会很棒,但这不是我现在的主要目的,我需要一些有用的东西。我将发布查询和结果
SELECT u.[UserName]
,u.[LoweredUserName]
,m.[BarCode]
,m.[MemberID]
,d.[FirstName]
,d.[LastName]
FROM [sqlmdstgbiz02].[WebDB].[dbo].[aspnet_Users] u,
[mdsqlst].[CMS_PRODUCTION].[dbo].[memMember] me,
[mdsqlst].[CMS_PRODUCTION].[dbo].[Demographics] d,
[sqlmd05stg\sqlmd05stg].EntranceControl.dbo.MemberBarCodes m
where
(substring(u.UserName, len(u.UserName)-12,13) = m.Barcode or
substring(u.UserName, len(u.UserName)-11,12) = m.Barcode or
substring(u.UserName, len(u.UserName)-10,11) = m.Barcode or
substring(u.UserName, len(u.UserName)-9,10) = m.Barcode or
substring(u.UserName, len(u.UserName)-8,9) = m.Barcode) and
me.MemberID = m.MemberID and me.DemographicsID = d.DemographicsID
代码的结果是
UserName BarCode MemberID FirstName LastName
down120000008 120000008 8300100364005 TOUCH DOWN
test120000009 120000009 8300100606009 KET TAG TEST
abbott123567567 123567567 8300100635008 HENRY ABBOTT
现在您可以看到UserName很简单,Lastname和BarCode组合在一起。我需要做的是使用LastName和MemberID更新UserName,但这样可以让我用一个脚本更新数百个UserName。所有这些信息都在不同的服务器上,所以我知道我必须做一个DBA正在设置的链接服务器。有任何更多信息,请询问。
答案 0 :(得分:2)
假设这是SQL Server,您可以使用UPDATE FROM来执行更新。
BEGIN TRAN
UPDATE u
SET UserName = d.FirstName + m.MemberID
OUTPUT INSERTED.*
FROM [sqlmdstgbiz02].[WebDB].[dbo].[aspnet_Users] u
, [mdsqlst].[CMS_PRODUCTION].[dbo].[memMember] me
, [mdsqlst].[CMS_PRODUCTION].[dbo].[Demographics] d
, [sqlmd05stg\sqlmd05stg].EntranceControl.dbo.MemberBarCodes m
WHERE (substring(u.UserName, len(u.UserName)-12,13) = m.Barcode
or substring(u.UserName, len(u.UserName)-11,12) = m.Barcode
or substring(u.UserName, len(u.UserName)-10,11) = m.Barcode
or substring(u.UserName, len(u.UserName)-9,10) = m.Barcode
or substring(u.UserName, len(u.UserName)-8,9) = m.Barcode
)
and me.MemberID = m.MemberID
and me.DemographicsID = d.DemographicsID
ROLLBACK TRAN
修改强>
正如HLGEM正确指出的那样,你应该用适当的INNER JOIN语句替换隐式语法。
BEGIN TRAN
UPDATE u
SET UserName = d.FirstName + m.MemberID
OUTPUT INSERTED.*
FROM [mdsqlst].[CMS_PRODUCTION].[dbo].[memMember] me
INNER JOIN [mdsqlst].[CMS_PRODUCTION].[dbo].[Demographics] d ON d.DemographicsID = me.DemographicsID
INNER JOIN [sqlmd05stg\sqlmd05stg].EntranceControl.dbo.MemberBarCodes m ON m.MemberID = me.MemberID
INNER JOIN [sqlmdstgbiz02].[WebDB].[dbo].[aspnet_Users] u ON u.UserName LIKE '%' + m.Barcode
ROLLBACK TRAN