I'm trying to figure out a better and more efficient way to write the script below. Can anyone think of a way to accomplish the same goal without using a cursor?
The "User" may appear multiple times in table1 one but can only exist once in table2.
TABLE1
|Name |Access |
-------------------
User1 |N |
User1 |N |
User1 |Y |
TABLE2
|Name |Access |
-------------------
User1 | |
User2 | |
User3 | |
Code:
DECLARE @Name VarChar(50), @Access VarChar(1)
DECLARE TestCursor CURSOR FOR
SELECT Name, Access FROM Table1 ORDER BY Obj ASC
OPEN TestCursor
FETCH NEXT FROM TestCursor INTO @Name, @Access
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE table2
SET Table2.Access = CASE
WHEN Table1.Access = 'Y' THEN Table1.Access
ELSE Table2.Access END
FROM table1
JOIN table2 ON table1.name = table2.name
FETCH NEXT FROM TestCursor INTO @Name, @Access
END
CLOSE TestCursor
DEALLOCATE TestCursor