Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 93
  • Last Modified:

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

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
brgdotnet
Asked:
brgdotnet
  • 4
  • 3
1 Solution
 
SimonCommented:
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
 
brgdotnetAuthor Commented:
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
 
SimonCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
brgdotnetAuthor Commented:
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
 
SimonCommented:
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
 
brgdotnetAuthor Commented:
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
 
SimonCommented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now