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.
| Author |
Topic |
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-01-09 : 01:00:55
|
| I deliberately intend to add some duplicates to one of my tables. For egJob User IsAdminJobID 235 User ID 1 JobID 235 User ID 5 JobID 235 User ID 9 JobID 235 User ID 5 JobID 235 User ID 2 JobID 235 User ID 9 JobID 235 User ID 10 JobID 235 User ID 1 I know its bad practice to do such a thing but there is a genuine reason. What I need to do is to be able to have a SQL statement that appends true to the IsAdmin field whenever it encounters the next UserID thats happens to be a duplicate. Hence the above would look like:Job User IsAdminJobID 235 User ID 1 JobID 235 User ID 5 JobID 235 User ID 9 JobID 235 User ID 5 TrueJobID 235 User ID 2 JobID 235 User ID 9 TrueJobID 235 User ID 10 JobID 235 User ID 1 TrueThanks |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2004-01-09 : 01:18:07
|
Can't be done, unless you assign another column which is unique. But if you do that, then its trivial..It's not that its a bad idea, its just that if they really are duplicate rows, there's no way to individually reference them, and they may as well not be there...Also, is your tableJob,User,IsAdmin----JobID 235,User ID 1,JobID 235,User ID 5,orJob,User,IsAdmin----JobID 235, User ID, 1 JobID 235, User ID, 5 Or what? Can you repost with commas?Anyway, here's the idea...create table jobs (jobid varchar(20), userid varchar(20), isadmin varchar(20))goinsert into jobs (jobid, userid, isadmin) select '235','5',nullinsert into jobs (jobid, userid, isadmin) select '235','7',nullinsert into jobs (jobid, userid, isadmin) select '235','9',nullinsert into jobs (jobid, userid, isadmin) select '235','5',nullinsert into jobs (jobid, userid, isadmin) select '235','5',nullinsert into jobs (jobid, userid, isadmin) select '235','7',nullselect * from jobsalter table jobs add id int identity(1,1)goupdate jobsset isadmin = 'true'where id in(select a.idfrom jobs a, jobs b where a.id > b.idand a.jobid = b.jobidand a.userid = b.userid)select * from jobs cheers |
 |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2004-01-09 : 02:10:31
|
| Use an identity column.CREATE Table JobUser(JobUserID int IDENTITY(1, 1),JobID int,UserID int,IsAdmin bit)This way you can do this for your stored procedure.CREATE PROCEDURE InsertJobUser( @JobID int, @UserID int)ASINSERT INTO JobUser (JobID, UserID, IsAdmin)SELECT @JobID, @UserID, CASE WHEN @JobID = (SELECT j.JobID FROM JobUser j WHERE j.JobUserID = (SELECT MAX(j2.JobUserID) FROM JobUser j2) AND j.IsAdmin = 0) THEN 1 ELSE 0 ENDGOThis should work for you (I think).Dustin Michaels |
 |
|
|
saglamtimur
Yak Posting Veteran
91 Posts |
Posted - 2004-01-09 : 02:35:30
|
| rrb's solution is for a table which currently has records. You can use it if you already have records. But, if I didn't misunderstand your question, you want to do an automation for future inserts (Question topic : Trying to append a field if an insert happens to ). If this is the case, you can use triggers. Here is the sample;create trigger recdeneme on jobs instead of insert asdeclare @jobid userid varchar(20), userid varchar(20)set @jobid= (select jobid from inserted)set @userid = (select userid from inserted)if exists (select * from jobs where (jobid=@jobid and userid=@userid and isadmin is null))-- we found the record, so set isadmin to true for new insertbegin insert into jobs (jobid, userid, isadmin) values (@jobid, @userid, 'true') endelse-- This is the first time of this record inserted, so do nothing just insertbegin insert into job (jobid, userid) values (@jobid, @userid)endBut what about Nth same value insert? In this case this trigger will set isadmin to true for the same recuring records.Home this helps. |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-01-09 : 19:01:48
|
| The last reply was exactly what I needed. As to some of the other questions:rrb : Can't be done, unless you assign another column which is unique. But if you do that, then its trivial..It's not that its a bad idea, its just that if they really are duplicate rows, there's no way to individually reference them, and they may as well not be there...May I ask a question regarding this? I understand that I won't be able to reference two ids with the same id but can I not differentiate two rows with the same id if for eg the admin field is set to true in one and not the other. egJob User AdminjobID 5, userID 4, TruejobID 5, userID 4, Nullso i could sael, select jobid(my jobiD) where Admin = true. Would that not work. There will never be more than two rows that will have exactly the same data, IE one will be admin one will not. Will that work?rrb:Also, is your tableJob,User,IsAdmin----JobID 235,User ID 1,JobID 235,User ID 5,orJob,User,IsAdmin----JobID 235, User ID, 1 JobID 235, User ID, 5 Or what? Can you repost with commas?Sorry its actuallyJob,User,IsAdmin----------------235, 3, True236, 7, Null236, 7, TrueHope that makes sense.saglamtimur : But what about Nth same value insert? In this case this trigger will set isadmin to true for the same recuring records.There will only ever be one duplicate record. I make sure of that. |
 |
|
|
rrb
SQLTeam Poet Laureate
1479 Posts |
Posted - 2004-01-11 : 17:01:37
|
| >so i could sael, select jobid(my jobiD) where Admin = true.yep that will work, but since the rows are now no longer identical, you may as well have a hidden identity which is not unique.--I hope that when I die someone will say of me "That guy sure owed me a lot of money" |
 |
|
|
|
|
|
|
|