开发者社区> 问答> 正文

识别SQL Server存储过程中的错误

我正在使用SQL Server 2016

通过我的存储过程,我试图找到Recommendationsper 的数量CityID,并且遇到了语法相关的麻烦。我在底部列出了错误消息。**

DROP PROCEDURE IF EXISTS [Kittens_Dogs_AGGREGATE].[dbo].[sp_RecommendationsPerSite]

USE [Kittens_Dogs_AGGREGATE]
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      Tom Brady
    -- Create date: 12/27/2019
    -- Description: This script is to check for the amount of recommendations per site
    -- =============================================
    CREATE PROCEDURE [dbo].[sp_RecommendationsPerSite] 
        -- Add the parameters for the stored procedure here
 DECLARE @CityID NVARCHAR(50),
         @RunDate DATETIME = CAST(GETDATE() as date),
         @DepartureTime DATETIME,
         @ApplesOranges NVARCHAR(250),
         @RollerCoaster NVARCHAR(50))
    AS
    BEGIN
        SET NOCOUNT ON;
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SELECT @RApplesOranges AS ApplesOranges, Count(DISTINCT @RollerCoaster) AS RollerCoaster
        FROM [Kittens_Dogs_AGGREATE].[dbo].[Recommendations] b
        WHERE @CityID = 210
        AND cast(@DepartureTime as date) = CAST(GETDATE() AS NVARCHAR)
        AND a.ApplesOranges = b.ApplesOranges
        AND cast(@DepartureTime as date) <> cast(@RunDate as date) as '2019-12-27' -- ,(select count(DISTINCT RollerCoaster)
        FROM [Kittnes_Dogs_AGGREGATE].[dbo].[Recommendations] b
        WHERE CityID = 210
        AND cast(@DepartureDate as date) = CAST(GETDATE() AS NVARCHAR)
        AND a.ApplesOranges = b.ApplesOranges
        AND cast(@DepartureDate as date) <> cast(@RunDate as date)) as CAST(GETDATE() - 1 AS NVARCHAR)
        FROM [Kittens_Dogs_AGGREGATE].[dbo].[Recommendations] a
        WHERE CityID = 210
        GROUP BY ApplesOranges
        ORDER BY 1

    END
    GO

错误讯息

消息156,级别15,状态1,过程sp_RecommendationsPerSite,行3 [Batch Start Line 22] 关键字'DECLARE'附近的语法不正确。

消息102,级别15,状态1,过程sp_RecommendationsPerSite,行7 [Batch Start Line 22] ')'附近的语法不正确

展开
收起
祖安文状元 2020-01-04 15:29:31 574 0
1 条回答
写回答
取消 提交回答
  • 这里发生了很多事情。

    您的第二个错误是在参数定义的最后一行:@RollerCoaster NVARCHAR(50))结尾处有一个额外的')'。

    接下来,您的select语句将失败,因为您有多个FROM和WHERE子句。您似乎已尝试将其注释掉,但--仅注释了一行。

    2020-01-04 15:29:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载