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

x
?
Solved

SQL Exec Insert Integer Error?

Posted on 2014-01-23
8
Medium Priority
?
335 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
[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
  • 4
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 1600 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 1600 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
 

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 1600 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

721 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