SQL 2008设置变量选择语句错误

时间:2016-07-04 11:49:26

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

存储过程中的变量存在问题。

我一直收到错误:

Msg 156, Level 15, State 1, Procedure SP_ProjectData, Line 6
Incorrect syntax near the keyword 'SELECT'.
Msg 137, Level 15, State 1, Procedure SP_ProjectData, Line 6
Must declare the scalar variable "@CurrentVersion".

第二个变量(@CurrentVersion)是问题所在。我最初创建的程序没有那个,它很好。现在我需要更新它,但它不会接受我尝试使用select语句设置其值的第二个变量。

我一直在使用这里的帖子和各种其他网站尝试“设置”,“选择”在游戏中使用格式,但却什么都没有。

这是完整的程序,任何帮助都非常受欢迎。

  USE [ic_intranet]
    GO
    /****** Object:  StoredProcedure [dbo].[SP_ProjectData]    Script Date: 07/04/2016 12:29:06 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[SP_ProjectData]
        @ProjectRef int,
        @CurrentVersion decimal(4, 2)

        SELECT @CurrentVersion = VersionNumber FROM t_ProjectEquipmentVersions WHERE CurrentVersion = 'true' and JobNumber = @ProjectRef

    AS

    SELECT
    t_Projects.JobNumber,
    t_Projects.JobName,
    t_Projects.ProjectType,
    t_Projects.ProjectStatus,
    t_Projects.JobCode,
    t_Projects.TargetCompleteDate,
    t_Projects.ActualFinishDate,
    t_Projects.LocationCountry,
    t_Projects.LocationTown,
    t_Projects.SoldDate,
    (SELECT SUM(ProjectGBPCost) FROM dbo.t_ProjectEquipment WHERE (JobNumber = @ProjectRef)) AS TotalValueBuy, 
    (SELECT SUM(ProjectGBPRetail) FROM dbo.t_ProjectEquipment WHERE (JobNumber = @ProjectRef)) AS TotalValueSell,
    (SELECT COUNT(Ordered) FROM dbo.t_ProjectEquipment WHERE (Ordered = '1' and JobNumber = @ProjectRef) GROUP BY Ordered) AS OrderedItems,
    (SELECT SUM(ProjectGBPCost) FROM dbo.t_ProjectEquipment WHERE (Ordered = '1' and JobNumber = @ProjectRef)) AS OrderedValueBuy,
    (SELECT SUM(ProjectGBPRetail) FROM dbo.t_ProjectEquipment WHERE (Ordered = '1' and JobNumber = @ProjectRef)) AS OrderedValueSell,
    (SELECT COUNT(Ordered) FROM dbo.t_ProjectEquipment WHERE (Ordered = '0' and JobNumber = @ProjectRef) GROUP BY Ordered) AS NotOrderedItems,
    (SELECT SUM(ProjectGBPCost) FROM dbo.t_ProjectEquipment WHERE (Ordered = '0' and JobNumber = @ProjectRef)) AS NotOrderedValueBuy,
    (SELECT SUM(ProjectGBPRetail) FROM dbo.t_ProjectEquipment WHERE (Ordered = '0' and JobNumber = @ProjectRef)) AS NotOrderedValueSell,
    t_EmployeeProjectManager.Firstname AS ProjectManagerFirstname, 
    t_EmployeeProjectManager.Surname AS ProjectManagerSurname,
    t_EmployeeLeadEngineer.Firstname AS LeadEngineerFirstname, 
    t_EmployeeLeadEngineer.Surname AS LeadEngineerSurname,
    t_EmployeeSalesRep.Firstname AS SalesRepFirstname, 
    t_EmployeeSalesRep.Surname AS SalesRepSurname,
    t_EmployeeDesigner.Firstname AS DesignerFirstname, 
    t_EmployeeDesigner.Surname AS DesignerSurname
    FROM t_Projects
    LEFT JOIN t_Employee AS t_EmployeeProjectManager ON t_Projects.ProjectManager = t_EmployeeProjectManager.ID
    LEFT JOIN t_Employee AS t_EmployeeLeadEngineer ON t_Projects.LeadEngineer = t_EmployeeLeadEngineer.ID
    LEFT JOIN t_Employee AS t_EmployeeSalesRep ON t_Projects.SalesRep = t_EmployeeSalesRep.ID
    LEFT JOIN t_Employee AS t_EmployeeDesigner ON t_Projects.Designer = t_EmployeeDesigner.ID
    WHERE JobNumber = @ProjectRef

1 个答案:

答案 0 :(得分:0)

乍一看,

Select语句

之后写下AS语句
USE [ic_intranet]
    GO
    /****** Object:  StoredProcedure [dbo].[SP_ProjectData]    Script Date: 07/04/2016 12:29:06 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[SP_ProjectData]
        @ProjectRef int,
        @CurrentVersion decimal(4, 2)


    AS


        SELECT @CurrentVersion = VersionNumber FROM t_ProjectEquipmentVersions WHERE CurrentVersion = 'true' and JobNumber = @ProjectRef

    SELECT
    t_Projects.JobNumber,
    t_Projects.JobName,
    t_Projects.ProjectType,
    t_Projects.ProjectStatus,
    t_Projects.JobCode,
    t_Projects.TargetCompleteDate,
    t_Projects.ActualFinishDate,
    t_Projects.LocationCountry,
    t_Projects.LocationTown,
    t_Projects.SoldDate,
    (SELECT SUM(ProjectGBPCost) FROM dbo.t_ProjectEquipment WHERE (JobNumber = @ProjectRef)) AS TotalValueBuy, 
    (SELECT SUM(ProjectGBPRetail) FROM dbo.t_ProjectEquipment WHERE (JobNumber = @ProjectRef)) AS TotalValueSell,
    (SELECT COUNT(Ordered) FROM dbo.t_ProjectEquipment WHERE (Ordered = '1' and JobNumber = @ProjectRef) GROUP BY Ordered) AS OrderedItems,
    (SELECT SUM(ProjectGBPCost) FROM dbo.t_ProjectEquipment WHERE (Ordered = '1' and JobNumber = @ProjectRef)) AS OrderedValueBuy,
    (SELECT SUM(ProjectGBPRetail) FROM dbo.t_ProjectEquipment WHERE (Ordered = '1' and JobNumber = @ProjectRef)) AS OrderedValueSell,
    (SELECT COUNT(Ordered) FROM dbo.t_ProjectEquipment WHERE (Ordered = '0' and JobNumber = @ProjectRef) GROUP BY Ordered) AS NotOrderedItems,
    (SELECT SUM(ProjectGBPCost) FROM dbo.t_ProjectEquipment WHERE (Ordered = '0' and JobNumber = @ProjectRef)) AS NotOrderedValueBuy,
    (SELECT SUM(ProjectGBPRetail) FROM dbo.t_ProjectEquipment WHERE (Ordered = '0' and JobNumber = @ProjectRef)) AS NotOrderedValueSell,
    t_EmployeeProjectManager.Firstname AS ProjectManagerFirstname, 
    t_EmployeeProjectManager.Surname AS ProjectManagerSurname,
    t_EmployeeLeadEngineer.Firstname AS LeadEngineerFirstname, 
    t_EmployeeLeadEngineer.Surname AS LeadEngineerSurname,
    t_EmployeeSalesRep.Firstname AS SalesRepFirstname, 
    t_EmployeeSalesRep.Surname AS SalesRepSurname,
    t_EmployeeDesigner.Firstname AS DesignerFirstname, 
    t_EmployeeDesigner.Surname AS DesignerSurname
    FROM t_Projects
    LEFT JOIN t_Employee AS t_EmployeeProjectManager ON t_Projects.ProjectManager = t_EmployeeProjectManager.ID
    LEFT JOIN t_Employee AS t_EmployeeLeadEngineer ON t_Projects.LeadEngineer = t_EmployeeLeadEngineer.ID
    LEFT JOIN t_Employee AS t_EmployeeSalesRep ON t_Projects.SalesRep = t_EmployeeSalesRep.ID
    LEFT JOIN t_Employee AS t_EmployeeDesigner ON t_Projects.Designer = t_EmployeeDesigner.ID
    WHERE JobNumber = @ProjectRef