开发者

Tough SQL Update

2 databases QF AND TK

QF has开发者_如何转开发 the following:

Imagine you have a table called FunctionalGroup with this data:

FunctionalGroupID | FunctionalGroup
1                    Engineering
2                    Purchasing

And a table that was a set of login's with a functionalgroupID to reference the group the person is in...

LoginID | FunctionalGroupID | Login
1                  1           Jon
2                  1           Joe
3                  2           Jane

So Jon and Joe are engineering while Jane is purchasing..simple enough

Now there is another database TK.

TK has the following table Login with something to this effect:

Login | FunctionalGroupID
Jon          Purchasing
Joe          Purchasing
Jane         Purchasing

Notice how Jon and Joe in this database are now part of the purchasing group...But notice how this field is the text field and no ID. So what I want to do is use this table as the master data source and update the QF table such that the logins table from the QF now looks like this:

LoginID | FunctionalGroupID | Login
1                  2           Jon
2                  2           Joe
3                  2           Jane

That is update this table to make Jon and Joe part of the purchasing group by setting their functionalgroupid = 2. Because 2 means purchasing.

I tried this:

UPDATE
Login 
SET Login.FunctionalGroupID = FunctionalGroup.FunctionalGroupID
FROM Login INNER JOIN
TKKCommonData.dbo.Login lz
ON lz.Login = Login.Login
AND lz.FunctionalGroupID = FunctionalGroup.FunctionalGroup

But I get an error:

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "FunctionalGroup.FunctionalGroup" could not be bound.

This seems so easy but Im just not sure how to write the update statement. Im just looking to join the tables by the Login (which is the users name) and then by the Functionalgroup names.

I even tried this EDIT per Jay's answer with same error message

UPDATE
QuikFix.dbo.Login 
SET QuikFix.dbo.Login.FunctionalGroupID = QuikFix.dbo.FunctionalGroup.FunctionalGroupID
FROM QuikFix.dbo.Login INNER JOIN
TKKCommonData.dbo.Login 
ON TKKCommonData.dbo.Login.Login = QuikFix.dbo.Login.Login
AND TKKCommonData.dbo.Login.FunctionalGroupID = QuikFix.dbo.FunctionalGroup.FunctionalGroup
WHERE TKKCommonData.dbo.Login.LoginID= 101


You need an additional INNER JOIN:

UPDATE Login  
SET
    Login.FunctionalGroupID = FunctionalGroup.FunctionalGroupID 
FROM Login
    INNER JOIN TKKCommonData.dbo.Login lz
        ON lz.Login = Login.Login 
    INNER JOIN FunctionalGroup
        ON lz.FunctionalGroupID = FunctionalGroup.FunctionalGroup 


Specify the database name for all of the tables in your query instead of just TKKCommonData.dbo.Login, seems like it can't find the FunctionalGroup table in the database the query is running against.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜