Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-12-12
7
Medium Priority
?
88 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 free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 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