?
Solved

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

Posted on 2014-12-12
7
Medium Priority
?
85 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40497817
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
ID: 40498310
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:Simon
ID: 40498313
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 2

Author Comment

by:brgdotnet
ID: 40498953
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:Simon
ID: 40499147
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
ID: 40499261
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:
Simon earned 2000 total points
ID: 40499274
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

777 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