Solved

SQL MERGE Statement with Target Field Variable

Posted on 2013-12-02
5
506 Views
Last Modified: 2016-02-10
Hey Experts,

I am stuck on a SQL Merge Procedure that I am not even sure is possible. Basically, I have a table that stores field name references based on a few criteria. I have another table with fields Score0 through Score9. I have yet a third table with a list of values in a single column that is updated via an SSIS package.

What I need to do is create a Merge Statement (as SSIS does not appear to be able to do what I need it to do). The trick here is the field is dynamic based on the criteria selected. AN example of what I want is below, but the syntax is wrong.

1. Is this possible
2. I do not really care if this is done in SSIS or in a Stored Procedure called from the SSIS Package, I am just looking to automate all of this as the package updates hourly and I cannot manually feed variables in as the criteria evolves.

Stored Procedure
DECLARE @DestField AS varchar(6)

MERGE dbo.MasterScore AS T
USING dbo.QMCMScores AS S
ON (T.CCQ = S.CCQ AND T.Session = S.Session AND T.EMPLID = S.EMPLID)
WHEN NOT MATCHED BY TARGET THEN
         INSERT(CCQ,Session,EMPLID,@DestField)
         VALUES(S.CCQ,S.Session,S.EMPLID,S.Score);

Open in new window


I know I need to SET @DestField with the following (SELECT TargetField FROM dbo.ScoreLookup WHERE [System] = S.System AND CCQ = S.CCQ AND AssessmentID = S.AssessmentID) but I can't seem to figure out where this would go, or it would even work?
0
Comment
Question by:Kds4evr
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:virtuadept
ID: 39692717
Instead of USING dbo.QMCMScores as S, try making that a subselect join:

USING (
   SELECT blah blah blah FROM dbo.QMCMScores as s1
   INNER JOIN dbo.ScoreLookup as s2
   ON s1.System = s2.System
   AND s1.CCQ = s2.CCQ
   AND s1.AssessmentID = s2.AssessmentID ) as S

If sub select is not allowed there (I'm not sure) then you could do that as a temp table above the MERGE statement and use temp table there.


Also replace "blah blah blah" with the actual field names you want to pull into the join that you're going to want to merge.
0
 

Author Comment

by:Kds4evr
ID: 39693440
Hey virtuadept,

Thanks for the reply. I understand the approach with the sub-query. I guess the issue lies with how to reference the returned TargetField in the INSERT statement. Currently I receive an error message "The insert list used in the MEREGE statement cannot contain multi-part indetifiers. Use single part identifiers instead"

MERGE dbo.MasterScore AS T
USING (SELECT S1.[System], S1.CCQ, S1.Session, S1.AssessmentID, S1.EMPLID, S1.Score, S2.TargetField FROM dbo.QMCMScores as s1
   INNER JOIN dbo.ScoreLookup as s2
   ON s1.System = s2.System
   AND s1.CCQ = s2.CCQ 
   AND s1.AssessmentID = s2.AssessmentID ) AS S
ON (T.CCQ = S.CCQ AND T.Session = S.Session AND T.EMPLID = S.EMPLID)
WHEN NOT MATCHED BY TARGET THEN
         INSERT(CCQ,Session,EMPLID,S.TargetField)
         VALUES(S.CCQ,S.Session,S.EMPLID,S.Score);

Open in new window


The issue appears to be the S.TargetField in the column list which gives me an invalid column name. Thoughts?
0
 
LVL 8

Expert Comment

by:virtuadept
ID: 39708851
If it says it can not use multi-part identifiers try using an "AS SomeColName" after each column in the SELECT part of the sub-select. Maybe it needs named identifiers in the sub-select?
0
 

Accepted Solution

by:
Kds4evr earned 0 total points
ID: 39719468
Yeah, I tried that as well with the same result. It just does not seem to like a variable in there in either case. I was able to do something similar using an SSIS Conditonal Split for each column. Not pretty, but it works for now. Thanks for your help.
0
 

Author Closing Comment

by:Kds4evr
ID: 39728788
Used SSIS Solution with Conditonal Split
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

SQL Server  2012 Release with lots of Enhancements in Database Engine functions, SSIS, SSRS and some of new services like Data Quality Server and Master Data Service. Of particular interest, and the focus of this Article is SSIS. So, time to elab…
Lessons learned during ten years of interviewing for SQL Server Integration Services (SSIS) and other Extract-Transform-Load (ETL) contract roles and two years of staff manager interviewing contractors.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

15 Experts available now in Live!

Get 1:1 Help Now