关键字IF dynamic sql附近的语法不正确

时间:2016-02-01 08:49:31

标签: sql-server visual-studio-2010 stored-procedures

我目前是sql server的新手。

我在visual studio 2010 express的帮助下使用存储过程。

当我发现这个错误时,我正在sql server中试验存储过程,这非常烦人:

关键字“IF

附近的语法不正确
UPDATE [table_name] 
   SET 
[delete_datetime] = CURRENT_TIMESTAMP, 
[delete_user_record_id] = 2 
 WHERE [table_name].[record_id] = 2

这是我的存储过程函数。它是在我当前的表上更新时间戳:

USE [MGroupIS]
GO
/****** Object:  StoredProcedure [dbo].[iSP_SET_DATA_INFORMATION]    Script Date: 02/01/2016 14:40:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_SET_DATA_INFORMATION]
    --Paramateres

AS

DECLARE @sql AS NVARCHAR(4000)
DECLARE @pFlag AS VARCHAR(6)

SET @pFlag = @flag

--IF Delete
IF @pFlag = 'DELETE'
BEGIN
    SET @sql = 'UPDATE [' + @tableName + '] ' + CHAR(13) + 
               '    SET ' + CHAR(13) + 
               ' [delete_datetime] = CURRENT_TIMESTAMP, ' + CHAR(13) +
               ' [delete_user_record_id] = ' + CAST(@userRecordID AS VARCHAR(20)) + CHAR(13) +
               ' WHERE [' + @tableName + '].[' + @keyField + '] = ' + CAST(@recordID AS VARCHAR(20))
END
ELSE
BEGIN
    SET @sql = 'UPDATE [' + @tableName + '] ' + CHAR(13) + 
               '    SET ' + CHAR(13) + 
               ' [edit_datetime] = CURRENT_TIMESTAMP, ' + CHAR(13) +
               ' [edit_user_record_id] = ' + CAST(@userRecordID AS CHAR(3)) 

END

EXEC sp_executeSQL @sql

PRINT @sql

我从另一个存储过程(ISP_CFG_ACCESS_RIGHT_QUERY)调用存储过程:

ELSE IF @action = 'SAVE'
--SAVE Action
BEGIN
    --If saved
    IF @removeUnsaved = 0
    BEGIN
         UPDATE [CFG_ACCESS_RIGHT] 
           SET [CFG_ACCESS_RIGHT].[NAME] = '' + @name + '', 
               [CFG_ACCESS_RIGHT].[NOTE] = '' + @note + ''
         WHERE [CFG_ACCESS_RIGHT].[RECORD_ID] = @recordID

     DELETE FROM [CFG_ACCESS_RIGHT_DETAIL]
     WHERE [CFG_ACCESS_RIGHT_DETAIL].[access_right_record_id] = @recordID

     SET @sql = 'INSERT INTO [CFG_ACCESS_RIGHT_DETAIL] ' + CHAR(13) + 
                'SELECT * FROM [' + @tempTable + '] ' + CHAR(13) + 
                ' WHERE [' + @tempTable + '].[delete_datetime] IS NULL;'

     EXEC sp_executeSQL @sql

     EXEC [ISP_SET_DATA_INFORMATION] 
            'CFG_ACCESS_RIGHT',
            'record_id',
            @userRecordID,
            @recordID,
            @voidStatus,
            @voidReason,
            @flag,
            0

    EXEC [ISP_SET_DATA_INFORMATION] 
            'CFG_ACCESS_RIGHT_DETAIL',
            'access_right_record_id',
            @userRecordID,
            @recordID,
            @voidStatus,
            @voidReason,
            @flag,
            0

END
--If leave without saving
ELSE
BEGIN
    EXEC [ISP_SET_DATA_INFORMATION]
            'CFG_ACCESS_RIGHT',
            'record_id',
            @userRecordID,
            @recordID,
            @voidStatus,
            @voidReason,
            'DELETE',
            0
END      

请帮忙。 先谢谢你。

编辑1:隔离潜在的有问题的部分。请帮忙

1 个答案:

答案 0 :(得分:0)

过程中的动态DROP表语句在语法上是错误的。

应该是这样的,

IF OBJECT_ID('tablename', 'U') IS NOT NULL
  DROP TABLE tablename;

除此之外,T-SQL中的语法错误也很少。我修好了。

请参阅此代码,

IF @pFlag = 'DELETE'
  BEGIN
      SET @sql = 'UPDATE [' + @tableName + '] ' + Char(13)
                 + '    SET ' + Char(13)
                 + ' [delete_datetime] = CURRENT_TIMESTAMP, '
                 + Char(13) + ' [delete_user_record_id] = '
                 + Cast(@userRecordID AS VARCHAR(20))
                 + Char(13) + ' WHERE [' + @tableName + '].['
                 + @keyField + '] = '
                 + Cast(@recordID AS VARCHAR(20))
  END
ELSE
  BEGIN
      SET @sql = 'UPDATE [' + @tableName + '] ' + Char(13)
                 + '    SET ' + Char(13)
                 + ' [edit_datetime] = CURRENT_TIMESTAMP, '
                 + Char(13) + ' [edit_user_record_id] = '
                 + Cast(@userRecordID AS CHAR(3))

      --IF New Record        
      IF @flag = 'NEW'
        BEGIN
            SET @sql = @sql + ', ' + Char(13)
                       + '[create_datetime] = CURRENT_TIMESTAMP, '
                       + Char(13) + '[create_user_record_id] = '
                       + Cast(@userRecordID AS CHAR(3))
        END

      --IF Void Record
      IF @voidStatus = 1
        BEGIN
            SET @sql = @sql + ', ' + Char(13) + '[void_status] = 1, '
                       + Char(13)
                       + '[void_datetime] = CURRENT_TIMESTAMP, '
                       + Char(13) + '[void_user_record_id] = '
                       + Cast(@userRecordID AS CHAR(3)) + ', '
                       + Char(13) + '[void_reason] = ''' + @voidReason
                       + ''''
        END
      ELSE
        BEGIN
            SET @sql = @sql + ', ' + Char(13) + '[void_status] = 0, '
                       + Char(13) + '[void_datetime] = NULL, '
                       + Char(13) + '[void_user_record_id] = '
                       + Cast(@userRecordID AS CHAR(3)) + ', '
                       + Char(13) + '[void_reason] = NULL'
        END

      --IF Print
      IF @printed = 1
        BEGIN
            SET @sql = @sql + ', ' + Char(13)
                       + '[print_count] = [print_count] + 1, '
                       + Char(13) + '[last_print_datetime] = NULL, '
                       + Char(13) + '[print_user_record_id] = 0'
                       + Cast(@userRecordID AS CHAR(3))
        END

      SET @sql = @sql + ', ' + Char(13)
                 + 'record_version = record_version + 1 '
                 + Char(13) + ' WHERE [' + @tableName + '].['
                 + @keyField + '] = '
                 + Cast(@recordID AS VARCHAR(20))
  END

EXEC Sp_executesql
  @sql

PRINT @sql 

程序代码:

ALTER PROCEDURE [dbo].[Sp_cfg_access_right_query] @recordID      INT,
                                                  @userRecordID  INT,
                                                  @action        CHAR(4),
                                                  @flag          CHAR(6),
                                                  @tempTable     VARCHAR(50),
                                                  @name          VARCHAR(50),
                                                  @note          VARCHAR(1000),
                                                  @voidStatus    BIT,
                                                  @voidReason    VARCHAR(200),
                                                  @removeUnsaved BIT
AS
    --Setting NULL values
    SET @recordID = Isnull(@recordID, 0)
    SET @userRecordID = Isnull(@recordID, 0)
    SET @action = Isnull(@action, '')
    SET @flag = Isnull(@flag, '')
    SET @tempTable = Isnull(@tempTable, '')
    SET @name = Isnull(@name, '')
    SET @note = Isnull(@note, '')
    SET @voidStatus = Isnull(@voidStatus, 0)
    SET @voidReason = Isnull(@voidReason, '')

    DECLARE @detailCount INT
    DECLARE @currentID INT
    DECLARE @sql NVARCHAR(1000)
    DECLARE @temp VARCHAR(100)
    DECLARE @tempConstraint VARCHAR(100)

    IF @action = 'LOAD'
      --LOAD action
      BEGIN
          SET @temp = 'CFG_TEMP_ARD_'
                      + Replace(Cast(Newid() AS VARCHAR(50)), '-', '')
          SET @tempConstraint = Cast(Year(CURRENT_TIMESTAMP) AS VARCHAR(4))
                                + Cast(Month(CURRENT_TIMESTAMP) AS VARCHAR(2))
                                + Cast(Day(CURRENT_TIMESTAMP) AS VARCHAR(2))
                                + Cast(Datepart(HOUR, CURRENT_TIMESTAMP) AS VARCHAR(2))
                                + Cast(Datepart(MINUTE, CURRENT_TIMESTAMP) AS VARCHAR(2))
                                + Cast(Datepart(SECOND, CURRENT_TIMESTAMP) AS VARCHAR(2))
                                + Cast(Datepart(MILLISECOND, CURRENT_TIMESTAMP) AS VARCHAR(3))
          SET @sql = 'SELECT * INTO [' + @temp
                     + '] FROM [CFG_ACCESS_RIGHT_DETAIL] WHERE 1=0;'

          EXEC Sp_executesql
            @sql

          SET @sql = 'ALTER TABLE [' + @temp + '] ' + Char(13)
                     + 'ADD CONSTRAINT PK_' + @tempConstraint
                     + Char(13)
                     + 'PRIMARY KEY ([record_id], [access_right_record_id]);'

          EXEC Sp_executesql
            @sql

          IF @flag = 'NEW'
            BEGIN
                INSERT INTO [CFG_ACCESS_RIGHT]
                            (NAME,
                             note,
                             record_version)
                VALUES      ('',
                             '',
                             1)

                SELECT @recordID = Scope_identity()
                FROM   [CFG_ACCESS_RIGHT];
            END

          SELECT @recordID currentID,
                 @temp     tempTable

          --Load Access Right
          SELECT *
          FROM   [CFG_ACCESS_RIGHT]
          WHERE  [record_id] = @recordID

          --Check & Return Detail Count
          SELECT @detailCount = @@ROWCOUNT
          FROM   [CFG_ACCESS_RIGHT_DETAIL]
          WHERE  [access_right_record_id] = @recordID

          SELECT Isnull(@detailCount, 0) detailCount

          --Load Access Right Details if exist
          IF @detailCount > 0
            BEGIN
                SELECT *
                FROM   [CFG_ACCESS_RIGHT_DETAIL]
                WHERE  [access_right_record_id] = @recordID
                       AND [delete_datetime] IS NULL
            END
      END
    ELSE IF @action = 'SAVE'
      --SAVE Action
      BEGIN
          --If saved
          IF @removeUnsaved = 0
            BEGIN
                UPDATE [CFG_ACCESS_RIGHT]
                SET    [CFG_ACCESS_RIGHT].[NAME] = '' + @name + '',
                       [CFG_ACCESS_RIGHT].[NOTE] = '' + @note + ''
                WHERE  [CFG_ACCESS_RIGHT].[RECORD_ID] = @recordID

                DELETE FROM [CFG_ACCESS_RIGHT_DETAIL]
                WHERE  [CFG_ACCESS_RIGHT_DETAIL].[access_right_record_id] = @recordID

                SET @sql = 'INSERT INTO [CFG_ACCESS_RIGHT_DETAIL] '
                           + Char(13) + 'SELECT * FROM [' + @tempTable + '] '
                           + Char(13) + ' WHERE [' + @tempTable
                           + '].[delete_datetime] IS NULL;'

                EXEC Sp_executesql
                  @sql

                EXEC [Sp_set_data_information]
                  'CFG_ACCESS_RIGHT',
                  'record_id',
                  @userRecordID,
                  @recordID,
                  @voidStatus,
                  @voidReason,
                  @flag,
                  0

                EXEC [Sp_set_data_information]
                  'CFG_ACCESS_RIGHT_DETAIL',
                  'access_right_record_id',
                  @userRecordID,
                  @recordID,
                  @voidStatus,
                  @voidReason,
                  @flag,
                  0
            END
          --If leave without saving
          ELSE
            BEGIN
                EXEC [Sp_set_data_information]
                  'CFG_ACCESS_RIGHT',
                  'record_id',
                  @userRecordID,
                  @recordID,
                  @voidStatus,
                  @voidReason,
                  'DELETE',
                  0
            END
      END

    SET @sql ='IF OBJECT_ID(''' + @tempTable
              + ''', ''U'') IS NOT NULL
  DROP TABLE ' + @tempTable

    EXEC Sp_executesql
      @sql