Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update/Case query help


Update/Case query help

Author
Message
Renuka__
Renuka__
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1024 Visits: 1370
Hi,

I am trying to write an update query but not able to get the result as I want. Any help is much appreciated.

I have 2 table as shown below

TABLE 1:

ServerName Domain
A X
B X
C Y
D Y
E Z
F Z

TABLE 2:

ServerName Domain
A X
B X
C X
D Y
E Y
F Z

The domains are different in table 2 for the same servernames. All I am trying to do is to update the domain column in Table 2 same as Table 1 (as domain column in Table 1 is accurate) for all the matching servers and for the servers not matching I want to update it as Unknown.

Thank you
Renuka

Renuka__
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8995 Visits: 19028
Write a SELECT statement joining the two tables which outputs the PK of the table you want to update, the existing domain value, and the value you want to change it to.
Check that the output matches your requirements.
It's then a simple matter to convert this SELECT into an UPDATE...FROM. If you are unsure then post the SELECT and someone will step in to help you.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
danielw 23392
danielw 23392
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 142
Hi,

Could you not do this using a MERGE statement?

So using table 'Table 1' as the SOURCE and 'Table 2' as the TARGET?

When MATCHED - Update the Domain column in Table 2 to be Table1.Domain

When NOT MATCHED - Update the Domain column in Table 2 to be 'Unknown'

Dan
Stefan_G
Stefan_G
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 953
No need for anything fancy.

This should work:

update Table2
set Domain = isnull((select Domain from Table1 t1 where t1.ServerName = Table2.ServerName), 'Unknown')


Renuka__
Renuka__
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1024 Visits: 1370
Thank you for the replies.

I was trying to avoid complex queries too; however, inner Select query in the UPDATE statement returns multiple domain names, so it returns 'subquery returned more than 1 value' error and the update fails.

I am still trying...

Thank you.

Renuka__
Stefan_G
Stefan_G
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 953
Renuka__ (8/30/2013)
Thank you for the replies.

I was trying to avoid complex queries too; however, inner Select query in the UPDATE statement returns multiple domain names, so it returns 'subquery returned more than 1 value' error and the update fails.

I am still trying...

Thank you.


If you are using my code this error means that you have multiple domains in table1 for at least one server.
This means that you must decide which Domain you wan to use in this case.

If using the last domain in alphabetic order is ok for you something like this should work:

update Table2
set Domain = isnull((select max(Domain) from Table1 t1 where t1.ServerName = Table2.ServerName), 'Unknown')


Renuka__
Renuka__
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1024 Visits: 1370
Thank you Stefan_G.

Yes; we do have some servers with same name in one domain and non-domain(workgroup servers). Max(domain) does work, but unfortunately that is not the case with all the servers. This query was needed for a report and for now we have fixed it by adding an additional column to specify if one is a valid domain or not and report it based on that.

Before adding the additional columns, the issue was infact fixed using 'MERGE'.

Thank you all for the replies and suggestions.

Renuka__
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search