解析sql server

时间:2018-04-11 12:45:18

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

在sql server中创建存储过程时,我在斜体中收到以下错误(粗体),

消息156,级别15,状态1,过程IATF_upload_exce,第46行 关键字' AS'附近的语法不正确。 消息156,级别15,状态1,过程IATF_upload_exce,第64行 关键字' SET'附近的语法不正确。 消息102,级别15,状态1,过程IATF_upload_exce,第65行 ','附近的语法不正确。 消息102,级别15,状态1,过程IATF_upload_exce,102行 ','

附近的语法不正确

已存储的程序

**-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Shivam_nxf30130
-- Create date: 11April2018
-- Description: IATF_upload_excel
-- =============================================
CREATE PROCEDURE IATF_upload_exce 
    -- Add the parameters for the stored procedure here
    --@p1 int = 0, 
    --@p2 int = 0
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    DECLARE @name VARCHAR(50) -- database name 
--DECLARE @path VARCHAR(256) -- path for backup files 
--DECLARE @fileName VARCHAR(256) -- filename for backup 
--DECLARE @fileDate VARCHAR(20) -- used for file name 
--SET @path = 'C:\Backup\' 
--SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 
DECLARE db_cursor CURSOR FOR 
(SELECT 
                            AU.id                       AS audit_id,
                            AT.id                       AS audittype_id,
                            CC.id                       AS CausingClause_id,
                            IA.[Finding]                AS findings,
                            IA.[Rating]                 AS Rating,
                            IA.[Reach(L/R/G)]           AS lr_ind,
                            IA.[Root_Cause]             AS root_cause,
                            IA.[Corrective_Action]      AS corr_action,
                            IA.[Action_Owner]           AS by_whom,
                            IA.[Remark_Verification]    AS remarks,
                            IA.[Links]                  AS links,
                            IA.[Close Date]             AS close_date
                    FROM    [dbo].[IMPORT_AUDIT_REPORTS_EXCEL] AS IA,
                            [dbo].[Causing_Clause] AS CC,
                            [dbo].[Audit_Type] AS AT,                       
                            [dbo].[Audits] AS AU
                    WHERE   IA.ID           = CC.name
                    AND     IA.Audit_Type   = AT.name
                    AND     IA.Audit_No     = AU.id) AS Src
            --ON        Src.ID                  = dbo.AUDIT_REPORTS .id
            --AND       Src.audittype_id        = dbo.AUDIT_REPORTS .audittype_id
            --AND       Src.CausingClause_id    = dbo.AUDIT_REPORTS .CausingClause_id
OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  
WHILE @@FETCH_STATUS = 0  
BEGIN  
      ---SET @fileName = @path + @name + '_' + @fileDate + '.BAK' 
      --BACKUP DATABASE @name TO DISK = @fileName 
      --FETCH NEXT FROM db_cursor INTO @name 
      if(Src.ID                 = dbo.AUDIT_REPORTS .id
            AND     Src.audittype_id        = dbo.AUDIT_REPORTS .audittype_id
            AND     Src.CausingClause_id    = dbo.AUDIT_REPORTS .CausingClause_id)

            UPDATE SET  dbo.AUDIT_REPORTS .[audit_id]           = Src.audit_id,
                dbo.AUDIT_REPORTS .[category_id]        = (SELECT id FROM [dbo].CATEGORIES AS CA WHERE CA.name = Src.Rating),
                dbo.AUDIT_REPORTS .[ref_id]         = 0,
                dbo.AUDIT_REPORTS .[ref_ident]          = 'N/A',
                dbo.AUDIT_REPORTS .[findings]           = Src.findings,
                dbo.AUDIT_REPORTS .[corr_action]        = Src.corr_action,
                dbo.AUDIT_REPORTS .[lr_ind]         = Src.lr_ind,
                dbo.AUDIT_REPORTS .[by_whom]            = Src.by_whom,
                dbo.AUDIT_REPORTS .[remarks]            = Src.remarks,
                dbo.AUDIT_REPORTS .[due_date]           = NULL,
                dbo.AUDIT_REPORTS .[close_date]     = Src.close_date,
                dbo.AUDIT_REPORTS .[audittype_id]       = Src.audittype_id,
                dbo.AUDIT_REPORTS .[root_cause]     = Src.root_cause,
                dbo.AUDIT_REPORTS .[links]              = Src.links,
                dbo.AUDIT_REPORTS .[CausingClause_id]   = Src.CausingClause_id
    Else
INSERT
    (
        [audit_id],
        [category_id],
        [ref_id],
        [ref_ident],
        [findings],
        [corr_action],
        [lr_ind],
        [by_whom],
        [remarks],
        [due_date],
        [close_date],
        [audittype_id],
        [root_cause],
        [links],
        [CausingClause_id]
    )
    VALUES
    (
        Src.audit_id,
        (SELECT id FROM [dbo].CATEGORIES AS CA WHERE CA.name = Src.Rating),
        0,
        'N/A',
        Src.findings,
        Src.corr_action,
        Src.lr_ind,
        Src.by_whom,
        Src.remarks,
        NULL,
        Src.close_date,
        Src.audittype_id,
        Src.root_cause,
        Src.links,
        Src.CausingClause_id
    );  

END 
CLOSE db_cursor  
DEALLOCATE db_cursor 
END
GO**

请帮我解决问题。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可能想要检查UPDATE的语法。你需要这样的东西:

UPDATE table
SET col1 = ..., col2 = ...

您的更新语句缺少表引用。仅列出3部分列名称是不够的(无论如何这都不是好的做法。)

FWIW,我认为你可以使用MERGE更简单地做到这一点。值得一试。

答案 1 :(得分:0)

现在可以检查我的程序是对还是错。

ALTER PROCEDURE IATFUPLOAD 
    -- Add the parameters for the stored procedure here
    --@p1 int = 0, 
    --@p2 int = 0

AS
BEGIN

    DECLARE @audit_id int, @audittype_id int, @CausingClause_id int
    declare @findings varchar(500), @Rating varchar(500), @lr_ind varchar(500), @root_cause varchar(500), @corr_action varchar(500), @by_whom varchar(500), @remarks varchar(500), @links varchar(500)
    declare @close_date datetime
    declare @count int
    set @count = 0
    declare @category_id_insert int

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    --SELECT @p1, @p2
    DECLARE db_cursor CURSOR FOR
                            SELECT  
                            AU.id                       AS audit_id,
                            AT.id                       AS audittype_id,
                            CC.id                       AS CausingClause_id,
                            IA.[Finding]                AS findings,
                            IA.[Rating]                 AS Rating,
                            IA.[Reach]          AS lr_ind,
                            IA.[Root_Cause]             AS root_cause,
                            IA.[Corrective_Action]      AS corr_action,
                            IA.[Action_Owner]           AS by_whom,
                            IA.[Remark_Verification]    AS remarks,
                            IA.[Links]                  AS links,
                            IA.[Close_Date]             AS close_date
                    FROM    [dbo].[IMPORT_AUDIT_REPORTS_EXCEL] AS IA,
                            [dbo].[CausingClause] AS CC,
                            [dbo].[Audit_Type] AS AT,                       
                            [dbo].[Audits] AS AU
                    WHERE   IA.ID           = CC.name
                    AND     IA.Audit_Type   = AT.name
                    AND     IA.Audit_No     = AU.id;

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @audit_id, @audittype_id,
@CausingClause_id,
@findings,
@Rating,
@lr_ind,
@root_cause,
@corr_action,
@by_whom,
@remarks,
@links, 
@close_date

WHILE @@FETCH_STATUS = 0  
BEGIN  
    SET @category_id_insert=(SELECT id FROM [dbo].CATEGORIES AS CA WHERE CA.name = @Rating)

   SET @COUNT = (select count(1)  from [dbo].[AUDIT_REPORTS] as Src where 
                    Src.ID= @audit_id
            AND     Src.audittype_id        = @audittype_id
            AND     Src.CausingClause_id    = @CausingClause_id)
    --PRINT @COUNT;

    if @COUNT>0
    UPDATE dbo.AUDIT_REPORTS SET    dbo.AUDIT_REPORTS .[audit_id]           = @audit_id,
                dbo.AUDIT_REPORTS .[category_id]        = (SELECT id FROM [dbo].CATEGORIES AS CA WHERE CA.name = @Rating),
                dbo.AUDIT_REPORTS .[ref_id]         = 0,
                dbo.AUDIT_REPORTS .[ref_ident]          = 'N/A',
                dbo.AUDIT_REPORTS .[findings]           = @findings,
                dbo.AUDIT_REPORTS .[corr_action]        = @corr_action,
                dbo.AUDIT_REPORTS .[lr_ind]         = @lr_ind,
                dbo.AUDIT_REPORTS .[by_whom]            = @by_whom,
                dbo.AUDIT_REPORTS .[remarks]            = @remarks,
                dbo.AUDIT_REPORTS .[due_date]           = NULL,
                dbo.AUDIT_REPORTS .[close_date]     = @close_date,
                dbo.AUDIT_REPORTS .[audittype_id]       = @audittype_id,
                dbo.AUDIT_REPORTS .[root_cause]     = @root_cause,
                dbo.AUDIT_REPORTS .[links]              = @links,
                dbo.AUDIT_REPORTS .[CausingClause_id]   =@CausingClause_id
    Else

INSERT into dbo.AUDIT_REPORTS
    (
        [audit_id],
        [category_id],
        [ref_id],
        [ref_ident],
        [findings],
        [corr_action],
        [lr_ind],
        [by_whom],
        [remarks],
        [due_date],
        [close_date],
        [audittype_id],
        [root_cause],
        [links],
        [CausingClause_id]
    )
    VALUES
    (
        @audit_id,
        --0,
        @category_id_insert,
        0,
        'N/A',
        @findings,
        @corr_action,
        @lr_ind,
        @by_whom,
        @remarks,
        NULL,
        @close_date,
        @audittype_id,
        @root_cause,
        @links,
        @CausingClause_id
    );  
END 

CLOSE db_cursor  
DEALLOCATE db_cursor                    


END
GO