Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在T-sql中使用select语句_Sql_Tsql - Fatal编程技术网

如何在T-sql中使用select语句

如何在T-sql中使用select语句,sql,tsql,Sql,Tsql,有人能帮我了解一下SQL中IF语句中select with的语法吗。我在IF中使用BEGIN和END with时出错 ALTER PROCEDURE Proc1 @MonthSelection AS INT = 0 AS BEGIN IF (@MonthSelection = 0) BEGIN SELECT YEAR([EventStartDate]) AS [Year]

有人能帮我了解一下SQL中IF语句中select with的语法吗。我在IF中使用BEGIN和END with时出错

    ALTER PROCEDURE Proc1 @MonthSelection AS INT = 0
AS
BEGIN
      IF (@MonthSelection = 0)
        BEGIN
            SELECT
                YEAR([EventStartDate]) AS [Year]
                ,MONTH([EventStartDate]) AS [Month Number]
                ,DATENAME(MONTH,DateAdd( month ,MONTH([EventStartDate]), 0 ) - 1) AS [Month]
                ,COUNT([UserID]) AS [Total Number of Logins]
            FROM [Table1]
            WHERE eventtype ='login'
            GROUP BY YEAR([EventStartDate])
                    ,MONTH([EventStartDate])
            ORDER BY YEAR([EventStartDate])
                    ,MONTH([EventStartDate]
        END
        ELSE
        BEGIN
            SELECT
                    YEAR([EventStartDate]) AS [Year]
                    ,MONTH([EventStartDate]) AS [Month Number]
                    ,DATENAME(MONTH,DateAdd( month ,MONTH([EventStartDate]), 0 ) - 1) AS [Month]
                    ,COUNT([UserID]) AS [Total Number of Logins]
                FROM [Table1]
                WHERE eventtype ='login'
                        AND MONTH([EventStartDate]) = @MonthSelection
                GROUP BY YEAR([EventStartDate])
                        ,MONTH([EventStartDate])
                ORDER BY YEAR([EventStartDate])
                        ,MONTH([EventStartDate]
        END
END

我确实尝试过不使用“开始”和“结束”,但仍然出现了一个错误。这段代码放置在存储的进程中

END
关键字前面缺少一个右括号

这:

致:


错误消息是什么?关键字“END”附近的语法不正确。@user2919277生成错误的行是什么?关键字“END”附近的语法不正确。是错误,我不确定,但这看起来很尴尬,因为INT=0作为BEGIN,谢谢。那是个冒号。?再次感谢。我想知道为什么开始和结束在tsql中不起作用
...

 ORDER BY YEAR([EventStartDate])
                        ,MONTH([EventStartDate]
        END
END
...

 ORDER BY YEAR([EventStartDate])
                        ,MONTH([EventStartDate])
        END
END