来自T-SQL中的查询和存储过程的不同结果

时间:2018-06-11 09:46:46

标签: sql-server tsql stored-procedures

美好的一天!

我正在尝试对我们客户端的某些表进行一些小的空比率分析,我想创建一个存储过程以使其更容易自动化。

我遇到的问题是,当单独使用查询时,它会给出正确的结果,但在使用存储过程时,结果会有所不同。

查询

with cte as 
(SELECT sum(case when [EstateBOA] is NULL then 1 else 0 end) as "Null Values",
        sum(case when [EstateBOA] is not NULL then 1 else 0 end) as "Not-Null Values" 
        FROM [FASTDW].[PROP].[FactBuilding]
)

SELECT '[EstateBOA]' as ColumnName,*,CAST("Null Values" AS FLOAT) /
CAST(("Null Values" + "Not-Null Values") AS FLOAT) as "Null ratio"
FROM cte

存储过程

USE [AUDIT] 
GO
/****** Object:  StoredProcedure [dbo].[GetNullStatsFromColumnInBuilding]    
Script Date: 2018-06-11 11:22:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetNullStatsFromColumnInBuilding]
     @Columnname nvarchar(50)
AS
;with cte as 
(SELECT sum(case when @Columnname is NULL then 1 else 0 end) as "NullValues",
        sum(case when @Columnname is not NULL then 1 else 0 end) as "Not-Null Values" 
    FROM [FASTDW].[PROP].[FactBuilding]
)
SELECT @Columnname as ColumnName,*,  CAST("Null Values" AS FLOAT) / 
CAST(("Null Values" + "Not-Null Values") AS FLOAT) as "Null ratio"
FROM cte

查询结果

Query result

存储过程结果 Procedure Result

知道为什么会这样吗?是因为" X是"在存储过程中?

感谢您的时间。

0 个答案:

没有答案