Solved

Dynamic SQL Error

Posted on 2016-09-16
4
65 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 28

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 48

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 28

Assisted Solution

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

820 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