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)
 using WHERE IN(@VAR

Author  Topic 

durban

7 Posts

Posted - 2006-12-29 : 09:25:12
I am creating a comma deliminated string which I want to pass to a stored procedure and use it as a parameter in a where clause.

The string Roles looks like this: 'role1',role2','role2'

It doesn't return any records.

ALTER PROCEDURE dbo.treeViewDefaultDir

@ListOwner varchar(200),
@Roles varchar(2000)

AS

SELECT Distinct ListID,
ListTitle,
ListParent,
ListOwner

FROM GroupList

WHERE (ListID IN
(
SELECT ListReferencedID
FROM GroupListShares
WHERE (SecurityGroupReferenceID
IN(@Roles))
OR ListOwner = @ListOwner
)
)
ORDER BY ListTitle

RETURN

any help.....thanks dan

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-29 : 09:35:09
Check out these links:

[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6134[/url]
[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830[/url]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2006-12-29 : 12:14:02
It's not that hard... if you don't mind not being able to use INDEX SEEKS (certainly, many of the other huge solutions won't either), the code is very simple...


ALTER PROCEDURE dbo.treeViewDefaultDir
@ListOwner VARCHAR(200),
@Roles VARCHAR(2000)
AS

-- SET @Roles = REPLACE(@Roles,'''','') --Uncomment if needed

SELECT Distinct
gl.ListID,
gl.ListTitle,
gl.ListParent,
gl.ListOwner
FROM GroupList gl,
GroupListShares gls
WHERE gl.ListID = gls.ListReferencedID
AND (
','+@Roles+',' LIKE '%,'+gls.SecurityGroupReferenceID+',%'
OR
gls.ListOwner = @ListOwner
)
ORDER BY gl.ListTitle

RETURN


This, of course, assumes that the actual string contained in @Roles is properly formatted as role1,role2,role3 as opposed to what you posted as 'role1',role2','role2'. If the format of the data in @Roles is as you posted AND you cannot change it, then uncomment the snippet in the code above...

"Splitting" the @Roles parameter into it's component parts isn't much more difficult using a little SQL prestidigitation...

First, if you don't already have one, you REALLY need to build a permanent "Tally" table... here's how...

--===== Create and populate the Tally table on the fly
SELECT TOP 11000 --equates to more than 30 years of dates
IDENTITY(INT,1,1) AS N
INTO dbo.Tally
FROM Master.dbo.SysColumns sc1,
Master.dbo.SysColumns sc2

--===== Add a Primary Key to maximize performance
ALTER TABLE dbo.Tally
ADD CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N)

--===== Allow the general public to use it
GRANT SELECT ON dbo.Tally TO PUBLIC


Then, the code to split @Roles and use the components in your query becomes very easy...

  ALTER PROCEDURE dbo.treeViewDefaultDir
@ListOwner VARCHAR(200),
@Roles VARCHAR(2000)
AS

-- SET @Roles = REPLACE(@Roles,'''','') --Uncomment if needed

--===== Prep the @Roles Parameter for "splitting"
SET @Roles = ','+@Roles+','

SELECT Distinct
gl.ListID,
gl.ListTitle,
gl.ListParent,
gl.ListOwner
FROM GroupList gl,
GroupListShares gls,
(--Derived table splits the input parameters in the @Roles variable so can join
SELECT SUBSTRING(@Roles,N+1,CHARINDEX(',',@Roles,N+1)-N-1) AS Role
FROM dbo.Tally WITH (NOLOCK)
WHERE N < LEN(@Roles)
AND SUBSTRING(@Roles,N,1) = ','
) roles

WHERE gl.ListID = gls.ListReferencedID
AND (
gls.SecurityGroupReferenceID = roles.Role
OR
gls.ListOwner = @ListOwner
)
ORDER BY gl.ListTitle

RETURN


Of course, I don't have your data or tables because you didn't post any... so I haven't tested the code, but it should be REAL close.

--Jeff Moden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-12-30 : 02:58:56
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=CSV,Splitting%20delimited%20lists,Best%20split%20functions

Kristen
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2006-12-30 : 10:43:50
Thanks, Kristen...

Beautiful! Nice to see that someone else knows about Tally tables (so far as I can tell, you're the only one I've seen on the forum that knows about them). I like some of the things you did with the sp_indexoption... I'll have to incorporate that into my own code. I also like the idea of the doubled up join to find the current and next delimiter... I can see where that might speed up the performance of a function a bit. I'll check it out. Thanks again.

--Jeff Moden
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-12-30 : 10:56:28
"so far as I can tell, you're the only one I've seen on the forum that knows about them"

I reckon that will get you lynched !!

Kristen
Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2006-12-30 : 11:05:54
Heh... yeah, I see now... I've been following more of the threads that came up from your original. Even Celko popped in on one of them...

Looks like I may have brought my own rope

--Jeff Moden
Go to Top of Page
   

- Advertisement -