【问题标题】:Stored procedure gives no results, truncate works, but inserts nothing存储过程没有结果,截断工作,但什么也不插入
【发布时间】:2019-07-02 10:18:02
【问题描述】:

忙于用 SQL 编写我的第一个存储过程,我遇到了一个我自己无法解决的问题。

存储过程必须截断表并在其中插入新值。截断顺利,但插入不顺利。当我在过程之外运行查询时,它会返回预期的结果。

我手动启动程序。

  • 过程中的数据类型与目标表中的数据类型匹配

  • 在过程外运行查询返回预期结果

这是代码:

Create Procedure Usp_Max_DateUpdated( 
        @TableName nvarchar (50)    
    ,   @Max_DateUpdated numeric(6,0)   
)
as
Begin

set nocount on;

truncate table Max_UPMJ;


With CTE_Fact_Account_Ledger as(
select 
cast(isnull(max([DateUpdated (GLUPMJ)]),0) as numeric(6,0)) as Max_DateUpdated,
'Fact_Account_Ledger' as TableName 
from DWH_Backroom.dbo.Fact_Account_Ledger
)

, CTE_Dim_Company as(
select 
cast(isnull(max([DateUpdated (CCUPMJ)]) ,0) as numeric(6,0)) as Max_DateUpdated, 
'Dim_Company' as TableName
from DWH_Backroom.dbo.Dim_Company
)

insert into Max_UPMJ (DateUpdated, TableName)
select 
Max_DateUpdated,
TableName 
From CTE_Fact_Account_Ledger CFAL
where Max_DateUpdated = @Max_DateUpdated and TableName = @TableName

union 

select 
Max_DateUpdated,
TableName
From CTE_Dim_Company
where Max_DateUpdated = @Max_DateUpdated and TableName = @TableName

End

预期结果是:(来自查询)

  • DateUpdated 119183,表名 Fact_Account_Ledger

  • DateUpdated 0,表名 Dim_Company

程序结果 没有行

**查询结果* 2 行受影响

编辑:没有错误信息,返回值为0

【问题讨论】:

  • 那么当您运行 SP 时会发生什么? @Max_DateUpdated 表示它是一个日期,那么为什么它被声明为 anumeric?
  • 你有什么错误吗?
  • 它是来自数据库的 Oracle 日期格式(年,年中的天数),数字很好。
  • 没有返回错误代码,只有返回值 = 0,所以应该没问题 :) 感谢您的快速回复
  • “它是来自数据库的 Oracle 日期格式(年,年中的天数),数字很好。” 当您使用 @987654324 时,它不是“很好” @您的WHERE 的值;使您确实没有用的任何索引。应使用合适的数据类型提供数据; numeric 不是日期(和时间)数据的合适数据类型,日期和时间数据类型是。

标签: sql-server stored-procedures union sql-insert common-table-expression


【解决方案1】:

您的存储过程将行插入 Max_UPMJ。但不要选择此行。 如果你想看到这一行,你应该附加代码select * from Max_UPMJ

【讨论】:

    【解决方案2】:

    自己找到了解决方案:

    1) 通过将(@TableName nvarchar (50), @Max_DateUpdated numeric(6,0)) 直接放在create procedure 之后,我调用了不存在的变量;

    2) 在这个过程中似乎不需要任何参数或变量。

    以下代码有效:

    Create Procedure Usp_Max_DateUpdated
    
    as
    Begin
    
    set nocount on;
    
    truncate table Max_UPMJ;
    
    With CTE_Fact_Account_Ledger as(
    select 
            cast(isnull(max([DateUpdated (GLUPMJ)]),0) as numeric(6,0)) as Max_DateUpdated
        ,   'Fact_Account_Ledger' as TableName 
    from DWH_Backroom.dbo.Fact_Account_Ledger
    )
    
    , CTE_Dim_Company as(
    select 
            cast(isnull(max([DateUpdated (CCUPMJ)]) ,0) as numeric(6,0)) as Max_DateUpdated
        ,   'Dim_Company' as TableName
    from DWH_Backroom.dbo.Dim_Company
    )
    
    insert into Max_UPMJ (DateUpdated, TableName)
    
    select 
            Max_DateUpdated
        ,   TableName 
    From CTE_Fact_Account_Ledger CFAL
    
    union 
    
    select 
            Max_DateUpdated
        ,   TableName
    From CTE_Dim_Company
    ;
    End
    

    【讨论】:

      【解决方案3】:

      试试@Max_DateUpdated DateTime

      我遇到了这个确切的问题。让我发疯了一段时间。问题始终是参数。直接运行查询是看不到这个问题的,因为这里声明变量的方式不一样。

      如果您想证明存储过程中的参数数据类型存在问题,请检查您使用的数据类型是否与您所说的“相同”查询相同。

      还可以使用 SQL 分析器查看实际传递的参数值。有时日期格式也会给您带来问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-19
        • 2019-06-13
        相关资源
        最近更新 更多