Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 17th, 2004, 10:16 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Data Type Conversion using WHERE IN ()

I am getting a "Syntax error converting the varchar value '10,90' to a column of data type int." error when I run the following procedure:

@myList varchar(200)

Code:
SELECT column1
FROM table1
WHERE table1.ID IN (@myList)


When @myList is a single value, I get no errors. However, when @myList is a comma separated list like in the message above, I error out. I am using SQL Server 2000.

How else can I build this list of IDs? Thank you in advance for your comments.

--Colonel

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old August 17th, 2004, 10:23 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CREATE PROCEDURE dbo.letterStatus
     @award as tinyint
    ,@members as varchar(500)
AS
 EXEC('SELECT * FROM awardHistory
    WHERE membID IN (' + @members + ') '
    + @letterClause +
    ' AND awardTypeFK = ' + @award)

Where members could = '1,2,3,4'. this works for me

Regards
Ian

 
Old August 17th, 2004, 02:02 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was able to find the solution. What was going on was that SQL Server adds single quotes around the varchar parameter. I got around this by using the REPLACE function in the WHERE clause. My WHERE clause now looks like this:

WHERE table1.ID IN (REPLACE(@myList,'''',NULL))

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old August 17th, 2004, 02:03 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ian-

I asked around, and of course your solution works, but I would lose some of the benefits of a stored procedure by using dynamic SQL. Specifically, the compiled execution plan.

Thank you for your comments.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old August 18th, 2004, 02:11 AM
Authorized User
 
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, I agree the REPLACE method looks more efficient

 
Old August 18th, 2004, 05:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by Colonel Angus
 I was able to find the solution. What was going on was that SQL Server adds single quotes around the varchar parameter. I got around this by using the REPLACE function in the WHERE clause. My WHERE clause now looks like this:

WHERE table1.ID IN (REPLACE(@myList,'''',NULL))

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
And, according to your sig, you actually did test this approach, and it works?

Really?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old August 18th, 2004, 08:33 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ya know...

I wrote that reply because I thought I ran that and it worked. It must have been another procedure. My apologies - my solution did not work, I thought it had but I was mistaken.

Dynamic is the way to go.

However, it does strike me as odd that SQL Server cannot handle this better. I would think that this scenario presents itself often. In a former life I worked in a ColdFusion environment and it was able to handle this more elegantly (it probably converted to a dynamic SQL behind the scenes).

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old August 18th, 2004, 09:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Cold Fusion.. ELegant? Not..

You could do the same thing in ASP.NET as cold fusion- Create the SQL statement in the code and then pass it to the SQL Server (Using CFQUERY or ADO.NET to connect changes nothing).. However, NEITHER solution lets you pre-compile and get the benefit of that from SQL Server.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old August 18th, 2004, 09:17 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's why I was hoping to find a way around using dynamic SQL; I still can reduce some network traffic by implementing as a stored procedure, but as you said I cannot get a compiled execution plan. bummer.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old August 18th, 2004, 09:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Is there a limit to the number of entries in @mylist?


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee





Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from type 'DBNull' to type 'String' is GailCG ASP.NET 2.0 Basics 5 February 22nd, 2007 03:12 PM
Help with data type conversion please androoo Pro VB 6 2 December 24th, 2004 05:45 PM
Help with data type conversion please androoo Pro VB.NET 2002/2003 0 November 29th, 2004 09:06 AM
data type conversion Adam H-W SQL Server 2000 2 March 2nd, 2004 08:38 AM
Data Type Conversion owain SQL Language 5 October 31st, 2003 12:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.