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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-12-09 : 06:50:16
|
| Rishi writes "Hi Sir/Madam,I'm trying to pass an interger array into MS SQL Server Procedure as parameterBut I'm not able get any result.Can you please help me out.e.g I want to pass 1,2,3,4 from c++,then I'm having a procedurexxx.@list should contain that integer array.Should it be declared as Varchar or some other type?create procedure xxx(@list AS ??????????(varchar or integer)) AS Select Status, Type, c_orderno, a.company_code, b.company_name, b.feed_code, a.market, order_time, Side, Order_qty, part_quan, Executed_price, DisclosedQty, UndisclosedQty, CPF, Contra, a.Currency_code, SettlCurrency, Executed_time, PC, Dealer, withdraw_request_time, OrderID, Submitted_by, Withdrawn_by, msg, Price FROM Orders a, cominfo b WHERE order_no = 29451 AND acct_no = '0000000' AND c_orderno NOT IN(@list) list shd be comma separated order values. order is an integer type. How to pass list of integers as parameter???Please guide me." |
|
|
surendrakalekar
Posting Yak Master
120 Posts |
Posted - 2005-12-09 : 07:46:08
|
| Try this in your sp.Exec ('Select Status, Type, c_orderno, a.company_code, b.company_name, b.feed_code, a.market, order_time, Side, Order_qty, part_quan, Executed_price, DisclosedQty, UndisclosedQty, CPF, Contra, a.Currency_code, SettlCurrency, Executed_time, PC, Dealer, withdraw_request_time, OrderID, Submitted_by, Withdrawn_by, msg, Price FROM Orders a, cominfo b WHERE order_no = 29451 AND acct_no = ''0000000'' AND c_orderno NOT IN(' + @list + ')')Surendra |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-09 : 08:29:05
|
| Surendra,If c_orderno is of Varchar datatype then the values at @list will come as data1,data2,etc without single quotes. You need to handle that. Refer thishttp://sqlteam.com/forums/topic.asp?TOPIC_ID=58867MadhivananFailing to plan is Planning to fail |
 |
|
|
surendrakalekar
Posting Yak Master
120 Posts |
Posted - 2005-12-09 : 08:39:08
|
quote: Originally posted by madhivanan Surendra,If c_orderno is of Varchar datatype then the values at @list will come as data1,data2,etc without single quotes. You need to handle that. Refer thishttp://sqlteam.com/forums/topic.asp?TOPIC_ID=58867MadhivananFailing to plan is Planning to fail
>> order is an integer type. The type of OrderNo is int or else he can use provided link.Surendra |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-09 : 11:48:10
|
Rishi,Many developers like to avoid dynamic sql (when possible). An alternative to EXECing the statement is to use a generic parsing routine that converts a delimited string to typed values. Here is a simple example a parsing an array. There are many topics here that discuss various ways to do this. Here is one that includes links to others:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58826you can also search articles for "CSV" on this site.execute this entire code block to see it work:set nocount on--============================================================================--A simple parsing routineif object_id('dbo.udf_ParseIntArray') > 0 drop function dbo.udf_ParseIntArraygocreate function dbo.udf_ParseIntArray(@in varchar(8000))returns @out table (i int)asbegin declare @idx int select @idx = 1 ,@in = ',' + @in + ',' while @idx > 0 and @idx < len(@in) begin insert @out (i) select i from (select substring(@in, @idx+1, charindex(',', @in, @idx+1)-@idx-1) i) a --make sure it's an int where i not like '%[^0-9]%' and len(i) <= 10 --this ones not fool proof but...eh --reset @idx to next delimiter set @idx = charindex(',',@in,@idx+1) end returnendgo--============================================================================declare @intArray varchar(8000)set @intArray = '3,4'--use not in ()select nfrom (select 1 n union select 2 union select 3 union select 4) awhere n not in (select i from dbo.udf_ParseIntArray(@intArray))--or use a left outer joinselect nfrom (select 1 n union select 2 union select 3 union select 4) aleft join dbo.udf_ParseIntArray(@intArray) ex on ex.i = a.nwhere ex.i is null--============================================================================goif object_id('dbo.udf_ParseIntArray') > 0 drop function dbo.udf_ParseIntArrayBe One with the OptimizerTG |
 |
|
|
|
|
|
|
|