Solved

Dynamic SQL Error

Posted on 2016-09-16
4
45 Views
Last Modified: 2016-09-16
Hi

I'm pretty new to using dynamic SQL and have the following bit of code:

SET @maxcounter = 0;
--
WHILE @maxcounter < @maxcnt
BEGIN
      SET @crtcnt = @maxcounter + 1;
      SET @maxcounter = @maxcounter + 1;
      SET @sqlstr = 'UPDATE LFRD SET  old_ssn' + @crtcnt + ' = substring(ConcatF,13*' + @maxcounter + ',13) WHERE old_ssn Is Not Null OR @maxcounter < SSNCount;'
      EXEC (@sqlstr);
      
END

The variables are declared and used earlier, so I'm pretty sure that isn't the problem.

I'm getting the error message:

Msg 245, Level 16, State 1, Line 137
Conversion failed when converting the nvarchar value 'UPDATE LFRD SET  old_ssn1 = substring(ConcatF,13*' to data type int.

Which refers to the @sqlstr =  line.

I'm pretty sure it's my syntax, but I don't understand why it's trying to convert to a data type int.

The line is supposed to fill various columns (the number is dynamic) with data from a concatenated column.  Each block of data is 13 characters long.  I'm trying to say that if @maxcounter is 1, start at character 13 (probably needs to be 14, but I can deal with that later) and pick up 13 characters.  If @maxcounter is 2, start at character 26 (or maybe 27) and pick up 13 characters, and so on.

Can anybody please help me here?  I don't know where I'm going wrong.

Thanks
Sarah
0
Comment
Question by:ScuzzyJo
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
ID: 41801284
Please try this

--

SET @maxcounter = 0;
--
WHILE @maxcounter < @maxcnt 
BEGIN
      SET @crtcnt = @maxcounter + 1;
      SET @maxcounter = @maxcounter + 1;
      SET @sqlstr = 'UPDATE LFRD SET old_ssn' + @crtcnt + ' = substring( Concat(F,13) *' + @maxcounter + ',13) WHERE old_ssn Is Not Null OR @maxcounter < SSNCount;'
      EXEC (@sqlstr);
      
END

--

Open in new window

0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 400 total points
ID: 41801295
@crtcnt and @maxcounter are both INT and you want to concatenate with a string and that's why you're having the error.
Use CAST function to explicitly convert it to varchar.
Also, inside the dynamic SQL your variables aren't known so you need to do the same for the WHERE clause:

SET @sqlstr = 'UPDATE LFRD SET  old_ssn' + CAST(@crtcnt AS VARCHAR) + ' = substring(ConcatF,13*' + CAST(@maxcounter AS VARCHAR)+ ',13) WHERE old_ssn Is Not Null OR ' + CAST(@maxcounter AS VARCHAR) + '< SSNCount;'
0
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 100 total points
ID: 41801301
Try this ..

--

--

SET @maxcounter = 0;
--
WHILE @maxcounter < @maxcnt 
BEGIN
      SET @crtcnt = @maxcounter + 1;
      SET @maxcounter = @maxcounter + 1;
      SET @sqlstr = 'UPDATE LFRD SET old_ssn' + CAST(@crtcnt AS VARCHAR(50)) + ' = substring(ConcatF,13*' + CAST(@maxcounter AS VARCHAR(50)) + ',13) WHERE old_ssn Is Not Null OR @maxcounter < SSNCount'
      EXEC (@sqlstr);
      
END

--

Open in new window


--
0
 

Author Comment

by:ScuzzyJo
ID: 41801347
Hi

Vitor - that works perfectly, thank you.  I'm going to award you the bulk of the points.

Pawan - yours was nearly there, so I'm going to award you some points.

Hope that's OK with both of you.

Thanks
Sarah
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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

19 Experts available now in Live!

Get 1:1 Help Now