我試圖刪除存儲過程(如果存在),然後重新創建它,所有內部事務。SQL Server:不能在事務中刪除和創建SP
BEGIN TRANSACTION
BEGIN TRY
IF OBJECT_ID(N'dbo.GET_DATA', N'P') IS NOT NULL
BEGIN
DROP PROCEDURE [dbo].[GET_DATA]
END
CREATE PROCEDURE [dbo].[GET_DATA]
@date datetime2
AS
SET NOCOUNT ON
BEGIN
SELECT
dbo.Products.product_cod AS 'product_cod',
dbo.Product_Types.name AS 'product_type_name',
dbo.UM.name AS 'um_name',
dbo.Products.category_id AS 'category_id',
dbo.Bins_Products.bin_id AS 'product_bin_id'
FROM dbo.Products
LEFT JOIN dbo.Product_Types on Products.product_type_id = Product_Types.product_type_id
LEFT JOIN dbo.UM on Products.um_id = UM.um_id
LEFT JOIN Bins_Products ON Bins_Products.product_id = Products.product_id
WHERE
Products.update_date >= @date
END
COMMIT TRANSACTION
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS 'ErrorMessage'
ROLLBACK TRANSACTION
END CATCH
當我運行上面的腳本,我得到以下錯誤:
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'PROCEDURE'.
Msg 137, Level 15, State 2, Line 31
Must declare the scalar variable "@date".
而且我對SET和@date波浪線。
IF語句和create語句都可以自行工作。
您在創建過程之前缺少GO語句,並且無法使用try/catch塊。 –
'CREATE PROCEDURE'和'DROP PROCEDURE'不能以事務方式完成。相反,如果它還不存在,則創建一個空過程('CREATE PROCEDURE GET_DATA AS BEGIN RETURN END'),然後無條件地創建'ALTER'。 –
@DeanSavović我嘗試使用GO,但只會產生其他錯誤。 – dmdany07