Help in SQl Query

Hi all,

Greetings!! I am Migrating data from one database to a new one, both the database are in SQL Server 2005. The tables in the old DB are;

Users (UserName (PK,varchar),ISAdmin(bool),IsGuest(bool))

Groups table(GroupName (pk Varchar))

UserGroupmapping (UserName,GroupName)

In our new DB these tables are modified as

Users (userId (pk), UserName (varchar))

Groups table(GroupId(int),GroupName (pk Varchar))

UserGroupmapping (UserId(FK),GroupId(FK),AccessId(int))

AccessMaster(AccessId(PK,int),Desc(varchar))

The idea now is, all the bool value previously in the User table (IsAdmin etc...) are now to be represented as a entry in the UserGroupMappingTable.

for E.g

if the Old users table was like

Sam 1 1

and the old UserGroupMapping was like

Sam, 'Player'

then the new UserGroupMapping should be populated like;

1,1,1(this last 1 will be from the AccessMaster table for the Access Admin)

1,1,2(2 should be from AccessMaster for Guest etc.)

pffff.. Sorry if am confusing here :(

i was trying with a quert of the sort to first select the first row only.

/*************/

select Users.UserId, Grp.GroupId,
case when Usr.IsAdmin=1 then 8 else 0
End as accesslevel
from
(
select distinct UG.UserName from [Old_db]..[UserGroupMapping] UG
join
[Old_db]..[Users]Usr
on
UG.login = Usr.login
join
[NewDB]..[Groups]Grp
on
UG.GroupName = Grp.GroupName
join
[NEWDB]..[Users]Users
on
Users.UserName=UG.UserName) as mytable

/*************/

but am getting error like;

Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Users.UserId" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Grp.GroupId" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Usr.IsAdmin" could not be bound.

Can someone help?

Thanks in Advance.

[2319 byte] By [MShetty] at [2007-12-24]
# 1

Once you create the derived table in the From clause and give it an alias -- mytable -- then you need to use the alias to access the values. So instead of Users.UserID you would need mytable.UserId. However, you are not selecting all the fields in your derived table that you are trying to access.

It looks like you don't need the derived table at all. Just select the proper fields after you have joined all the tables together. Otherwise, you will need to join the other tables back in with the derived table.

anomolous at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2

Thanks for the valuable input. It worked by selecting all cols inside the derived table itself.

Is there any other eficient way of writing a query for the same requirement ?

thanks

MShetty at 2007-10-8 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified