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)
 CharIndex and ...?

Author  Topic 

Hillxie
Starting Member

17 Posts

Posted - 2002-09-15 : 14:26:42
I have two tables named tblPhoto and tblUser.

tblPhoto table has a field named PhotoType, tblUser table has a field named AccessLevel.

PhotoType has values like '|A|B|' which means this photo belongs to Type "A" and Type "B".

AccessLevel has values like '|B|C|' which means this user can see the photos whose type is Type "B" or Type "C".


My question is how can I use sql statement to select all photos that allow the user(for example: UserID=10) to see?

thanks!

rrb
SQLTeam Poet Laureate

1479 Posts

Posted - 2002-09-15 : 19:51:29
Well there probably is a way, but here in Oz it's a monday morning, and that's way too difficult (perhaps after my third cup of tea...) My suggestion: just a tea-spoon of normalisation helps the medicine go down...in the most delightful way.


--this
/*
declare @tblPhoto table (id int identity(1,1), Phototype nvarchar(50))
insert into @tblPhoto (Phototype) values ('|A|B|')
insert into @tblPhoto (Phototype) values ('|C|D|')
insert into @tblPhoto (Phototype) values ('|B|C|')
*/
--becomes
declare @tblPhoto table (id int identity(1,1), filepath nvarchar(200) )
declare @PhotoType table (PhotoId int , typename nvarchar(5))

--photos
insert into @tblPhoto (filepath) values ('C:\photos\photo1.bmp')
insert into @tblPhoto (filepath) values ('C:\photos\photo2.bmp')
insert into @tblPhoto (filepath) values ('C:\photos\photo3.bmp')

--phototype
insert into @PhotoType (PhotoId, typename) values (1,'A')
insert into @PhotoType (PhotoId, typename) values (1,'B')
insert into @PhotoType (PhotoId, typename) values (2,'C')
insert into @PhotoType (PhotoId, typename) values (2,'D')
insert into @PhotoType (PhotoId, typename) values (3,'B')
insert into @PhotoType (PhotoId, typename) values (3,'C')


--and this
/*
declare @tblUser table (id int identity(1,1), AccessLevel nvarchar(50))
insert into @tblUser (AccessLevel) values ('|A|B|')
insert into @tblUser (AccessLevel) values ('|C|D|')
*/
--becomes
declare @tblUser table (id int identity(1,1), username nvarchar(50))
declare @AccessLevel table (UserId int, levelname nvarchar(5))

--users
insert into @tblUser (username) values ('Mary Poppins')
insert into @tblUser (username) values ('Doctor Dolittle')
--levels
insert into @AccessLevel (UserId, levelname) values (1, 'A')
insert into @AccessLevel (UserId, levelname) values (1, 'B')
insert into @AccessLevel (UserId, levelname) values (2, 'B')
insert into @AccessLevel (UserId, levelname) values (2, 'C')


--and your answer becomes something like

select distinct a.*
from @tblPhoto a
inner join @PhotoType b
on a.id = b.PhotoId
inner join @AccessLevel c
on b.typename = c.levelname
inner join @tblUser d
on c.UserId = d.id
where d.Username = 'Mary Poppins'


try that and let me know how you go...

--
I hope that when I die someone will say of me "That guy sure owed me a lot of money"

Edited by - rrb on 09/15/2002 20:00:03
Go to Top of Page

Hillxie
Starting Member

17 Posts

Posted - 2002-09-15 : 22:19:33
rrb, thanks very much for your responses! I will give it a try!

Go to Top of Page

Hillxie
Starting Member

17 Posts

Posted - 2002-09-16 : 12:13:59
Thanks! It works. But my project is almost finished, if I use this new database design, I have to change lots of Asp.net codes. If there is a solution for my old database design, I will appreciate it very much!

If it is too difficult, I will start to change the codes according to the new database design.

thanks!

Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2002-09-16 : 12:36:15
Easier to make the change now, vs having to make a major change on a production database. it might cost you some time and effort now, but later on it's going to be a LOT more work since you have to keep all of the data. The normalized way should run faster as well, so as time goes on, your system should stay pretty fast, unlike the | separated value solution you currently have.

If you don't have time to fix it now, you probably won't have time to fix it later. Make some time now, you'll thank me later :)

Michael



<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

Hillxie
Starting Member

17 Posts

Posted - 2002-09-16 : 12:58:24
Michael, Thanks for your advice! You are totally right, I will start to make the changes right now.

Go to Top of Page

rrb
SQLTeam Poet Laureate

1479 Posts

Posted - 2002-09-16 : 19:07:15
Hillxie

you should be able to write some pretty basic scripts to do most of the changes, then create views to replace where you may have referred directly to tables in your sps and pages.

if you have any problems with manipulating the data, let us (all) know.

Cheers

--
I hope that when I die someone will say of me "That guy sure owed me a lot of money"
Go to Top of Page
   

- Advertisement -