Solved

SQL MERGE Statement with Target Field Variable

Posted on 2013-12-02
5
512 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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