SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


comma seperated parameter


comma seperated parameter

Author
Message
Naveen Kumar-807681
Naveen Kumar-807681
Say Hey Kid
Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)

Group: General Forum Members
Points: 702 Visits: 78
hi everybody,

i have 3 tables

1. customer_master(customerid, customername)
2. language_master(languageid, languagename)
3. customer_language(customerid, languageid)

i will get a parameter like 'english, kannada, punjabi' to my stored procedure or query. how simple can I get all the customers who speak all the 3 languages.

Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.Smile
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)

Group: General Forum Members
Points: 184173 Visits: 33296
There are a bunch of functions available in the Scripts area of the site. You do a search to track them down. Here's the one at the top of the list:http://www.sqlservercentral.com/scripts/Miscellaneous/31913/

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Naveen Kumar-807681
Naveen Kumar-807681
Say Hey Kid
Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)

Group: General Forum Members
Points: 702 Visits: 78
Thanks for ur reply. is there any other better way to do this?

Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.Smile
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)

Group: General Forum Members
Points: 184173 Visits: 33296
I know of four ways to do this, a function, a tally table, ad hoc queries or changing the data to XML. You have a function. This article is by Jeff Moden. He's the man where this is concerned. You can simply build a query so that you can insert your comma-delimited list into an IN clause, but that's going to cause recompiles and possibly bad performance. Converting to XML will work, but it's much more memory intensive and slower. I'd stick with the tally table (1st choice) or the function (2nd choice).

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)

Group: General Forum Members
Points: 427260 Visits: 43428
Thanks for the kudo, Grant. Smile

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)

Group: General Forum Members
Points: 184173 Visits: 33296
Credit where it's due.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (130K reputation)SSC Guru (130K reputation)SSC Guru (130K reputation)SSC Guru (130K reputation)SSC Guru (130K reputation)SSC Guru (130K reputation)SSC Guru (130K reputation)SSC Guru (130K reputation)

Group: General Forum Members
Points: 130182 Visits: 9672
Credit is due and all good, but that still does not answer the question....


SELECT m.customerid, m.customername
FROM dbo.customer_master m
inner join dbo.customer_language cl on m.customerid = cl.customerid
inner join dbo.language_master lm on cl.languageid = lm.languageid
where lm.languagename IN (Select languageName from dbo.JeffsFunction(@Languages)) -- Fetch the languages here with Jeff's code

--this is the not so obvious part that answer the question you are asking
GROUP BY m.customerid, m.customername
HAVING COUNT(*) = (SELECT COUNT(*) FROM dbo.JeffsFunction)


You need to know how many languages are in the parameter string (can be in a separate parameter to avoid calling the split function twice), then you need to count how many languages are matching per customer and make sure that those 2 numbers are equal... hence that customer speaks all required languages.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)SSC Guru (427K reputation)

Group: General Forum Members
Points: 427260 Visits: 43428
Ninja's_RGR'us (2/21/2009)
Credit is due and all good, but that still does not answer the question....


True enough... we were waiting on some test data so we could show some tested code.

Naveen, take a look at the article in my signature below to get better answers quicker.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Naveen Kumar-807681
Naveen Kumar-807681
Say Hey Kid
Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)Say Hey Kid (702 reputation)

Group: General Forum Members
Points: 702 Visits: 78
Hi

Thanks for all the replies and suggestions. here is the query to get the required reuslt

SELECT c.customer
FROM #customer_master c
INNER JOIN
(
SELECT cl.customerid,COUNT(DISTINCT l.languagename) AS langcnt
FROM #customer_language cl
INNER JOIN #language_master l
ON l.languageid=cl.languageid
INNER JOIN (SELECT ltrim(f.Val) as val FROM dbo.split(@param,',')f)t
ON t.Val=l.languagename
GROUP BY cl.customerid
)tmp
ON tmp.customerid=c.customerid
WHERE langcnt=(SELECT COUNT(DISTINCT f1.Val) FROM dbo.split(@param,',')f1)

parametere will be like
@param = 'hindi, telugu'

Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.Smile
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search