473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2539
"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*******@devd ex.com> wrote in message
news:Oq******** ******@tk2msftn gp13.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',m embergroups) > 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*******@devd ex.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
2320
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 matching say abc to another appearance later on in the list but to look for transposed patterns. The groups of letters can be made up of between 2 to 4 letters.
4
1371
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 | TicketNo | evaluation | Username ------------------------------------------------------------------------------- 1 1 9 Jamie 2 1 8.5 ...
1
1298
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
1699
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 record with that value or there could be multiple records with the same value. A sample of records looks like: PK RecIdent 1 1 2 3 3 3 4 5
6
2067
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 any real code to post because all i got now is a raw TXT file holding the names and phones of my contacts. The only idea I could figure out until now seems too weak, so that's why i'm asking for help. I thought of making a separate list (in a...
4
1528
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 have complete control over the format and the naming of the files. I will be using DirectShow to read and process the video files. One obvious approach -- somewhat UNIX-like -- is to require that the four files reside in the same directory...
1
1053
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 arcitechture changes that will be needed to support restriction of visibility of resources or permissions to perform an action. Management of Groups etc, administration of users in groups. The obvious one which springs to mind is the use of Groups...
0
937
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 formula as: Numbervar Zero; if {StockOnHandOrderPlaced.QtyOrdered} = 0 then (Zero:=Zero+1) It calculating all records which is not according to groups, how do i change the formula so it can calculate group's record?
1
2078
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 select the first item and then a second next to it, it reverses the selection - If you select the first item and then Shift to a few further down or across groups it selects all but reverses the initial item This behavior doesn't occur when the...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.