0
我寫了這個存儲過程,並知道那裏的單個查詢工作。我只在公司內部實施了一個用於異常流程的標準化模板。存儲過程不編譯
USE [DEV_SERV]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [sch].[usp_tmSetstat]
@stat VARCHAR(50),
@statDesc VARCHAR(50),
@Parent_stat VARCHAR(50),
@Status SMALLINT
AS
BEGIN
SET NOCOUNT ON
DECLARE @Parent HIERARCHYID
DECLARE @Sibling HIERARCHYID
DECLARE @OldParent HIERARCHYID
DECLARE @NewParent HIERARCHYID
DECLARE @CurrentPos HIERARCHYID
DECLARE @StatusInactiveKey SMALLINT
BEGIN TRY
IF EXISTS (SELECT 1 FROM [sch].[stat_path] WHERE [stat_code] = @stat)
BEGIN
SET @OldParent = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat]
WHERE stat_code =
(SELECT s.parent_stat_code
FROM [sch].[stat_path] s WHERE s.stat_code = @stat))
SET @NewParent = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat]
WHERE t.stat_code = @Parent_stat);
SET @CurrentPos = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat]
WHERE t.stat_code = @stat);
UPDATE [sch].[stat] SET
stat_path = @CurrentPos.GetReparentedValue(@OldParent, @NewParent)
WHERE stat_key = @stat
GO
END
ELSE
BEGIN
SET @Parent = (SELECT [stat_path] AS tmpPath
FROM [sch].[stat] t
WHERE t.stat_code = @Parent_stat)
SET @Sibling = (SELECT TOP 1 [stat_path]
FROM [sch].[stat] t
WHERE parent_stat_code = @Parent_stat
ORDER BY t.stat_key DESC)
SET @StatusInactiveKey = [dbo].[udf_GetStatusKey]('Active')
INSERT INTO [sch].[stat]
([stat_code],
[stat_desc],
[stat_path],
[point_type],
[status_key])
VALUES (@stat,
@statDesc,
@Parent.GetDescendant(@Sibling, NULL),
'T',
@StatusInactiveKey)
GO
END
END TRY
BEGIN CATCH
END CATCH
END
但是,我得到這些惱人的錯誤,就像;
Msg 102, Level 15, State 1, Procedure usp_tmSetstat, Line 60
Incorrect syntax near '@stat'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'ELSE'.
Msg 137, Level 15, State 2, Line 7
Must declare the scalar variable "@Parent_stat".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "@Parent_stat".
Msg 137, Level 15, State 1, Line 14
Must declare the scalar variable "@StatusInactiveKey".
Msg 137, Level 15, State 2, Line 24
Must declare the scalar variable "@stat".
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'END'.
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "@stat".
Msg 137, Level 15, State 2, Line 7
Must declare the scalar variable "@errTemplate".
Msg 137, Level 15, State 2, Line 8
Must declare the scalar variable "@errTemplate".
Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "@errTemplate".
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "@errTemplate".
Msg 137, Level 15, State 2, Line 11
Must declare the scalar variable "@errTemplate".
Msg 137, Level 15, State 1, Line 12
Must declare the scalar variable "@errProcedure".
Msg 137, Level 15, State 1, Line 13
Must declare the scalar variable "@errNumber".
Msg 137, Level 15, State 1, Line 14
Must declare the scalar variable "@errLine".
Msg 137, Level 15, State 1, Line 15
Must declare the scalar variable "@errSeverity".
Msg 137, Level 15, State 1, Line 16
Must declare the scalar variable "@errState".
Msg 137, Level 15, State 2, Line 18
Must declare the scalar variable "@errProcedure".
Msg 137, Level 15, State 2, Line 20
Must declare the scalar variable "@errTemplate".
Msg 137, Level 15, State 2, Line 25
Must declare the scalar variable "@RetVal".
我粘貼了我整個存儲過程在那裏,有人知道什麼是錯的嗎?我應該宣佈什麼?
因此,我應該在哪裏放置go語句,然後如果我想要更改提交? – Ramie
呃 - 我認爲你誤解了GO語句的作用。基本上,SQL Server Management Studio將您的代碼分批發送到SQL Server。 'GO'表示批次結束。您無法跨多個批次拆分存儲過程。一般來說,在BEGIN-END構造中永遠不會有'GO'語句。 – Dan
如果您試圖控制如何以及何時向表提交數據,則必須使用[transactions](http://msdn.microsoft.com/zh-cn/library/ms188929(v = sql.105).aspx) (BEGIN TRANS/COMMIT TRANS)。 – Dan