I need to make this query without a cursor, but how?
My boss said not to use cursors, why not? How can this be written without one?
I created a table called tbl_StudentCoverage that has SSN varchar(9) and fklogin int.
I also created a table Mstr_tbl_login that has an intID int that is a foreign key to fklogin in tbl_StudentCoverage, and strFour varchar(4).
I want to update strFour in Mstr_tbl_login with the last four of the SSN in tbl_StudentCoverage matching intID to fklogin.
I'm thinking my boss doesn't know SQL or he doesn't understand what I need to do. I consider myself an experienced SQL person.
USE mstr_Database
GO
DECLARE @fkLogin int
DECLARE @ssn varchar(9)
DECLARE NW4 CURSOR FOR
SELECT fkLogin, SSN FROM mstr.tbl_StudentCoverage
OPEN NW4
FETCH NEXT FROM NW4
INTO @fkLogin, @ssn
WHILE (@@FETCH_STATUS = 0)
BEGIN
IF (SELECT intID FROM mstr.mstr_tbl_Login WHERE intID = @fkLogin) = @fkLogin
BEGIN
UPDATE mstr.mstr_tbl_Login SET strFour = right(@ssn,4) WHERE intID = @fkLogin
END
FETCH NEXT FROM NW4
INTO @fkLogin, @ssn
END
print 'done'
CLOSE NW4
DEALLOCATE NW4
GO
No errors, this works perfect. Just my boss wants no cursors.