我有 3 个表,我必须检查祖父表 id 是否有孙表中的记录。如果是,则返回是,否则返回否。这是我的存储过程。我收到了不正确的语法错误。我是存储过程的新手。请帮我。
CREATE PROCEDURE P_Check
@PKG_ID INT,
@S_ID INT,
@FLAG VCHAR(10) OUT
DECLARE IDS CURSOR LOCAL FOR SELECT S_ID FROM T1 WHERE P_ID = @PKG_ID
OPEN IDS
FETCH NEXT FROM IDS into @S_ID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT * FROM T2 WHERE S_ID = @S_ID
IF @@ROWCOUNT<>0
@FLAG = 'YES'
RETURN
ELSE
@FLAG = 'NO'
FETCH NEXT FROM IDS into @S_ID
END
CLOSE IDS
DEALLOCATE IDS