Solved

Dynamic SQL Error

Posted on 2016-09-16
4
73 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 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 50

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove () 10 41
Access PS SQLSERVER from powershell 1 30
Restore a log backup compressed 3 16
first parameter x of the decimal (x, y) 1 21
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

739 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