【发布时间】: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