This SQL statement is not grabbing s.badge_no and joining it.
I want to match the "s.lname" field using the "like" operator to the "n.name" field and join n.badge_no to the corresponding row in name_ID.
I want to match the "s.lname" field using the "like" operator to the "n.name" field and join n.badge_no to the corresponding row in name_ID.
Code:
DELETE FROM stemp
DECLARE
@Id integer,
@name varchar(50),
@password varchar(50),
@dept varchar(50),
@division char(10),
@active bit,
@badge_no char(10)
DECLARE S_CURSOR CURSOR FOR
SELECT n.Id, n.name, n.password, n.dept, n.division, n.active, s.badge_no
FROM nameid n
JOIN server.dbo.employees s ON n.name LIKE '%' + s.lname + '%'
OPEN S_CURSOR FETCH NEXT FROM S_CURSOR
INTO @Id, @name, @password, @dept, @division, @active, @badge_no WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM S_CURSOR INTO @Id, @name, @password, @dept, @division, @active, @badge_no
INSERT INTO stemp(ID, name, password, dept, division, active, badge_no)
VALUES (@ID, @name, @password, @dept, @division, @active, @badge_no)
WHERE NOT EXISTS (SELECT i.ID FROM nameID i WHERE i.ID = @Id)
END
CLOSE S_CURSOR
DEALLOCATE S_CURSOR