473,407 Members | 2,306 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,407 software developers and data experts.

How to use comma separated value list in the where clause

i want to do something like this;
for-example;
Set vote = true for all rows in tbl_emp where pk_tbl is in the comma separated value list.

Expand|Select|Wrap|Line Numbers
  1. update tbl_emp 
  2. set vote=true where pk_tbl in @empl_vote_true
  3.  
Where, @empl_vote_true = ’12,23,345,’ (IDs of the employees),

Is it possible, if so how?
Jul 26 '12 #1
9 8822
Hi Yarsirzafar

The IN Clause within WHERE should be enclosed in Parentheses i.e IN(6,45,233);

Try that
Kind regards
Jul 26 '12 #2
Rabbit
12,516 Expert Mod 8TB
You can't do it that way. Not even if you put it in parentheses because the variable will always be a string. The IN clause will never see it as more than one value.

Unless you know the string will always contain the same number of items, you will have to build a dynamic string and then execute that.
Jul 26 '12 #3
ck9663
2,878 Expert 2GB
If you really need to, you can convert that string into a result set so you can use IN clause.

Here, read this...

Good Luck!!!


~~ CK
Jul 26 '12 #4
As an update on my previous answer I will make the following assumptions;

Column 'vote' is type yes/no; true/false
column 'pk_tbl' is type integer
@empl_vote_true = "12,23,345"

sql= "UPDATE tbl_emp SET " _
& "vote = true " _
& "WHERE pk_tbl IN('" & @empl_vote_true & "');

Make sure you have the single and double quotes correct
This performs OK in Access 2007

Kind regards
Jul 26 '12 #5
thanks for your answer, if that set is passed to a Stored procedure, via a variable whose datatype is string in c#, and varchar(8000) in sql server

Expand|Select|Wrap|Line Numbers
  1. string empl_vote_true = ",12,13,14,15,16"
Then at this stage,

Expand|Select|Wrap|Line Numbers
  1. sql= "UPDATE tbl_emp SET " _
  2. & "vote = true " _
  3. & "WHERE pk_tbl IN('" & @empl_vote_true & "')
will it work fine? or something should be added or parsed before. would i need a loop to update for each where clause, thanks
Jul 26 '12 #6
I don't do c#, but it should work in MS SQLserver
Jul 26 '12 #7
Hi Yasirzafar

When I looked at your code again the line

string empl_vote_true = ",12,13,14,15,16"

You should not have an opening comma without data it will throw an error.

Kind regards
Jul 27 '12 #8
you mean, the string should be in the form of "12,13,14,15,16";
(for clarification). And it will work?
Jul 28 '12 #9
The only way is to try it.

Kind regards
Jul 29 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Craig Keightley | last post by:
I can do the match perfectly but what i also need to do is create a third list of comma separated values that are in both eg: List 1 => 1,2,3,4,5,6,7,8,11 List 2 => 1,3,4,5,6,7,10,23 ...
5
by: Bob | last post by:
I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first...
3
by: starman7 | last post by:
I'm attempting a query that gathers product data for a particular product id. One of the items is designer(s) which can be more than one. The product table has comma separated id's of the...
3
by: mahe23 | last post by:
All, How do One convert a comma separated column from a text file into rows in oracle. I have a scenario where the list of comma separated values changes dynamically. It is like: abc, ttt,...
10
by: Nishanth | last post by:
I want to know how to validate a string which is comma separated "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate each field value against a set of rules, for...
0
MMcCarthy
by: MMcCarthy | last post by:
This is a module that imports information to a Query or a Table from comma separated values file in a text format! It is very helpful for getting information from other applications and from files! ...
4
hirak1984
by: hirak1984 | last post by:
Dear Sir/mam, I am new comers for DB2 .I Write a Stored Procedure below in line CREATE PROCEDURE AUS.PROCEDURE1 ( IN var0 VarCHAR(4000), OUT var1 CHAR(6) ) DYNAMIC RESULT SETS 1...
1
by: alister joseph | last post by:
hi I have two tables, i have to combine them & a column should contain comma separated value. example 1 a 2 b 2 c 2 d 3 a
7
by: Hermann | last post by:
I run a server with apache 1.3 and php 5. Yesterday I notice that sometimes the HTTP_HOST server variable has a comma separated list in it. Let's say my domain name is: www.mydomain.com Usually...
1
by: rythmik1 | last post by:
I need to create an alphabetized list from multiple rows that contain comma separated tags. Example: Row1 -> Lessons, Discussions, Help Row2 -> Discussions, Chat Row3 -> Surveys,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.