sql server programming:
Hi, I am quite new to SQL Programming. I need to achieve the output for a query which is something like this:- "Select * from xyz where itemid in(2,3,4,5,6)" I need to create a stored procedure. I need to declare a variable in the stored procedure and store all these values(2,3,4,5,6) and should be writing the query which is like this: "Select * from xyz where itemid like @x" where @x is a variable which stores all the values. How do i go about doing this?? i.e i need to store multiple values in a variable and refer to this variable in my query. In future if the business needs to add some other value then it can be just done in the beginning of the program by adding it to the variable values rather than going into the query. I am able to call if its a single value but how do i assign multiple values to a single variable?? I hope i was clear.Please help me. I would appreciate any help. Thanks,
Thanks a million.Great this is working for integer values,, How do i go about if the datatype is varchar!!. Could you please let me know!! Vamsi [quoted text, click to view] >-----Original Message----- >In your Stored Procedure: > >DECLARE @ITMX VARCHAR(100) >DECLARE @STRSQL VARCHAR(2000) >SET @ITMX = '2,3,4,5,6' > >SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMID IN (' + @ITMX + ')' >EXEC (@STRSQL) > > > >"Vamsi" <anonymous@discussions.microsoft.com> wrote in message >news:048501c3ace0$8fe4e3a0$a401280a@phx.gbl... >> Hi, >> >> I am quite new to SQL Programming. I need to achieve the >> output for a query which is something like this:- >> >> "Select * from xyz where itemid in(2,3,4,5,6)" >> >> I need to create a stored procedure. >> I need to declare a variable in the stored procedure and >> store all these values(2,3,4,5,6) and should be writing >> the query which is like this: >> "Select * from xyz where itemid like @x" >> >> where @x is a variable which stores all the values. >> >> How do i go about doing this?? i.e i need to store >> multiple values in a variable and refer to this variable >> in my query. In future if the business needs to add some >> other value then it can be just done in the beginning of >> the program by adding it to the variable values rather >> than going into the query. >> >> I am able to call if its a single value but how do i >> assign multiple values to a single variable?? >> >> I hope i was clear.Please help me. I would appreciate any >> help. >> Thanks, >> Vamsi > > >.
Erland has written some good info regarding this... http://www.algonet.se/~sommar/arrays-in-sql.html -- -oj RAC v2.2 & QALite! http://www.rac4sql.net [quoted text, click to view] "Vamsi" <anonymous@discussions.microsoft.com> wrote in message news:064601c3ace9$c8c29a60$a101280a@phx.gbl... > Thanks a million.Great this is working for integer > values,, How do i go about if the datatype is varchar!!. > Could you please let me know!! > > Vamsi > > > >-----Original Message----- > >In your Stored Procedure: > > > >DECLARE @ITMX VARCHAR(100) > >DECLARE @STRSQL VARCHAR(2000) > >SET @ITMX = '2,3,4,5,6' > > > >SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMID IN (' + > @ITMX + ')' > >EXEC (@STRSQL) > > > > > > > >"Vamsi" <anonymous@discussions.microsoft.com> wrote in > message > >news:048501c3ace0$8fe4e3a0$a401280a@phx.gbl... > >> Hi, > >> > >> I am quite new to SQL Programming. I need to achieve > the > >> output for a query which is something like this:- > >> > >> "Select * from xyz where itemid in(2,3,4,5,6)" > >> > >> I need to create a stored procedure. > >> I need to declare a variable in the stored procedure > and > >> store all these values(2,3,4,5,6) and should be writing > >> the query which is like this: > >> "Select * from xyz where itemid like @x" > >> > >> where @x is a variable which stores all the values. > >> > >> How do i go about doing this?? i.e i need to store > >> multiple values in a variable and refer to this > variable > >> in my query. In future if the business needs to add > some > >> other value then it can be just done in the beginning > of > >> the program by adding it to the variable values rather > >> than going into the query. > >> > >> I am able to call if its a single value but how do i > >> assign multiple values to a single variable?? > >> > >> I hope i was clear.Please help me. I would appreciate > any > >> help. > >> Thanks, > >> Vamsi > > > > > >. > >
In your Stored Procedure: DECLARE @ITMX VARCHAR(100) DECLARE @STRSQL VARCHAR(2000) SET @ITMX = '2,3,4,5,6' SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMID IN (' + @ITMX + ')' EXEC (@STRSQL) [quoted text, click to view] "Vamsi" <anonymous@discussions.microsoft.com> wrote in message news:048501c3ace0$8fe4e3a0$a401280a@phx.gbl... > Hi, > > I am quite new to SQL Programming. I need to achieve the > output for a query which is something like this:- > > "Select * from xyz where itemid in(2,3,4,5,6)" > > I need to create a stored procedure. > I need to declare a variable in the stored procedure and > store all these values(2,3,4,5,6) and should be writing > the query which is like this: > "Select * from xyz where itemid like @x" > > where @x is a variable which stores all the values. > > How do i go about doing this?? i.e i need to store > multiple values in a variable and refer to this variable > in my query. In future if the business needs to add some > other value then it can be just done in the beginning of > the program by adding it to the variable values rather > than going into the query. > > I am able to call if its a single value but how do i > assign multiple values to a single variable?? > > I hope i was clear.Please help me. I would appreciate any > help. > Thanks, > Vamsi
DECLARE @STRSQL VARCHAR(2000) DECLARE @ITMX VARCHAR(100) SET @ITMX = '''TEXT2'',''TEXT5''' SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMNAME IN (' + @ITMX + ')' EXEC(@STRSQL) [quoted text, click to view] "Vamsi" <anonymous@discussions.microsoft.com> wrote in message news:064601c3ace9$c8c29a60$a101280a@phx.gbl... > Thanks a million.Great this is working for integer > values,, How do i go about if the datatype is varchar!!. > Could you please let me know!! > > Vamsi > > > >-----Original Message----- > >In your Stored Procedure: > > > >DECLARE @ITMX VARCHAR(100) > >DECLARE @STRSQL VARCHAR(2000) > >SET @ITMX = '2,3,4,5,6' > > > >SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMID IN (' + > @ITMX + ')' > >EXEC (@STRSQL) > > > > > > > >"Vamsi" <anonymous@discussions.microsoft.com> wrote in > message > >news:048501c3ace0$8fe4e3a0$a401280a@phx.gbl... > >> Hi, > >> > >> I am quite new to SQL Programming. I need to achieve > the > >> output for a query which is something like this:- > >> > >> "Select * from xyz where itemid in(2,3,4,5,6)" > >> > >> I need to create a stored procedure. > >> I need to declare a variable in the stored procedure > and > >> store all these values(2,3,4,5,6) and should be writing > >> the query which is like this: > >> "Select * from xyz where itemid like @x" > >> > >> where @x is a variable which stores all the values. > >> > >> How do i go about doing this?? i.e i need to store > >> multiple values in a variable and refer to this > variable > >> in my query. In future if the business needs to add > some > >> other value then it can be just done in the beginning > of > >> the program by adding it to the variable values rather > >> than going into the query. > >> > >> I am able to call if its a single value but how do i > >> assign multiple values to a single variable?? > >> > >> I hope i was clear.Please help me. I would appreciate > any > >> help. > >> Thanks, > >> Vamsi > > > > > >. > >
Hi Tanuja, Yes, got it.This is working with characters. This was great help.Actually i have referred to many sites and also sql help, but no where i am able to get the exact picture i wanted. It would have taken me years if not for your reply. Thanks a lot once again. Vamsi. [quoted text, click to view] >-----Original Message----- >DECLARE @STRSQL VARCHAR(2000) >DECLARE @ITMX VARCHAR(100) > > > >SET @ITMX = '''TEXT2'',''TEXT5''' > >SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMNAME IN (' + @ITMX + ')' >EXEC(@STRSQL) > > > >"Vamsi" <anonymous@discussions.microsoft.com> wrote in message >news:064601c3ace9$c8c29a60$a101280a@phx.gbl... >> Thanks a million.Great this is working for integer >> values,, How do i go about if the datatype is varchar!!. >> Could you please let me know!! >> >> Vamsi >> >> >> >-----Original Message----- >> >In your Stored Procedure: >> > >> >DECLARE @ITMX VARCHAR(100) >> >DECLARE @STRSQL VARCHAR(2000) >> >SET @ITMX = '2,3,4,5,6' >> > >> >SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMID IN (' + >> @ITMX + ')' >> >EXEC (@STRSQL) >> > >> > >> > >> >"Vamsi" <anonymous@discussions.microsoft.com> wrote in >> message >> >news:048501c3ace0$8fe4e3a0$a401280a@phx.gbl... >> >> Hi, >> >> >> >> I am quite new to SQL Programming. I need to achieve >> the >> >> output for a query which is something like this:- >> >> >> >> "Select * from xyz where itemid in(2,3,4,5,6)" >> >> >> >> I need to create a stored procedure. >> >> I need to declare a variable in the stored procedure >> and >> >> store all these values(2,3,4,5,6) and should be writing >> >> the query which is like this: >> >> "Select * from xyz where itemid like @x" >> >> >> >> where @x is a variable which stores all the values. >> >> >> >> How do i go about doing this?? i.e i need to store >> >> multiple values in a variable and refer to this >> variable >> >> in my query. In future if the business needs to add >> some >> >> other value then it can be just done in the beginning >> of >> >> the program by adding it to the variable values rather >> >> than going into the query. >> >> >> >> I am able to call if its a single value but how do i >> >> assign multiple values to a single variable?? >> >> >> >> I hope i was clear.Please help me. I would appreciate >> any >> >> help. >> >> Thanks, >> >> Vamsi >> > >> > >> >. >> > > > >.
Don't see what you're looking for? Try a search.
|