Solved

SQL Exec Insert Integer Error?

Posted on 2014-01-23
8
320 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
ID: 39805338
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
ID: 39805349
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
ID: 39805351
It worked, but why?
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 400 total points
ID: 39805359
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
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.

 

Author Comment

by:WorknHardr
ID: 39805365
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
ID: 39805369
>>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
ID: 39805375
No not embedded that's something altogether different, its just called dynamic sql execution.
0
 

Author Closing Comment

by:WorknHardr
ID: 39806422
Thx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

912 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

16 Experts available now in Live!

Get 1:1 Help Now