473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Select where val in(groups)

Hi - I have set-up security for my users - the security is held in a
text field, separated by a comma.

If the users a member of groups 1, 5 and 6 - the usergroups field is set
to 1,5,6 - to check if they are allowed access to the books, I need to
check each of these numbers (by using split,"," and building a SQL
statement - this needs to check against the membergroups of each book
title - which again is set as a text field - eg. "2,3,4,5"

The Select statement to show users book titles they are allowed to see
should be:

select id, titles, authors from tblbooks where (1 in (membergroups) OR 5
in (membergroups) or 6 in (membergroups))

...the error I get though is 'Syntax error converting the varchar value
'2,3,4,5' to a column of data type int.

If I change it to:

select id, titles, authors from tblbooks where ('1' in (membergroups) OR
'5' in (membergroups) or '6' in (membergroups))
...I don't get any errors, but I don't get any results either - any
ideas? Thanks a lot,

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #1
5 2520
"best" solution involves restructuring your database to move the security
field into a table so that each code is by itself. Then you could use a
subselect with the IN operator to test.

I am going to guess that you will be unable to do that. In that case you
will have to use the LIKE operator as in

where (membergroups LIKE '%1%) or ...

Note that this will not work properly if codes are more than one digit
(above will match any code containing "1").

--
Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com
"Mark" <an*******@devdex.com> wrote in message
news:Oq**************@tk2msftngp13.phx.gbl...
Hi - I have set-up security for my users - the security is held in a
text field, separated by a comma.

If the users a member of groups 1, 5 and 6 - the usergroups field is set
to 1,5,6 - to check if they are allowed access to the books, I need to
check each of these numbers (by using split,"," and building a SQL
statement - this needs to check against the membergroups of each book
title - which again is set as a text field - eg. "2,3,4,5"

The Select statement to show users book titles they are allowed to see
should be:

select id, titles, authors from tblbooks where (1 in (membergroups) OR 5
in (membergroups) or 6 in (membergroups))

..the error I get though is 'Syntax error converting the varchar value
'2,3,4,5' to a column of data type int.

If I change it to:

select id, titles, authors from tblbooks where ('1' in (membergroups) OR
'5' in (membergroups) or '6' in (membergroups))
..I don't get any errors, but I don't get any results either - any
ideas? Thanks a lot,

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 19 '05 #2
Mark wrote:
Hi - I have set-up security for my users - the security is held in a
text field, separated by a comma.
Bad! You are much better off normalizing this database design by using a
separate table in which the group numbers for each user are stored in
separate rows, like this:

UserID GroupID
1 1
1 5
1 6

If the users a member of groups 1, 5 and 6 - the usergroups field is
set to 1,5,6 - to check if they are allowed access to the books, I
need to check each of these numbers (by using split,"," and building
a SQL statement - this needs to check against the membergroups of
each book title - which again is set as a text field - eg. "2,3,4,5"
This really highlights why it is a bad idea to store multiple pieces of
information in a single field. Think about how easy this query would be if
you had the above structure:

WHERE ... GroupID IN (1,5,6)


The Select statement to show users book titles they are allowed to see
should be:

select id, titles, authors from tblbooks where (1 in (membergroups)
OR 5 in (membergroups) or 6 in (membergroups))
Why is user-security information stored in a table called "tblbooks"? Oh
wait! These are the groups that are allowed to view the books, right? So you
will need a table to store these groups as well! (can a user belong to more
than one group?) So a table such as the above example will work. Just change
"UserID" to "id" (my personal preference is to make these column names a
little more descriptive: BookID leaves no doubt about the data stored in the
column). The simple query would be:

.... FROM tblbooks b inner join BookGroups g
ON b.id = g.id
WHERE g.GroupID IN (1,5,6)

..the error I get though is 'Syntax error converting the varchar value
'2,3,4,5' to a column of data type int.

If I change it to:

select id, titles, authors from tblbooks where ('1' in (membergroups)
OR '5' in (membergroups) or '6' in (membergroups))
..I don't get any errors, but I don't get any results either - any
ideas? Thanks a lot,


The IN operator expects a list of values. It WILL NOT parse a column or
variable to turn it into a list, merely because the data in the variable or
column contains commas, making it appear to be a list. For one thing, you
may not wish it to do this.
So, here is what is happening when the comparison "('5' in (membergroups)"
is being evaluated: '5' is being compared to the entire string '2,3,4,5'.
This comparison will always return false: '5' will never equal '2,3,4,5'.
Your best approach is to normalize the database (see above). However, if you
cannot do this for some reason, then here are some possible solutions (these
solutions will not perform very well, but ...):

....WHERE membergroups LIKE '%1%' OR membergroups LIKE '%5%' OR membergroups
LIKE '%6%'

or

.... WHERE charindex('1',membergroups) > 0 OR ...

These will fail if you have any group numbers greater than 9, so:

....WHERE ',' + membergroups + ',' LIKE '%,1,%' OR ...

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #3
Here is some more information about using lists and arrays with SQL Server:
http://www.algonet.se/~sommar/arrays-in-sql.html

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #4
Hi - thank you both very much - it is not too late to change the
structure, and your recommendations have helped a lot.

Thanks again,

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #5
On Mon, 10 Nov 2003 09:52:16 -0800, Mark <an*******@devdex.com> wrote:
Hi - thank you both very much - it is not too late to change the
structure, and your recommendations have helped a lot.


Still seems a bit more complicated. You say you have 1 or more groups
that a member is in (1,5,6) and a list of items to match to (2,3,4,5).
The IN operator will not work here... you are trying to see if all
values of (1,5,6) appear in (2,3,4,5) - or if any of them are in the
2nd list, right?

Best way I can see is to have a table of User_Groups

UID GID
1 1
1 5
1 6

And a table of Book_Groups

BID GID
99 2
99 3
99 4
99 5
55 2

Then you should be able to do a query like...

Select DISTINCT b.ID FROM Books as b
INNER JOIN Book_Groups as bg ON bg.BID = b.ID
INNER JOIN User_Groups as ug ON ug.GID = bg.GID
INNER JOIN Users as u ON u.ID = ug.ID
WHERE u.ID = 1

This should return the book ID 99 using the data I showed above.
NOTE: Untested "air" code.

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: temp | last post by:
Hi All, I wonder could someone help me with this? What I want to do is search through a list of letters and look for adjacent groups of letters that form sequences, not in the usual way of...
4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
1
by: superfly2 | last post by:
I realize the irony of asking this here, but does anyone know of any MySQL groups where I can ask my questions? Thanks.
1
by: Liz | last post by:
I have a table of about 10,000 records where each record has a numeric field named RecIdent. The value of RecIdent starts at 1 and is not sequential. For a given RecIdent, there may be only one...
6
by: rhcarvalho | last post by:
Hello there! I'm trying to make a simple Contact Manager using python (console only), however i'm having trouble implementing a division by "Groups" or "Labels" just like in Gmail. I don't have...
4
by: Michael A. Covington | last post by:
I'm developing an application that will handle files in groups of 4, namely 3 video files plus a script saying how to put them together. These are all files that I will deliver with the app, so I...
1
by: Goofy | last post by:
Hi, I am working in an organisation developing an asp.net application. The application and general use of .NET may well expand to other applications, so I need to start thinking about the...
0
by: mjay83 | last post by:
I got 3 groups in my crystal report with few records with 1 field name QtyOrdered How do I calculate number of records with QtyOrdered= 0 and show it in the end of each group I written out the...
1
by: c35tar1 | last post by:
I have a ListView within a dialog. In code the groups are created and items added. Display is 100% correct. The problem is with using Shift to select multiple items in the list. - If you...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.