Solved

Using a join in dynamic sql

Posted on 2016-09-16
3
32 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
3 Comments
 
LVL 49

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 13

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Restore Procedure question 4 31
Help with SQL Query 23 39
Trouble connecting to SqlServer database 4 32
Sql query for filter 12 22
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

757 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

14 Experts available now in Live!

Get 1:1 Help Now