2017-01-13 58 views
0

我有一個存儲過程來更新單獨的表中的記錄。首先爲此任務刪除與該主鍵相關的所有記錄。然後運行插入查詢。但是當我這樣做時,我在SQL Server中出現錯誤。如何在刪除具有相同主鍵的行後將數據插入到表中?

這裏是存儲過程

ALTER PROCEDURE [dbo].[update_HS_HR_LANG] 
    @appNo Int, 
    @LCODE VARCHAR(30), 
    @ABILITY int 
AS 
BEGIN 
    DELETE FROM [HS_HR_LANG] 
    WHERE App_no = @appNo 
END 
BEGIN 
    INSERT INTO [HS_HR_LANG] ([App_no], [lang_code], [ability_type]) 
    VALUES (@appNo, @LCODE, @ABILITY) 
END        

這是錯誤我得到

必須聲明標量變量 「@appNo」

+2

中間 – artm

+0

@artm它的工作取下'結束begin' – Mike

回答

1

你的存儲過程到此結束:

ALTER PROCEDURE [dbo].[update_HS_HR_LANG] 
@appNo Int, 
@LCODE VARCHAR(30), 
@ABILITY int 

AS 

BEGIN 

DELETE 
FROM [HS_HR_LANG] 
WHERE [email protected] 

END -- <-----HERE 

您稍後訪問@appNo,以便您收到錯誤消息。

因此,你應該有這個代替:

ALTER PROCEDURE [dbo].[update_HS_HR_LANG] 
@appNo Int, 
@LCODE VARCHAR(30), 
@ABILITY int 

AS 

BEGIN -- <----- START 

DELETE 
FROM [HS_HR_LANG] 
WHERE [email protected] 

insert into [HS_HR_LANG] 
([App_no] 
,[lang_code] 
,[ability_type]) 
VALUES 
     (@appNo, 
     @LCODE, 
     @ABILITY) 
END -- <----- END 
0
ALTER PROCEDURE [dbo].[update_HS_HR_LANG] 
@appNo Int, 
@LCODE VARCHAR(30), 
@ABILITY int 

AS 

BEGIN --<--start begin from here and remove the begin,end in the middle--- 

DELETE 
FROM [HS_HR_LANG] 
WHERE [email protected] 

insert into [HS_HR_LANG] 
([App_no] 
,[lang_code] 
,[ability_type]) 
VALUES 
    (@appNo, 
    @LCODE, 
    @ABILITY) 
    END -- <--------- 
相關問題