简体   繁体   English

执行存储过程后在SQL Server中返回空表

[英]Returning Empty table in SQL Server after execute stored procedure

I have a problem with my stored procedure. 我的存储过程有问题。 I'm a bit confused about that because the stored procedure how I defined before it was working fine, but now it doesn't work. 我对此感到有些困惑,因为在我定义存储过程之前,存储过程可以正常工作,但是现在不起作用。

Here the T-SQL code: 这里是T-SQL代码:

CREATE PROCEDURE [dbo].[getMonthMaxVal] 
    @from datetime,
    @to datetime
AS
BEGIN
    SET NOCOUNT ON;

    SELECT ds22.*
    FROM (SELECT 
                MAX(DAY(ds.datum)) AS TagMax,
                MONTH(ds.datum) AS MonatMax,
                YEAR(ds.datum) AS JahrMax,
                CONVERT(VARCHAR(10), MAX(ds.datum), 120) AS DatumMax
          FROM QR_DS022s ds
          WHERE ds.datum >= @from AND ds.datum <= @to
          GROUP BY MONTH(ds.datum), YEAR(ds.datum)) maxDate INNER JOIN QR_DS022s ds22 ON maxDate.DatumMax = ds22.datum
    ORDER BY ds22.datum
END

In the SQL Server 2008 R2 this stored procedure was working fine. 在SQL Server 2008 R2中,此存储过程运行良好。 In SQL Server 2014 it doesn't work. 在SQL Server 2014中,它不起作用。 What's wrong? 怎么了?

EDIT: 编辑:

When I'm executing the SP with the code: 当我使用代码执行SP时:

EXECUTE ETF_DB.dbo.getMonthMaxVal @from='2014-01-01', @to='2014-04-01'

I'm getting an empty table with empty columns. 我正在得到一个带有空列的空表。

When I'm doing this: 当我这样做时:

EXECUTE ETF_DB.dbo.getMonthMaxVal @from='2014-01-01', @to='2014-04-02'

I'm getting an error message: 我收到一条错误消息:

When converting a varchar data type to a datetime data type, the value is out of range. 将varchar数据类型转换为datetime数据类型时,该值超出范围。

While I can't see any data, and table structure, i'd remove the VARCHAR reference and use DATE across the board: 虽然我看不到任何数据和表结构,但我将删除VARCHAR引用并全面使用DATE:

CREATE PROCEDURE [dbo].[getMonthMaxVal] 
    @from   DATE,
    @to     DATE
AS
BEGIN
    SET NOCOUNT ON;

    SELECT ds22.*
    FROM (  SELECT 
                    MAX(DAY(ds.datum)) AS TagMax,
                    MONTH(ds.datum) AS MonatMax,
                    YEAR(ds.datum) AS JahrMax,
                    CONVERT(DATE, MAX(ds.datum)) AS DatumMax
              FROM QR_DS022s ds
              WHERE ds.datum >= @from AND ds.datum <= @to
              GROUP BY MONTH(ds.datum), YEAR(ds.datum)) maxDate 
          INNER JOIN QR_DS022s ds22 
            ON maxDate.DatumMax = CONVERT(DATE, ds22.datum)
    ORDER BY ds22.datum
END

Let me know if that fixes your problem. 让我知道是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM