Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamic SQL Error

Posted on 2016-09-16
4
Medium Priority
?
90 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 35

Expert Comment

by:Pawan Kumar
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 52

Accepted Solution

by:
Vitor Montalvão earned 1600 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 35

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 400 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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, show how to extract information from SQL Server on Database, Connection and Server properties

877 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