我有一个像这样的表类型:
CREATE TYPE [dbo].[TT_CustomType] AS TABLE
(
[Month] [int] NULL,
[Year] [int] NULL,
[CountryId] [int] NULL,
[BranchId] [int] NULL
)
从存储过程中,我正在创建这种类型的变量并向其添加值。
添加值后,我将其传递给另一个存储过程。
DECLARE @Tbl TT_CustomType;
INSERT INTO @Tbl
SELECT Year, Month, CountryId, BranchId
FROM @TEMP1 DT
GROUP BY Year, Month, CountryId, BranchId
ORDER BY DT.Year ASC, DT.Month ASC;
EXEC usp_ABC @Tbl;
但是在 中usp_ABC
,我正在调用标量值函数来从表中获取tbl_RawData
与 Month、Year、CountryId 和 BranchId 相关的数据@Tbl
。我得到 0 个结果。
当我传递实际值时,我从函数中获取值。
为了避免参数欺骗,我创建了一个临时表变量并将所有值@Tbl
插入其中。
ALTER PROCEDURE [dbo].[usp_ABC]
@Tbl1 TT_CustomType ReadOnly
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Tbl TABLE(MONTH INT, YEAR INT, CountryId INT, BranchId INT)
INSERT INTO @Tbl
SELECT * FROM @Tbl1
-- the following query returns 0s even if there is value for matching conditions
SELECT
dbo.CustomFunction(month, year, countryId, BranchId, 'option1'),
dbo.CustomFunction(month, year, countryId, BranchId, 'option2')
FROM
@Tbl
-- The following query returns values. Those are the exact values of @Tbl
SELECT
dbo.CustomFunction(1, 2018, 910, 1980, 'option1'),
dbo.CustomFunction(1, 2018, 910, 1980, 'option2')
FROM
@Tbl
END
ALTER FUNCTION [dbo].[CustomFunction]
(@Year INT,
@Month INT,
@CountryId INT,
@BranchId INT,
@option NVARCHAR(200))
RETURNS DECIMAL(38, 18)
AS
BEGIN
DECLARE @ReturnValue DECIMAL(38,18);
SELECT
@ReturnValue = Datavaluefield
FROM
Table1
WHERE
month = @Month AND year = @Year
AND countryId = @CountryId
AND BranchId = @BranchId
AND option = @option
END
我是否遗漏了什么或参数欺骗仍在发生。请提出任何解决方案或替代方案。