Solved

I need to update a table but need to link two other tables toghether

Posted on 2014-12-12
7
73 Views
Last Modified: 2014-12-15
I need a query that will update "TableToUpdate" with data found in two other tables ADMIN_USERS
and SourceTable.

Basically I need to set the TableToUpdate.FUAD_ITS_CLERK_ID value to the SourceTable.FUAD_ITS_CLERK_ID
value. The challenge though, is that I also need to set the TableToUpdate.PSUS_USER_NUM value which I
will need to get from the ADMIN_USERS table. So basically I need to link 3 tables together.
Can someone help me to write the query. I don't know how to write it, however if someone can give me the query,
I will study it in detail, and make sure I understand how it works.


SourceTable
-------------
PSUS_USER_ID   nvarchar(225)
FUAD_ITS_CLERK_ID nvarchar(255)
SAM_USER_STATE nvarchar(255)
SAM_USER_CLERK_ID


ADMIN_USERS
--------------
PSUS_USER_NUM (PK, numeric(5,0), not null)
PSUS_USER_ID



TableToUpdate
--------------
PSUS_USER_NUM  (numeric(5,0), not null)
FUAD_ITS_CLERK_ID
0
Comment
Question by:brgdotnet
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Do you want to update existing records or insert new ones? If you are inserting data into both the columns in the target table, it looks like an insert, but maybe you only want to insert rows where a value doesn't already exist in the table or delete rows in the target table where the User_NUM/CLERK_ID combination no longer exists in the source and admin_users tables?
0
 
LVL 2

Author Comment

by:brgdotnet
Comment Utility
Hi SImon,

Dear sir, I need to update existing records in the table. Not insert new ones. Can you or another expert possibly help me?
0
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Please post some sample data from all 3 tables. It only has to be a few rows (with any sensitive data replaced with fake values of the same data type).

Ideally, right click each table in SSMS and select Script/Create/To Clipboard and then paste the clipboard into your reply, preferably within a code block. Part of the problem is that the table structures you included in your question aren't complete. If you're updating the table rather than adding rows, there needs to be a unique key column that is NOT being updated.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Author Comment

by:brgdotnet
Comment Utility
Hello Simon sir. I tried to simplify the problem, by creating a minimum set of sample tables an renaming the variable names. I have been trying various queries, but am stuck. I would be willing to send you money through PayPal for a celebrationary Beer or actual money if you can help me out. Anyway, attached iare my scripted database table. Below is my question rephrased which I believe will help the question to not sound so complicated.

Update Target.ClerkId with the Sheet.ClerkId bearing in mind that you have to first get
the UserNum from the Admin table

Sheet
UserId (varchar(10)
ClerkId INT


Admin
UserNum INT
UserId (varchar(10)


Target
UserNum INT   (varchar(10)


Insert into Admin (UserNumber,UserId) VALUES (1,'c456999')
Insert into Admin (UserNumber,UserId) VALUES (2,'f456499')
Insert into Admin (UserNumber,UserId) VALUES (3,'b678999')
Insert into Admin (UserNumber,UserId) VALUES (4,'r456999')

Insert into Sheet (UserId,ClerkId) VALUES ('c456999','ABNV')
Insert into Sheet (UserId,ClerkId) VALUES ('f456499','JMKH')
Insert into Sheet (UserId,ClerkId) VALUES ('b678999','AYYV')
Insert into Sheet (UserId,ClerkId) VALUES ('r456999','JUUH')

Insert into Target (UserNumber,ClerkId) VALUES (1,'####')
Insert into Target (UserNumber,ClerkId) VALUES (2,'$$$$')
Insert into Target (UserNumber,ClerkId) VALUES (3,null)
Insert into Target (UserNumber,ClerkId) VALUES (4,'####')
tablesScripted.txt
0
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Thanks. that makes it much clearer.

You can use this syntax:
update target
set clerkid=src.clerkid 
from target t inner join
(select clerkid,usernumber from admin A  join sheet S on a.userid=s.userid) as src
on t.usernumber=src.usernumber

Open in new window


This produces the result:
UserNumber      ClerkId
1                     ABNV
2                    JMKH
3                    AYYV
4                    JUUH
0
 
LVL 2

Author Comment

by:brgdotnet
Comment Utility
Wow, you are truly awesome. Thanks so much. I will commit this query to memory, as I consider it a trophy query. Once I commit it to memory and understand it, I am sure it will take me to the next level as a sql programmer. Thanks so much. I wish their was a donate Beer button, because I would definitely give you a six pack. Thanks so much.
0
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
Comment Utility
No problem. It's not a complex query, though this type of update is not quite as easy in TSQL as in MS Access. The key to it is just to join the two required source tables first in a subquery a.k.a. derived table (src) and then join the target table to it.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now