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
 General SQL Server Forums
 New to SQL Server Programming
 Hairy Parse Like Statement - Dynamic Query?

Author  Topic 

msinca
Starting Member

3 Posts

Posted - 2010-08-11 : 12:52:57
Hello. I'm helping a client migrate a legacy system and am writing some SQL Server 2008 R2 SPs for them. I'm having trouble with the following:

Simplified Background:
2 Tables (User, Document).
User (id int, name varchar, groups varchar)
1 Tom 50,70,90
2 Mary 30,50,80
3 Sam 10,50,70

Document (id int, docname varchar, groups varchar)
1 Financials 30,90
2 Real Estate 50,70,90
3 Employee List 10

the 'groups' field is used for security purposes. a user only is allowed to see documents to the 'group' that he belongs to. however, users can belong to 1 or more groups and documents can be read by 1 or more groups.

I need to write a SP that first pulls up the groups that a user belongs to (ex. Sam - 10,50,70) and then does a LIKE %X% to find which documents he can read (ex. Sam -> Real Estate and Employee List). Essentially, I'll need a statement that looks like:

Select id
FROM document
WHERE group like '%' + 10 + '%'
OR group like '%' + 50 + '%'
OR group like '%' + 70 + '%'

I can't modify the table structure and use intermediary tables (which should have been done a long time ago). I can use temporary tables but am not sure how that would help.

QUESTION:
Is there any way of doing this without doing a dynamic query? I think I can figure it out that way but I try to make that a last resort. I know how to parse the CSV group strings but can't figure out how to incorporate that into a WHILE loop (with Union) or something like that.

Any help would be much appreciated.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-11 : 12:58:53
I would use Viskahs fnParseValues, which you can find on this site. It will turnn the strings into tables, which you can then join on.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-11 : 13:13:03
fnParseValues is not mine . My version is ParseValues. see

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html




------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

msinca
Starting Member

3 Posts

Posted - 2010-08-11 : 13:24:06
Thanks for the replies. However, its not the parse that I have trouble with - its the WHERE statement. Consider another user record (4 Mark 20). In this case the statement will be:
Select id
FROM document
WHERE group like '%' + '20' + '%'

How do you do a WHERE clause where it could be 1 statement or 20 statements (separated by OR's) depending on how many items I parse out of a user's group privileges? My only answer: build a dynamic query. Maybe thats the only answer here.

Again, thanks for the help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-11 : 13:32:01
why? this wont suffice?

SELECT DISTINCT n.docname
FROM
(
SELECT u.name,f.Val
FROM User u
CROSS APPLY dbo.ParseValues(u.groups,',')f
)m
JOIN
(
SELECT d.docname,f.Val
FROM Document d
CROSS APPLY dbo.ParseValues(d.groups,',')f
)n
ON n.Val=m.Val
WHERE m.name = @UserName

@UserName is passed on user name

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

msinca
Starting Member

3 Posts

Posted - 2010-08-11 : 14:20:02
visakh16: that is IMPRESSIVE. Seems to work great. I had not previously worked with CROSS APPLY (and frankly, I'm not even 100% sure how it works at this moment but I will step through it carefully to understand it).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-11 : 14:30:36
ok. then go through to understand what all you can do with it

http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -