Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conversion failed when converting the varchar value to data type int.

Posted on 2009-04-01
4
Medium Priority
?
367 Views
Last Modified: 2012-05-06
I have a web application that defines the list of order numbers that I want to update in a DB. This list of order numbers (which is defined as int in the DB) is collected in a string. I would like to pass the value of this string to the sql parameter to update the DB, but when I run it I get the error message: "Conversion failed when converting the varchar value ''44','45'' to data type int."

This code below represents the situation:
declare @OrderSelection varchar(max)
set @OrderSelection = '''44'',''45'''
update SY1_OrderDetail set OrderLineStatus = 1
where orderNumber in (@OrderSelection)

Please, could you help me to solve the problem?
declare @OrderSelection varchar(max)
set @OrderSelection = '''44'',''45'''
update SY1_OrderDetail set OrderLineStatus = 1
where orderNumber in (@OrderSelection)

Open in new window

0
Comment
Question by:EdsSystem
[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 37

Expert Comment

by:momi_sabag
ID: 24040263
that won't work
is this a one time thing or do you need to run this in a  procedure>?
0
 

Author Comment

by:EdsSystem
ID: 24040294
I will use it in a procedure.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 24040316
You have to put it in dynamic SQL in order to pass @OrderSelection in like that.  Or, use a UDF to split the @OrderSelection into a table and join to it or us in/exists.

Here's the function I wrote to do it:
http://sqlservernation.com/blogs/howtos/archive/2009/03/07/converting-a-delimited-string-into-a-table.aspx



--with dynamic sql
declare @OrderSelection varchar(max)
set @OrderSelection = '44,45'
 
exec('update SY1_OrderDetail set OrderLineStatus = 1 where orderNumber in (' + @OrderSelection + ')')
 
 
--with function 1
declare @OrderSelection varchar(max)
set @OrderSelection = '44,45'
 
update SY1_OrderDetail set OrderLineStatus = 1 where orderNumber in
(select cast(theValue as int) from dbo.fn_delimitedToString(@OrderSelection,','))
 
--with function 2
declare @OrderSelection varchar(max)
set @OrderSelection = '44,45'
 
update sy 
set OrderLineStatus = 1 
from SY1_OrderDetail sy
where exists
(select 1 from dbo.fn_delimitedToString(@OrderSelection,',') where cast(theValue as int) = sy.OrderNumber)

Open in new window

0
 

Author Closing Comment

by:EdsSystem
ID: 31565353
Thanks a lot for your prompt response!!!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

719 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