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 2008 Forums
 Transact-SQL (2008)
 Why work this Code without Error?

Author  Topic 

gpc44
Starting Member

35 Posts

Posted - 2014-06-22 : 01:48:41
Hi,
the Select for the cursor uses the same table as the update statement in the cursor. If the data set by the cursor Reading not be locked for update statement?
Regards
Nicole

DECLARE @pkPerson INT
DECLARE myCur CURSOR
LOCAL
FORWARD_ONLY
FAST_FORWARD
READ_ONLY
TYPE_WARNING
FOR SELECT pkPerson
FROM dbo.tEmployee
OPEN myCur ;
FETCH NEXT FROM myCur INTO @pkPerson
WHILE @@FETCH_STATUS = 0
BEGIN;
UPDATE dbo.tEmployee
SET ActiveRoles = dbo.udf_GetRoles(@pkPerson)
WHERE pkPerson = @pkPerson

FETCH NEXT FROM myCur INTO @pkPerson
END ;
CLOSE myCur ;
DEALLOCATE myCur ;

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-06-22 : 03:20:34
You don't need a cursor for this
UPDATE	dbo.tEmployee 
SET ActiveRoles = dbo.udf_GetRoles(pkPerson);



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -