SQL插入 - 关键字“AS”附近的语法不正确

时间:2018-03-26 05:38:15

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

我在创建触发器以从更新/插入行复制数据时遇到问题,所以我想我可以通过向存储过程添加额外的代码来做同样的事情。

我在尝试保存错误时一直遇到错误:

  

Msg 156,Level 15,State 1,Procedure usp_updateNexStep,Line 33 [Batch Start Line 7]
  关键字'AS'.it

附近的语法不正确

代码:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [updateNexStep]
    @p1 DATETIME,           --follow up date
    @p2 CHAR(2),            -- Next step code
    @p3 VARCHAR(255),       -- comments
    @p4 CHAR(2),            -- last EMCODE
    @p5 SMALLINT,           -- user id of who made the change
    @p6 INT                 -- Record to be updated
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE table1
    SET dFollowUp       = @p1, 
        cNextStepCode   = @p2,
        cComments       = @p3, 
        cEmCode         = @p4, 
        nUserId         = @p5, 
        dUpDated        = GETDATE()
    WHERE nIDNo = @p6;
END

IF (@@ROWCOUNT > 0 ) 
BEGIN
    INSERT INTO table2 AS d
        (d.cTicketNo, d.cSystem, d.cNextStepCode, d.dFollowUp, d.cComments, d.cEmCode, d.nUserId, d.dUpDated)
        (SELECT s.cTicketNo ,s.cSystem ,s.cNextStepCode, s.dFollowUp, s.cComments, s.cEmCode, s.nUserId, s.dUpDated 
         FROM table3 AS s
         WHERE s.nIDNo = @p6);
END

1 个答案:

答案 0 :(得分:1)

通常,INSERT不需要AS关键字,

删除下面的别名:

USE [Reports]
GO
/****** Object:  StoredProcedure [dbo].[usp_updateNexStep]    Script Date: 3/26/2018 1:08:09 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [updateNexStep]
    -- Add the parameters for the stored procedure here
    @p1 DATETIME,           --follow up date
    @p2 CHAR(2),            -- Next step code
    @p3 VARCHAR(255),       -- comments
    @p4 CHAR(2),            -- last EMCODE
    @p5 SMALLINT,           -- user id of who made the change
    @p6 INT                 -- Record to be updated
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    UPDATE table1
    SET dFollowUp       = @p1, 
        cNextStepCode   = @p2,
        cComments       = @p3, 
        cEmCode         = @p4, 
        nUserId         = @p5, 
        dUpDated        = GETDATE()
    WHERE nIDNo = @p6;
END
IF (@@ROWCOUNT > 0 ) 
    BEGIN
        INSERT INTO table2 
        (dcTicketNo ,cSystem, cNextStepCode, dFollowUp, cComments, cEmCode, nUserId, dUpDated)
        (SELECT s.cTicketNo ,s.cSystem ,s.cNextStepCode, s.dFollowUp, s.cComments, s.cEmCode, s.nUserId, s.dUpDated 
            FROM table3 AS s
            WHERE  s.nIDNo = @p6);
    END