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 RETURNOf 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