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 2005 Forums
 Transact-SQL (2005)
 Basic One

Author  Topic 

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-02-17 : 05:32:45
Hi Guys

Look like i am getting stuck with basic issues.
i have SP which takes comma sperated string as parameter.
the parameter has the customer names.

in my SP i am trying to use the parameter and return customer total sale value.
CustomerId is int and @CustomerRange is varchar

select @@CustomerRange=COALESCE(@@CustomerRange+',','')+ cast(CustomerID as varchar) from Customer where Customer name in('a','b,'c')

select customerID,sum(SalVal) from CustomerSales where cast(CustomerId as varchar) in(@CustomerRange)

i get no records. is some thing i am missing

========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 05:38:23
try like this
select customerID,sum(SalVal) from CustomerSales where '%,' + @CustomerRange+ ',%' LIKE '%,' + CAST( CustomerId AS VARCHAR(255)) +',%' group by customerid
Go to Top of Page

karthik_padbanaban
Constraint Violating Yak Guru

263 Posts

Posted - 2009-02-17 : 05:47:42
Try this.

exec('select customerID,sum(SalVal) from CustomerSales where cast(CustomerId as varchar) in('+@CustomerRange+')')

Karthik
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 06:50:57
slight modification to karthik,
exec('select customerID,sum(SalVal) from CustomerSales where cast(CustomerId as varchar) in('+@CustomerRange+')group by customerid')

but it is better to avoid using dynamically........
Go to Top of Page

khalik
Constraint Violating Yak Guru

443 Posts

Posted - 2009-02-17 : 07:04:11
thanks bklr


========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-17 : 07:06:16
quote:
Originally posted by khalik

thanks bklr


========================================
Project Manager who loves to code.
===============
Ask to your self before u ask someone



ur welcome
Go to Top of Page
   

- Advertisement -