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
 General SQL Server Forums
 New to SQL Server Programming
 Trouble in using Loop in Update query

Author  Topic 

HeyZain
Starting Member

7 Posts

Posted - 2011-02-06 : 16:52:52
Hi Folks,

I need to update the value of "AssignedTo" column in my table (MITagging) to logged in user's name for all pending requests in MITagging table. For example, if there are 10 pending requests and 2 logged in users, then I want to set value of AssignedTo column for 1st 5 rows to 1st username and next 5 rows to 2nd username. I'm stuck on how to use a loop (online users may be 10 and pending requests may be 1000)

Table MITagging:
----------------
ReqID CustomerName MITaggingStatus AssignedTo
----- ------------ ------------- ---------
1 Cust 1 Done NULL
2 Cust 2 Pending NULL
3 Cust 3 Pending NULL
4 Cust 4 Done NULL
5 Cust 5 Pending NULL

Table UserRights:
-----------------
UserID CurrentStatus
------ -------------
User1 Active
User2 Active
User3 InActive


My code goes like this:

----------------------------------------------

DECLARE @ActiveCount INT
DECLARE @RequestCount INT
DECLARE @ActiveUser VARCHAR(50)

SELECT @ActiveCount = Count(CurrentStatus) FROM UserRights WHERE CurrentStatus = 'Active'

SELECT @RequestCount = Count(RequestID) FROM dbo.MITagging WHERE (MITaggingStatus = 'Pending')

If ((@ActiveCount > 0) AND (@RequestCount > 0))
Begin
SELECT @ActiveUser = UserID FROM UserRights WHERE CurrentStatus = 'Active'
UPDATE TOP(@RequestCount/@ActiveCount) MITagging SET AssignedTo = @ActiveUser WHERE (MITaggingStatus = 'Pending')
End

-----------------------------

This code will always pick same user name in @ActiveUser and update same rows each time we run it. Please suggest a way to pick each Active user in @ActiveUser (in a loop or some other way)

Any help would be greatly appreciated.

Thanks & Regards,

Z@in

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-06 : 16:58:14
You need to provide more info...you really don't likely need a loop for this. A set based approach would likely be feasible.

Please provide DDL for structure of table, sample data and sample results.

is this code running in a stored procedure? a trigger? an agent job? this stuff matters.



Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

HeyZain
Starting Member

7 Posts

Posted - 2011-02-06 : 17:08:25
Dear DataGuru1971, I have posted sample data with table structures

Z@in
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-06 : 17:34:13
Not quite...

this is what I meant, so we wouldn't have to write it ...

DECLARE @MITagging Table
( ReqID int not null,
CustomerName char(6) not null,
MITaggingStatus varchar(9) not null,
AssignedTo varchar(5) null)

INSERT INTO @MITagging
SELECT 1 ,'Cust 1', 'Done', NULL UNION
SELECT 2 ,'Cust 2', 'Pending', NULL UNION
SELECT 3 ,'Cust 3', 'Pending', NULL UNION
SELECT 4 ,'Cust 4', 'Done', NULL UNION
SELECT 5 ,'Cust 5', 'Pending', NULL

DECLARE @UserRights Table ( UserID varchar(5) not null, CurrentStatus varchar(8) not null)

INSERT INTO @UserRights
SELECT 'User1', 'Active' UNION
SELECT 'User2', 'Active' UNION
SELECT 'User3', 'InActive'

SELECT UserID, UserRow = ROW_NUMBER() OVER( PARTITION BY CurrentStatus ORDER BY CurrentStatus)
FROM @UserRights
Where CurrentStatus = 'Active'

SELECT REQID, ReqRow = ROW_NUMBER() OVER (PARTITION BY MITaggingStatus ORDER BY MITaggingstatus)
FROM @MITagging
Where MITaggingStatus = 'Pending'
------------------------------------------
;WITH cteActiveUsers AS
( SELECT UserID, UserRow = ROW_NUMBER() OVER( PARTITION BY CurrentStatus ORDER BY CurrentStatus)
FROM @UserRights
Where CurrentStatus = 'Active' )


, ctePending AS
( SELECT REQID, ReqRow = ROW_NUMBER() OVER (PARTITION BY MITaggingStatus ORDER BY MITaggingstatus)
FROM @MITagging
Where MITaggingStatus = 'Pending' )

UPDATE t
SET AssignedTo = cteActiveUsers.UserID
,MITaggingStatus = 'Done'
FROM cteActiveUsers
join ctePending on UserRow = ReqRow
join @MITagging t on t.ReqID = ctePending.REQID
WHERE MITaggingStatus = 'Pending'

SELECT * FROM @MITagging




What would the desired results be from that ? I am not sure what results you want, but I think the results of the above match....correct?





Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page
   

- Advertisement -