Solved

SQL Exec Insert Integer Error?

Posted on 2014-01-23
8
317 Views
Last Modified: 2014-01-24
I receive the following error trying to insert a known Int variable like so:

   Error: " Conversion failed when converting the varchar value 'Insert Into MyTableTest([ListID] Values(' to data type int.  "

Q1. What is the proper name for using Exec and variable as below, 'Embedded SQL?

Q2. Why is this Insert failing when I know 'MyTableTest' and 'ListID' is type Int?

Note: I think all the single and double-quotes are correct...

//calling code
exec sp_MyTableTest 1469

//SP
ALTER PROCEDURE [dbo].[SP_MyTableTest]
(
	@ListID		int
)

AS

BEGIN
     Declare @SQL varchar(max) = 'Insert Into MyTableTest( [ListID] )' + ' Values(' + @ListID + ')';
     Exec(@SQL);
END

Open in new window

0
Comment
Question by:WorknHardr
  • 4
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 400 total points
Comment Utility
try :

Declare @SQL varchar(max) ;
Set @SQL = 'Insert Into MyTableTest( [ListID] )' + ' Values(' + CAST(@ListID AS VARCHAR) + ')';
     Exec(@SQL);
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Or simply lose the Dynamic SQL altogether (and the corresponding security risks involved) and do it like this:
//calling code
exec sp_MyTableTest 1469

//SP
ALTER PROCEDURE [dbo].[SP_MyTableTest]
(
	@ListID		int
)

AS

BEGIN
     Insert Into MyTableTest([ListID]) Values(@ListID)

END

Open in new window

0
 

Author Comment

by:WorknHardr
Comment Utility
It worked, but why?
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 400 total points
Comment Utility
When concatenating strings in tsql they have to be of the same type so you need to explicitly convert any integers to a varchar otherwise it tries to add them together.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:WorknHardr
Comment Utility
Wow, even though it's Inserted into an INT type column, I would have never guessed...

Q1. What is the proper name for using Exec and variable, 'Embedded SQL?
0
 

Author Comment

by:WorknHardr
Comment Utility
>>Anthony Perkins

Thanks for your input and opinion. I have excluded other columns which make my crazy coding necessary. I have a very unusual project requirement, thanks again...
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 400 total points
Comment Utility
No not embedded that's something altogether different, its just called dynamic sql execution.
0
 

Author Closing Comment

by:WorknHardr
Comment Utility
Thx
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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

12 Experts available now in Live!

Get 1:1 Help Now