|
Hi, I've searched and haven't found a relevant answer to this 'simple' problem.
I have 3 tables, one of which is a junction table between the other two:
Users
-----
UserId
UserName
Roles
-----
RoleId
RoleName
UserRoles
---------
UserRoleId
UserId
RoleId
There are FK relationships between UserRoles and the other 2 tables.
Problem statement:
What I want to do is update the user's RoleId in the UserRoles table if I only have the person's RoleName and UserName.
Here's what I'm starting with code-wise (MSSQL):
<br />
update a<br />
set a.RoleId = ?? Set to what?<br />
FROM UserRoles a<br />
inner join Users b on a.UserId = b.UserId<br />
inner join Roles c on a.RoleId=c.RoleId<br />
where c.RoleName= 'Administrator' AND b.UserName='someuser'<br />
Further information:
I'm trying to do this within an update method call within an ASP.NET GridView control. This information may be a bit more than what's needed but I thought I would mention it just in case it may offer a different solution.
Thanks for any assistance in advance.
---
Stan
|
|
|
|
|
Sorry, I'm missing the logic in your question.
This is what I read:
we know the UserName (and hence the UserId)
we know the RoleName (and hence the RoleId)
I see two possibilities:
1. the UserRoles table does not contain a matching UserId,RoleId record; you have to insert one.
2. the UserRoles table does contain a matching UserId,RoleId record. Now what needs to be updated???
Or is it you want the existing UserId,RoleId record to get another value of UserRoleId for some mysterious reason?
|
|
|
|
|
Your last point is what I'd like to find an answer for: I want to UPDATE the RoleId for an EXISTING RoleId,UserId combo.
In short, I want to update the junction table.
|
|
|
|
|
You failed to make it any clearer. This is my only gamble at what it is you might possibly want (not tested!):
update a
set a.RoleId = d.RoleId
FROM UserRoles a
inner join Users b on a.UserId = b.UserId
inner join Roles c on a.RoleId = c.RoleId
inner join Roles d
where c.RoleName= 'Administrator' AND b.UserName='someuser' AND d.RoleName='AnotherRole'
|
|
|
|
|
Thanks for your help. I apologize I hadn't been able to properly characterize my problem. In short: I want to update a junction table (RoleId and UserId) when I only know the UserName and RoleName.
I'll play around with variations of what you provided and search a bit further online. I'll post whatever resolution(s) I come up with.
|
|
|
|
|
I think you're thinking a bit wrong here (Alternatively have I understood you wrong).
There's no real point in updating the UserRoles table. You want to add or remove roles a user have. There's nothing to update really.
So you should rather think along the line of Insert and Delete instead.
For example something similar to:
Insert Into UserRoles (UserID,RoleID)
Values ((Select Userid from Users Where UserName = 'someuser'),(Select RoleID from Roles Where Rolename = 'Administrator')) Or:
Delete From UserRoles
Where Userid = (Select Userid from Users Where UserName = 'someuser')
And Roleid = (Select RoleID from Roles Where Rolename = 'Administrator')
"When did ignorance become a point of view" - Dilbert
|
|
|
|
|
OK, thank you, I'll think along the lines of DELETE and INSERT.
The next step is to have this work with an SqlDataSource1_Updating method with my Gridview control in C#.
Thanks to all for your help.
|
|
|
|
|
i'm trying to execute the "sp_databases" proc in sql sever 2008 express as "sa" but it doesn't return any value even though there
are two customer created DBs available. what must be the problem.. any idea about this issue.
thanx in advance.
|
|
|
|
|
what happens if you execute
select * from sys.databases
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
hmm i get all the DBs in the server . at the same time a found some articles saying that there is a permission problem
i used
USE master;
GRANT EXECUTE ON OBJECT::dbo.sp_databases
TO public;
GO
but still get the "Cannot find the object 'sp_databases', because it does not exist or you do not have permission."
message.
and also in the same machine i have another standard instance is installed and running, but there i get the list of DBs with
this sp_databases.
have any idea...
thanx in advance..
|
|
|
|
|
try
exec master..sp_database
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
exec master..sp_databases executes but it shows nothing
|
|
|
|
|
|
sys.database is a system table introduced with SQL 2005 and used in sql 2008
where sp_database is an actual stored procedure that has been present since sql server 2000 I believe
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
I guess I will wake up sooner or later.
The sp is sys.sp_databases
|
|
|
|
|
oh that something new learnt today it also works executing it on sys.sp_databases too
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
using RowNum? who can tell me the RowId in last Column? what's effect?
|
|
|
|
|
openboy2010 wrote: using RowNum?
SELECT ROWNUM, T.*
FROM TEST_TABLE T
ORDER BY ROWNUM
openboy2010 wrote: who can tell me the RowId in last Column?
SELECT C.ROWYOURBOAT
FROM
(SELECT COUNT(*) AS ROWCOUNT
FROM TEST_TABLE T
) A,
(SELECT ROWNUM AS ROWCOUNT, ROWID AS ROWYOURBOAT
FROM TEST_TABLE T
ORDER BY ROWNUM
) C
WHERE A.ROWCOUNT = C.ROWCOUNT
openboy2010 wrote: what's effect?
I don't know what your are asking here...
modified on Thursday, December 30, 2010 11:03 AM
|
|
|
|
|
the last sentence means what's the RowId's effect?
|
|
|
|
|
The RowID's affect on what?
|
|
|
|
|
if we sort a table ,can we using the RowID sort ? I'm a Chinese Student,My Written English is not very well.
|
|
|
|
|
openboy2010 wrote: My Written English is not very well.
That's ok it is good enough, I just wanted clarification so I can answer your homework question appropriately.
RowID is a column generated by oracle to map to that particular row.
If you sort by RowID instead of RowNum, the results may not be the same. RowNum is a column generated by Oracle from the RESULTS of the Select Query.
|
|
|
|
|
|
Hello All,
I will be designing a new database and wanted to read an article to make sure all grounds are covered while implementing this.
I know the need for good naming convention, primary keys, foreign keys, indexes. Make sure to have a schema set up. This is in SQL server 2005.
Anything else? Any articles that has helped you through the process, please do send me.
Thanks for all your help!
Vani
|
|
|
|
|
There is a series of articles by Joe Celko 'Stairway to Database Design' Step 1[^], I think there are 6/7 in the series. You may have to sign up to read them (can't remember) but it is a useful site for database stuff anyway.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|