我無法在下面的存儲過程中使用表類型的參數抽象數據。我從中抽取的數據由超過100列/變量組成。在存儲過程中,我關注四個變量:Age_CNT(Age),SEX,ADMSNDT(准入日期)和DGNS_CD01(診斷代碼1)。變量DGNS_CD01或診斷代碼1有多種可能性。例如,在下面的代碼中,我正在尋找使用DGNS_CD01或診斷代碼值390,391或459來抽象記錄。由於DGNS_CD01可以有多個值,我聲明一個名爲@DiseaseCodes的類型表變量並插入值390,391,和459.但是,當我執行存儲過程時,我發現它不能使用參數@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
你爲什麼認爲你正在使用MySQL與MS Management Studio中?我不認爲這是可能的。 –
你說得對。我混淆了這些條款。我會修復標題。 – Meli
[將用戶定義表傳遞給存儲過程]的可能重複(https://stackoverflow.com/questions/30515297/pass-a-user-defined-table-to-a-stored-procedure) –