Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to pass a variable into In operator

Author  Topic 

mrleokarthik
Starting Member

16 Posts

Posted - 2006-07-19 : 10:21:29
How can i execute this query thru Stored procedure ?

delete from ts_imported where ts_imported.c_change_symbol in ('A','B')

Symbols ('A','B') varies
How to pass this in paramter


I tried this

declare @Symbol varchar(20)
declare @apos char(1)
set @apos=''''

set @Symbol = @apos +'A' + @apos + ',' + @apos + 'B' + @apos
delete from ts_imported where c_change_symbol in (@Symbol)

Doesn't produce correct output!

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-07-19 : 10:35:23
use dynamic sql as below:

declare @Symbol varchar(20)
Declare @Sql varchar(8000)
declare @apos char(1)
set @apos=''''

set @Symbol = @apos +'A' + @apos + ',' + @apos + 'B' + @apos

set @Sql = 'Delete from ts_imported where c_change_symbol in (' + @symbol + ')'

Execute(@Sql)



Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-07-19 : 10:39:43
[code]
Create Procedure MyProc (@a as varchar(10), @b varchar(10))
as

Delete from ts_imported where
ts_imported.c_change_symbol = @a or
ts_imported.c_change_symbol = @b
[/code]

Call it as
Exec myproc 'A','B'


Srinika
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-07-19 : 11:35:02
Srinika's suggestion is good if you have a fixed number of variables (2, in this case).

Otherwise, refer to these articles...

http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm
http://www.sommarskog.se/arrays-in-sql.html

Note that neither article recommends using dynamic sql.



Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-07-19 : 12:07:01
we have a nice article here as well:

http://www.sqlteam.com/item.asp?ItemID=11499

- Jeff
Go to Top of Page

mrleokarthik
Starting Member

16 Posts

Posted - 2006-07-20 : 03:07:32
quote:
Originally posted by harsh_athalye

use dynamic sql as below:

declare @Symbol varchar(20)
Declare @Sql varchar(8000)
declare @apos char(1)
set @apos=''''

set @Symbol = @apos +'A' + @apos + ',' + @apos + 'B' + @apos

set @Sql = 'Delete from ts_imported where c_change_symbol in (' + @symbol + ')'

Execute(@Sql)



Harsh Athalye
India.
"Nothing is Impossible"




Dynamic SQL Works well.
I appriciate our forum is very quick with relevant replies.

Thanks to you everybody.

Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-07-20 : 05:03:46
quote:
Dynamic SQL Works well.
You should note what the links I posted had to say about dynamic SQL...

quote:
The good ones:

* The iterative method. Looping through a comma-separated list, and returning the elements in a table, either with a used-defined function (UDF) or a stored procedure. Decent performance, and easy to understand and easily extensible.

* Using a table of numbers to unpack a comma-separated list, either in a UDF or an SP. The fastest of all methods for a list with delimiters.

* Fixed-length array. Rather than using a comma-separated list, use a string where all elements have the same length. You unpack the string with a table of numbers in a UDF or SP. This is the fastest method I know of.

* XML. Overkill for a comma-separated list, but marvellous for an array of structured data. Only possible on SQL 2000. Decent performance, but slower than the iterative method.

The ones to stay away from:

* Dynamic SQL. For a list of numbers, it may appear simpler than any other method, but there are permissions issues to consider. And messier to use for a list of strings. Performance is unacceptable for long input. It is probably the only viable method on SQL 6.5 though.

* Making the List Into a SELECT. Transform a delimited list into an INSERT statement. Or many INSERT statements. Cute, but cannot handle input longer than ~3-5000 chars, and has no other particular benefit.

* Really slow methods. Methods that uses charindex, patindex or LIKE. These solutions are just unbelievably slow even for short input.


quote:
Dynamic SQL has its limitations, and is not something I would recommend. For starters, notice the "GRANT EXEC" command in the above script. That statement grants EXECUTE permission to the user WebUser. But that is not enough for WebUser to execute this stored procedure. The user executing dynamic SQL commands needs explicit permissions on the underlying tables, which is not something I would do on a production system. Because of this limitation, I added a "GRANT SELECT" command in the above script, to enable WebUser to run the stored procedure.



Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

prajey
Starting Member

2 Posts

Posted - 2006-07-20 : 05:05:40
hi there

if u got to pass a parameter with single quotes, then add a double quote b4 and after the parameter.



Prabhakar Jeyaraman
Go to Top of Page
   

- Advertisement -