我在使用下面的存储过程中的表类型参数抽象数据时遇到问题。我抽象的数据包含100多个列/变量。在存储过程中,我关注四个变量:Age_CNT(年龄),SEX,ADMSNDT(入场日期)和DGNS_CD01(诊断代码1)。变量DGNS_CD01或诊断代码1具有多种可能性。例如,在下面的代码中,我希望用DGNS_CD01或诊断代码值390,391或459抽象记录。由于DGNS_CD01可以有多个值,我声明一个名为@DiseaseCodes的类型表变量并插入值390,391,但是,当我执行存储过程时,我发现它不能与参数@DiseaseCodes一起使用,我无法弄清楚原因。除了@DiseaseCodes
之外,其他参数都可以正常工作下面是一小部分虚假数据的查询。我使用的是Microsoft SQL Server Management Studio 2014.对存储过程的任何帮助都会很棒!我希望避免对特定诊断代码值进行硬编码,因为它们会根据请求进行更改。
--- This is the name of my database.
USE [Datasets_For_Researchers]
GO
--- Here is fake data to run the query below.
CREATE TABLE [dbo].[CMS_Fake_Practice_Data](
[AGE_CNT] [varchar](50) NULL,
[SEX] [varchar](50) NULL,
[ADMSNDT] [varchar](50) NULL,
[DGNS_CD01] [varchar](50) NULL,
) ON [PRIMARY]
GO
INSERT INTO [dbo].[CMS_Fake_Practice_Data] (AGE_CNT, SEX, ADMSNDT, DGNS_CD01)
VALUES (66,1,2000344,390), (66,1,2000355,391),(80,2,2000355,500)
GO
--- According to online articles, I first must initiate type table that
--- will be used to store the diagnostic codes I am using to subset the
--- data with.
CREATE TYPE list_of_three_character_diagnostic_code_tabletype AS TABLE
(DiagnosticCodes int NOT NULL PRIMARY KEY)
GO
---The goal of the stored procedure is to simplify data abstraction.
---The most important parameter are the list of diagnostic codes that are
---passed through parameter @DiseaseCodes of type table.
CREATE PROCEDURE [dbo].[RetrieveRecords_CMS_1991_2006_Data]
(
@MinimumAge AS INT = NULL
, @SelectSex AS INT = NULL
, @SelectYear AS INT = NULL
, @DiseaseCodes list_of_three_character_diagnostic_code_tabletype
READONLY
)
AS
BEGIN
SELECT
*
FROM
CMS_Fake_Practice_Data
WHERE
(@MinimumAge IS NULL OR AGE_CNT >= @MinimumAge) AND
(@SelectSex IS NULL OR SEX = @SelectSex) AND
(@SelectYear IS NULL OR (SUBSTRING(ADMSNDT, 1,4) = @SelectYear)) AND
(CONVERT(INT, (SUBSTRING( DGNS_CD01, 1, 3))) IN (SELECT
DiagnosticCodes FROM @DiseaseCodes))
END
GO
--- Then I declare the table variable @DiseaseCodes and insert values
--- that are definitely present in the CMS_Fake_Practice_Data table.
DECLARE @DiseaseCodes AS list_of_three_character_diagnostic_code_tabletype
INSERT INTO @DiseaseCodes (DiagnosticCodes)
VALUES (390),(391),(459)
GO
编辑我更新了我的代码以反映其他人的建议,但现在收到一条错误,指出@DiseaseCodes从未声明,即使它在上面的代码中。
---I execute the query with the default setting but retrieve no records.
EXEC RetrieveRecords_CMS_1991_2006_Data @DiseaseCodes
GO
答案 0 :(得分:0)
因为你有
,你没有得到任何结果 AND
(CONVERT(INT, (SUBSTRING( DGNS_CD01, 1, 3))) IN (SELECT
DiagnosticCodes FROM @DiseaseCodes))
因此,如果使用默认值执行该过程,则@DiseaseCodes为空,并且空表中没有任何内容,因此您不会得到任何结果。 AND
强制要求此条件。
编辑:
您的最新错误是因为在您DECLARE并插入@DiseaseCodes
表后,您有一个GO。结束批处理并销毁变量。然后你的EXEC在一个新的批处理中,其中变量尚未被声明。
在DECLARE / INSERT之后取出GO,并将EXEC更改为:
EXEC RetrieveRecords_CMS_1991_2006_Data @DiseaseCodes=@DiseaseCodes;
答案 1 :(得分:0)
唯一跳出来的东西......你不能在TVP的设置和EXEC Proc之间有一个批处理分隔符(GO),你必须将TVP添加到proc执行中。
所以改变这个......
--- Then I declare the table variable @DiseaseCodes and insert values
--- that are definitely present in the CMS_Fake_Practice_Data table.
DECLARE @DiseaseCodes AS list_of_three_character_diagnostic_code_tabletype
INSERT INTO @DiseaseCodes (DiagnosticCodes)
VALUES (390),(391),(459)
GO
---I execute the query with the default setting but retrieve no records.
EXEC RetrieveRecords_CMS_1991_2006_Data
GO
......对此...
--- Then I declare the table variable @DiseaseCodes and insert values
--- that are definitely present in the CMS_Fake_Practice_Data table.
DECLARE @DiseaseCodes AS list_of_three_character_diagnostic_code_tabletype
INSERT INTO @DiseaseCodes (DiagnosticCodes)
VALUES (390),(391),(459);
---I execute the query with the default setting but retrieve no records.
EXEC RetrieveRecords_CMS_1991_2006_Data @DiseaseCodes;
GO