存储过程没有结果,截断有效,但不插入任何内容
Stored procedure gives no results, truncate works, but inserts nothing
忙于在 SQL 中编写我的第一个存储过程,我遇到了一个我自己无法解决的问题。
存储过程必须运行分类table并在其中插入新值。 t运行cate 进行得很好,但插入部分却没有。当我 运行 程序外的查询时 它 return 是预期的结果。
我手动启动程序。
过程中的数据类型与目标中的数据类型匹配table
运行 过程外查询 return 预期结果
此代码:
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
预期结果是:(来自查询)
更新日期 119183,表名 Fact_Account_Ledger
更新日期 0,表名 Dim_Company
程序结果
没有行
**查询结果*
2 行受影响
编辑:没有错误消息,return 值为 0
您的存储过程将行插入 Max_UPMJ。但不是 select 这行。
如果你想看到这些行,你应该附加代码 select * from Max_UPMJ
自己找到解决方案:
1) 通过直接在 create procedure
之后放置 (@TableName nvarchar (50), @Max_DateUpdated numeric(6,0))
我调用了不存在的变量;
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
尝试@Max_DateUpdated DateTime
我确实遇到了这个问题。让我疯狂了一会儿。问题始终是参数。当您直接 运行 查询时,您不会看到此问题,因为您在那里声明变量的方式不同。
如果您想证明存储过程中的参数数据类型有问题,请检查您使用的数据类型是否与您所说的 'identical' 查询相同。
您还可以使用 SQL 探查器查看实际传递的参数值。有时日期格式也会给您带来问题。
忙于在 SQL 中编写我的第一个存储过程,我遇到了一个我自己无法解决的问题。
存储过程必须运行分类table并在其中插入新值。 t运行cate 进行得很好,但插入部分却没有。当我 运行 程序外的查询时 它 return 是预期的结果。
我手动启动程序。
过程中的数据类型与目标中的数据类型匹配table
运行 过程外查询 return 预期结果
此代码:
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
预期结果是:(来自查询)
更新日期 119183,表名 Fact_Account_Ledger
更新日期 0,表名 Dim_Company
程序结果 没有行
**查询结果* 2 行受影响
编辑:没有错误消息,return 值为 0
您的存储过程将行插入 Max_UPMJ。但不是 select 这行。
如果你想看到这些行,你应该附加代码 select * from Max_UPMJ
自己找到解决方案:
1) 通过直接在 create procedure
之后放置 (@TableName nvarchar (50), @Max_DateUpdated numeric(6,0))
我调用了不存在的变量;
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
尝试@Max_DateUpdated DateTime
我确实遇到了这个问题。让我疯狂了一会儿。问题始终是参数。当您直接 运行 查询时,您不会看到此问题,因为您在那里声明变量的方式不同。
如果您想证明存储过程中的参数数据类型有问题,请检查您使用的数据类型是否与您所说的 'identical' 查询相同。
您还可以使用 SQL 探查器查看实际传递的参数值。有时日期格式也会给您带来问题。