Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic SQL Error

Posted on 2016-09-16
4
Medium Priority
?
85 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
[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
  • 2
4 Comments
 
LVL 30

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 30

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

722 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