473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SQL Parameterized Queries with IN statement

Hi,

When trying the following code :

[...]
oCmd = "SELECT MyField FROM MyTable WHERE MyID IN (@IDList)"
MyCommand = New SqlCommand(oCmd, oConn)
MyCommand.Parameters.Add(New SqlParameter("@IDList",
SqlDbType.NVarChar))
MyCommand.Parameters("@IDList").Value = "5611,1332"
[...]

I've got the following error :
Syntax error converting the nvarchar value '5611,1332' to a column of
data type int

MyID type is SqlDbType.Int
@IDList is a comma separated int values list (=> String). Is this type
correct or should I use another one ? Is there a particular syntax to
assign a parameter in that kind of query ?

Thanks
Fred
Nov 18 '05 #1
2 1145
To SQL Server, the variable is one value. Not a list of values.

See http://www.sommarskog.se/arrays-in-sql.html
Good Luck!

"Fred" <fl******@yahoo.fr> wrote in message
news:fd**************************@posting.google.c om...
Hi,

When trying the following code :

[...]
oCmd = "SELECT MyField FROM MyTable WHERE MyID IN (@IDList)"
MyCommand = New SqlCommand(oCmd, oConn)
MyCommand.Parameters.Add(New SqlParameter("@IDList",
SqlDbType.NVarChar))
MyCommand.Parameters("@IDList").Value = "5611,1332"
[...]

I've got the following error :
Syntax error converting the nvarchar value '5611,1332' to a column of
data type int

MyID type is SqlDbType.Int
@IDList is a comma separated int values list (=> String). Is this type
correct or should I use another one ? Is there a particular syntax to
assign a parameter in that kind of query ?

Thanks
Fred

Nov 18 '05 #2
Thank you for the link, it was really usefull.

Fred
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
8
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf =...
2
by: deko | last post by:
Is it possible to build a parameterized query from another parameterized query? I've tried two variations of this and can't seem to get it to work (using DAO). Any suggestions welcome! I...
1
by: Jorell | last post by:
Hey everyone, I am currently using Microsofts DataAccess Application block ( SQLHelper ) and what I want to do is use a parameterized query instead of just SQL. I can not use stored procedures,...
11
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These...
2
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted...
7
by: Mark | last post by:
Hello. I am developing an Access 2000 form, and I want to populate a databound list box using a saved query that requires parameters. Is there any way to do this? I tried creating a DAO querydef...
0
by: tlyczko | last post by:
When I am working on the tutorials about data access from www.asp.net, every time I try to use the parameterized queries, and I type something like @CategoryID, it is changed to ? in the query...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.