存储过程无法编译

时间:2014-06-24 12:39:57

标签: sql sql-server sql-server-2008 stored-procedures

我编写了这个存储过程并知道那里的各个查询是否正常工作。我刚在公司实施了一个用于异常流程的标准化模板。

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".

我将整个存储过程粘贴在那里,有人知道什么是错的吗?我应该宣布什么吗?

1 个答案:

答案 0 :(得分:3)

您的存储过程中间有一个GO语句。删除它,你应该是好的。