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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Passing int array from c++ into MS SQL server procedure

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 parameter
But 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 procedure
xxx.
@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
Go to Top of Page

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 this
http://sqlteam.com/forums/topic.asp?TOPIC_ID=58867

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 this
http://sqlteam.com/forums/topic.asp?TOPIC_ID=58867

Madhivanan

Failing 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
Go to Top of Page

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=58826
you can also search articles for "CSV" on this site.

execute this entire code block to see it work:

set nocount on
--============================================================================
--A simple parsing routine

if object_id('dbo.udf_ParseIntArray') > 0
drop function dbo.udf_ParseIntArray
go
create function dbo.udf_ParseIntArray(@in varchar(8000))
returns @out table (i int)
as
begin
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
return
end
go
--============================================================================

declare @intArray varchar(8000)
set @intArray = '3,4'

--use not in ()
select n
from (select 1 n union select 2 union select 3 union select 4) a
where n not in (select i from dbo.udf_ParseIntArray(@intArray))

--or use a left outer join

select n
from (select 1 n union select 2 union select 3 union select 4) a
left join dbo.udf_ParseIntArray(@intArray) ex
on ex.i = a.n
where ex.i is null

--============================================================================
go
if object_id('dbo.udf_ParseIntArray') > 0
drop function dbo.udf_ParseIntArray



Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -