Solved

Using a join in dynamic sql

Posted on 2016-09-16
3
37 Views
Last Modified: 2016-09-16
Hi

Every time I think that I might be starting to understand this, it seems to put me in my place!

I have the following code:

SET @maxcounter = 0;
--
WHILE @maxcounter < @maxcnt
BEGIN
    SET @crtcnt = @maxcounter + 1;
      SET @sqlstr = 'UPDATE A SET old_SSN' + CAST(@crtcnt AS VARCHAR) + ' = B.old_SSN' + CAST(@crtcnt AS VARCHAR) + 'FROM LFRD_Summary A LEFT OUTER JOIN LFRD B ON A.SSN = B.SSN';
      EXEC (@sqlstr);
    SET @maxcounter = @maxcounter + 1;
END

I get an error which doesn't refer to any of these lines but it ran fine before I put these in.

I have varying numbers of columns for old_ssn in both LFRD and LFRD_Summary.  I used SELECT DISTINCT to get the unique SSNs into LFRD_Summary from LFRD.  Now, I need to get the other column data into LFRD_Summary.

I've tried putting a join together using dynamic SQL to do this, using what I've learned on here, but it doesn't seem to like it.

The variables are declared elsewhere.

Can anybody help me please?

Thanks
Sarah
0
Comment
Question by:ScuzzyJo
[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 Comments
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41801738
do you need to add a space before the "FROM" clause?

try this:

SET @sqlstr = 'UPDATE A SET old_SSN' + CAST(@crtcnt AS VARCHAR) + ' = B.old_SSN' + CAST(@crtcnt AS VARCHAR) + ' FROM LFRD_Summary A LEFT OUTER JOIN LFRD B ON A.SSN = B.SSN';
0
 

Author Comment

by:ScuzzyJo
ID: 41801754
Hi Ryan

Thanks.  I think that works.

It's heartening as maybe I do understand it a bit now if I only left a space out!

Thanks
Sarah
0
 
LVL 14

Expert Comment

by:Megan Brooks
ID: 41801769
It should become clearer within a year or two. :-) To actually understand it might take a bit longer.

Some basic questions:
What's the error message? (I don't see a space in front of FROM)
How big are the tables?
How often do you need to do this? Once?

The reason I ask the last two questions is that ordinarily I wouldn't use a WHILE loop (if I HAVE to loop, I usually use a well-configured cursor), but if it's a one-time thing or the amount of data is small then perhaps any old solution will do. Dealing with denormalized fields of this sort can be messy and possibly not worth the effort to do it 'right.'
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

749 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