SQL IN

Hi all,

Using ASP.NET, SQL SVR Express 2008

I want to use IN to select records that have one of a list of values like 
this:

SELECT * FROM MyTable WHERE MyField IN ("Value1","Value2","Value3")

MyField is a nvarchar(MAX)

My query has a palceholder for the parameter, so it looks like this:

SELECT * FROM MyTable WHERE MyField IN (@MyList)

I create my command object ans set the parameter:

myCommand.Parameters.AddWithValue("MyList", myList)

where myList = "Value1,Value2,Value3"

I get no values returned even though there are matches.

If I were to build this by hand I would surround each ValueN in quotes 
(like I did above), but since I am using the AddValueWith I thought I did 
not need delimiters, but if I do it by hand like that I get an error

Invalid column name near Value1

How can I make this work?

Thanks,
kpg
0
kpg
9/10/2010 8:03:05 PM
sqlserver.server 1327 articles. 0 followers. Follow

3 Replies
858 Views

Similar Articles

[PageSpeed] 3

kpg <ipost@thereforeiam> wrote in
news:Xns9DEF9926AD454ipostthereforeiam@207.46.248.16: 

OK...I got the 'by hand' part to work by delimiting the values in single 
quote, not double quotes.

I'm usre I'v used double quotes in the past...oh well.

My questions still reamins, however.

I want to use the @MyList parameter and set the parmaeter in code rahter
than build the query by hand.

thanks,
0
kpg
9/10/2010 8:16:13 PM
kpg (ipost@thereforeiam) writes:
> Using ASP.NET, SQL SVR Express 2008
> 
> I want to use IN to select records that have one of a list of values like 
> this:
> 
> SELECT * FROM MyTable WHERE MyField IN ("Value1","Value2","Value3")
> 
> MyField is a nvarchar(MAX)
> 
> My query has a palceholder for the parameter, so it looks like this:
> 
> SELECT * FROM MyTable WHERE MyField IN (@MyList)
> 
> I create my command object ans set the parameter:
> 
> myCommand.Parameters.AddWithValue("MyList", myList)
> 
> where myList = "Value1,Value2,Value3"
> 
> I get no values returned even though there are matches.

It you have a row with the value "Value1,Value2,Value3", there is a 
match, else there isn't. 

There is article on my web site that describes the way to go:
http://www.sommarskog.se/arrays-in-sql-2008.html
-- 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

0
Erland
9/10/2010 8:36:02 PM
On Fri, 10 Sep 2010 13:03:05 -0700, kpg <ipost@thereforeiam> wrote:

>Hi all,
>
>Using ASP.NET, SQL SVR Express 2008
>
>I want to use IN to select records that have one of a list of values like 
>this:
>
>SELECT * FROM MyTable WHERE MyField IN ("Value1","Value2","Value3")
>
>MyField is a nvarchar(MAX)
>
>My query has a palceholder for the parameter, so it looks like this:
>
>SELECT * FROM MyTable WHERE MyField IN (@MyList)
>
>I create my command object ans set the parameter:
>
>myCommand.Parameters.AddWithValue("MyList", myList)
>
>where myList = "Value1,Value2,Value3"
>
>I get no values returned even though there are matches.
>
>If I were to build this by hand I would surround each ValueN in quotes 
>(like I did above), but since I am using the AddValueWith I thought I did 
>not need delimiters, but if I do it by hand like that I get an error
>
>Invalid column name near Value1
>
>How can I make this work?
>
>Thanks,
>kpg


Hi 

You could look at table valued parameters or dynamic SQL
as two possible methods for more information see
http://www.sommarskog.se/arrays-in-sql-2008.html

John
0
John
9/10/2010 8:46:23 PM
Reply:

Similar Artilces:

Calling Dex Procedures from SQL
Is there any way to call dex procs from SQL or VB code? Jed There is an undocumented and unsupported method of calling Dex code from VBA. However, I know of no method of calling Dexterity from SQL. Please email me if you want an example, just remove the online. from the address below. David Musgrave [MSFT] Senior Development Consultant MBS Services - Asia Pacific Microsoft Business Solutions http://www.microsoft.com/BusinessSolutions mailto:dmusgrav@online.microsoft.com Any views contained within are my personal views and not necessarily Microsoft Business Solutions policy. This p...

is SQL 2000 architecture different SQL Server 2005 / 2008 architecture?
Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I have been told is a good book. I am going backwards... reading about the sql server architecture in chapter 3 of the book. As I'm reading... I was wondering is it different SQL Server 2000 vs SQL Server 2005/2008? Also where can I find a book/or link that can give me more details about the internals on architecture? Thank you norm (normanchan@gmail.com) writes: > Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I > have been told is a good book. I am g...

Moving customizations from SQL 2k to SQL 2k5 CRM
This may be a non-issue but I wanted to check to see if anyone has done this and ran into issues. I have the XML customization files from our previous install of CRM 3.0 with SQL 2k database. I now want to import and publish these same customizations to CRM 3.0 on SQL 2k5. Has anyone done this or can point me to documentation that tells about the success or issues for doing this? Thanks. On 7 Feb., 06:35, Chris Treanor <ChrisTrea...@discussions.microsoft.com> wrote: > This may be a non-issue but I wanted to check to see if anyone has done this > and ran into issues. > &...

CRM 3.0 Install Error with SQL Reporting Services and SQL 2005
Hi, Doing a CRM 3.0 Server installation. During the last page of the install wizard (or what I assume is the last page) when I goes through and does verification I get a red X next to SQL Reporting Services. The message I receive when I look at the details is: ***** The specified path is not a metabase path. Parameter name: path ***** Now SRS is installed and working. I put in the URL: http://<servername>/reportserver and every other variation on that I can find. Thoughts? Alan try http://servername/reports also assuming you did not use ssl... =======================...

SQL 'for xml' and C# example??
I'm missing some piece of the puzzle. I'm using 'for xml' type queries from SQL2K - and now trying to change the front-end from ADO to C#, I can't quite figure it out.. It seems that I could use an XmlReader.. and somehow use that instead of a DataReader.. but that didn't work.. I tried using a DataReader and get the byte array - but that wasn't right. SQL returns formatted XML, I need to load that XML into an XmlDocument somehow without ANY molestation of that dom. Any ideas? Examples? Missing links???? thanks THANKS!! I've been looking for the answer to ...

sql summarizing help
Hi All, Given: table1 --------- id, cdate, weeksago, amount, typeid, timeclose 496, 2010-02-11, 0, 450, 3, 1 490, 2010-02-01, 1, 200, 4, 1 491, 2010-02-01, 1, 350, 2, 2 493, 2010-02-01, 1, 500, 5, 1 489, 2010-01-21, 3, 150, 2, 1 136, 2010-01-12, 4, 500, 3, 2 137, 2010-01-12, 4, 100, 3, 1 138, 2010-01-12, 4, 500, 2, 1 1. i'm trying to create a sql that will give me a summary resultset grouped by weeksago. 2. i want to sum the columns and get counts based on typeid. 3. avg of timeclose here's the sql i have: select weeksago, , (sum(timeClose)/(count(timeclo...

Update multivalue field in table from form with SQL
Access 2007 on Vista I am using a form to establish the variables for a report before it is created. One field on the underlying table is a multivalue field, and the form includes a multi-select combo box control. After selections are made and the OK button on the multi-select combo drop down is clicked, focus moves to the next control. I have discovered though, that the underlying table does not update immediately, but does so correctly when I close the form. This sequence does not deliver the proper report data, so I need a way to immediately update the underlying table (m...

Refer to SQL statements globally
I have two large SQL statements that I need to use across several forms, reports, etc. Rather than repeat them in each form, I'm trying to store and reference them globally. For example, I'd like to store strSQL1 and strSQL2 in a global module, so I can use either like this in a form: Random form: Me!lstResults.RowSource = strSQL1 Random report: Me.Report.RecordSource = strSQL2 Would I just store the SQL statements in a global module like: Public Function SQLSource() As String Dim strSQL1 As String Dim strSQL2 As String strSQL1 = "SELECT blah blah...

To SQL or not to SQL?
I have a very vertical product developed 100% in M/S Access. I developed it myself in 1993 in the initial release of Access and been with it ever since. It is currently very stable and running in Access 2003. My big question is how or even *if* we should move it to SQL. My customers are getting bigger all the time with more users. Typically our customers run one or two workstations generally never more than 5. Performance, however, is starting to slow with database sizes passing 500 meg. We also have a LOT of code behind forms so are bound tightly to the Access development environ...

PostgreSQL or other SQL servers in RMS
Has anyone successfully implemented RMS with one of the free SQL servers? Theoretically, is there any reason why we have to use MS SQL Server 2000 for large databases? David "dh" <dh@discussions.microsoft.com> ha scritto nel messaggio news:1783A90D-AF3E-4D91-A445-0039D4E7E379@microsoft.com... > Has anyone successfully implemented RMS with one of the free SQL servers? > Theoretically, is there any reason why we have to use MS SQL Server 2000 > for > large databases? RMS has implemented only for MS SQL Server (2000/MSDE) and isn't possible to work with ano...

writeback to SQL 2005
hello! I use excel 2007 iI open a connection agianst SQL server 2005 and choose a table. In my workbook i can now see this data from this table. if i change a value in some cell i wish to writeback to table in SQL server. Is this possible? It's possible through the use of VBA and ADO, but not simply using the data connection you've already established. Ross "CJ" <CJ@discussions.microsoft.com> wrote in message news:D54EC521-0DD5-4B3D-971B-4FF3394D6FA8@microsoft.com... > hello! I use excel 2007 > iI open a connection agianst SQL server 2005 and choose a table...

sql.request #2
Does anyone have a comprehensible guide on how to use it that a dolt like me can follow? Every time I try I get a #N/A. The only guide I can find is at http://www.bygsoftware.com/examples/zipfiles/UsingSqlRequest.zip but it's really hard to follow. From the MSDN Library: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/office97/html/SEC41.asp Not sure how familiar you are with ODBC; but if you are getting #N/A as a result, it is usually because of problems either with the SQL syntax or the connection string. There are a few examples of connection strings in the re...

MSDE or SQL Personal or SQL Developer? or SQL Workstation?
Hi, which one is better for personal non-server computer? MSDE? SQL Personal? SQL Developer? SQL Workstation? Thank you. It depends on what edition of GP you are going to run. If you are going to use Professional, you need to go with SQL Server developer, assuming you are installing on a non-server OS. "ano" wrote: > Hi, which one is better for personal non-server computer? MSDE? SQL Personal? > SQL Developer? SQL Workstation? > Thank you. Is the function of SQL Developer the same as Enterprise edition? How about personal edition? Can it be used for GP Pro? Can dev...

Excel RoundUp in Transact-SQL
Last week I posted the following computation in Excel... roundup(roundup(rate * 500),4) - roundup(rate * 200,4) + polfee,2) ((6.108934 * (500000/1000)) - (200 * 6.108934) + 1311.51) = 3144.20 In SQL using round(num,4) = 3144.19 I've moded this to... round(num+0.0005,4) = 3144.20 This works most of the time but occasionally I'm still off from what Excel returns. I'm under 2% in my 342 calcs, so it's do-able and we'll explain if any client notes this that we've reduced their pmt by .01 cents. BUT, I'd still like a way to replicate Excel'...

Access frontend with SQL Server backend: my solution for ODBC Call error
Hi This thread follows threads I started in forums microsoft.public.access.forms, and microsoft.sqlserver.programming: 1. 'Overriding constraint violation message' at 21.04.2010 12:00; 2. 'Overriding constraint restriction message, when deleting record(s) from form' at 29.04.2010 10:36. The problem (Occurs in Access 2000 mdb as stated by MS and in Access 2007 mdb as I myself discovered): When record operation is aborted from SQL Server, Access gets the full information about error, but this info gets unaccessible at moment when OnError event is triggered. The...

SQL
Hi, i am new in Sql express i am trying to create a database in sql but i dont now how can i install the database in other computer or a server programmatically where the other computer have the sql and if is a server what i nead to do . or only what i need is a valid conection ; i use the localhots to identify any clinte machine but i dont now what i need to writh for the servers my conection is look like this as a defualt from MFC hr = _db.OpenFromInitializationString(L"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data.mdf;Data Sour...

GP 9.0 on SQL 2008
MBS has said GP 10 is supported on SQL Server 2008. However there is no information on support for GP 9.0 on SQL 2008. Does anyone have any real-world experience running GP 9.0 on SQL 2008 in a production environment? Thanks, John Dynamics GP 9.0 is not supported on Microsoft SQL Server 2008. This is an excerpt from the System Requirements page. Database Requirements: MSDE 2000 SP 4 Microsoft SQL Server 2000 Enterprise Edition SP 4 Microsoft SQL Server 2000 Standard Edition SP 4 Microsoft SQL Server 2000 Workgroup Edition SP 4 Microsoft SQL Server 2000 (64-bit) SP 4 Microsoft SQL Serve...

SQL Study Group
sql-study-group@googlegroups.com Please join for support our SQL Study Group ------=_NextPart_0001_3CECC08E Content-Type: text/plain Content-Transfer-Encoding: 7bit For those who want to join, the URL is http://groups.google.com/group/sql-study-group David Musgrave [MSFT] Escalation Engineer - Microsoft Dynamics GP Microsoft Dynamics Support - Asia Pacific Microsoft Dynamics (formerly Microsoft Business Solutions) http://www.microsoft.com/Dynamics mailto:David.Musgrave@online.microsoft.com http://blogs.msdn.com/DevelopingForDynamicsGP Any views contained within are my personal views a...

SQL 2008 and SRS
I am using SQL 2005 with CRM 4.0 on one box and have to move CRM and SQL to seperate boxes. The SQL box will have SQL 2008 Standard. The consultant who will be doing the SQL work reminded me to install SRS. Is SRS included with SQL 2008 Standard? Thanks, John SQL Server Reporting Services (SSRS) is part of 2008 Standard Edition. RLF "John" <bsmith@aol.com> wrote in message news:%23DpI1bfcKHA.808@TK2MSFTNGP02.phx.gbl... >I am using SQL 2005 with CRM 4.0 on one box and have to move CRM and SQL to >seperate boxes. The SQL box will have SQL 2...

IM with SQL 2000 to SQL 2005
Can IM be used to take data from an SQL 2000 database for use with GP 8.0 running on SQL 2005. Basilcy I need to extract order information from a website running on SQL 2000 and insert in in GP 8.0 Possible? Thanks Terry Yes, you can use SQL Server 2000 databases as a data source for Integration Manager with GP 8 running on SQL 2000/2005. -- Charles Allen, MVP "TerryB" wrote: > Can IM be used to take data from an SQL 2000 database for use with GP 8.0 > running on SQL 2005. > > Basilcy I need to extract order information from a website running on SQL > 2...

T-SQL to PL-SQL
Hello, can sombody help me to translate that script in oracle? DECLARE @ID INT DECLARE CURSEURS_JOBS CURSOR FOR SELECT REF_RE_JOB_POSTING_ID FROM DM_RE_JOB_POSTING OPEN CURSEURS_JOBS FETCH CURSEURS_JOBS INTO @ID WHILE @@FETCH_STATUS = 0 BEGIN update DM_RE_JOB_POSTING set publishedI = (select CASE WHEN rjpd.internal = 1 and (rjp.internal_external in ('B','I')) THEN ( CASE WHEN rjpd.start_date is null and rjpd.end_date is not null and rjpd.end_date >= getdate() THEN ( SELECT 'I' ) WHEN rjpd.start_date is...

debug sql, Watch, Command Window, see cotent
Hi, I know how to get into debug mode, place break point etc in a stroed proc in SQL Server Management Studio. But after it hits a break print, what actions I can take to peek the data? e.g. I try to see a temp table has just been populated? I can not put "select top 1 col1 from #tempTable1" in watch list. It says it could not be evalulated I can not execute it in Command window. How can I look at data? How can I execute addintion sql statements? What command supported in "Command Window"? Thanks, The easiest way is put a SELECT statement in you...

Use the OBDC that GP10 creates when in SQL 2005
Some time back, when we first installed GP10 in our SQL2000, we were recommeded NOT to use the ODBC that GP10 created. We just upgraded to SQL2005 and fully installed all SPs. We also made clean install of GP10 under this engine. Should we the ODBC created by GP, or still use the use the same configuration of ODBC SQL2000? Is ther a link where we can click to and see step by step to create a proper ODBC for GP10? ...

upgrading from SQL 2000 to SQL 2005 with a clean install of CRM 3.
I just finished installing SBS 2003 and SQL 2000 along with CRM 3.0 no data or customization has been done in CRM. Then I received the SBS 2003 R2 disk, which has what I originally want SQL 2005. So now I have 2 instances of SQL running and CRM is attached to SQL 2000. My ? is how do I get CRM to recognize SQL 2005. Can I uninstall CRM, then uninstall SQL 2000, then reinstall CRM? What can I do. Alan Alberani Ticket Angels Traffic Ticket Defense 954-682-6020 Hi Alan, Did you install Windows SBS 2003 R2 in a different server or you did an upgrade to the existing server and have SQL ...

Connection to Sql-7 Database Fails
Hello All, Here is my problem. I have a Windows SBS 2003 Server with around 7 XP Clients. Since i have a SQL2000 database on the server, i installed a SQL7 database on one of the client machine which runs on XP. Now when my program access the same from database from the same machine it connects Now i tried to access the same database from a different XP machine , i am getting time out. Now if i give the user Administrative rights on my SBS server, i am able to connect to the machine. The problem seems with some security or rights issuse. How can my users connect to the dat...