2017-07-24 110 views
0

我無法在下面的存儲過程中使用表類型的參數抽象數據。我從中抽取的數據由超過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 
+0

你爲什麼認爲你正在使用MySQL與MS Management Studio中?我不認爲這是可能的。 –

+0

你說得對。我混淆了這些條款。我會修復標題。 – Meli

+1

[將用戶定義表傳遞給存儲過程]的可能重複(https://stackoverflow.com/questions/30515297/pass-a-user-defined-table-to-a-stored-procedure) –

回答

0

你得到任何結果,因爲你有

AND 
     (CONVERT(INT, (SUBSTRING(DGNS_CD01, 1, 3))) IN (SELECT 
     DiagnosticCodes FROM @DiseaseCodes)) 

因此,如果您使用默認值執行該過程,然後@DiseaseCodes是空的,沒有什麼是空的表格,讓您得到任何結果。 AND使此條件成爲強制性的。

編輯:

你最新的錯誤是因爲你聲明並插入@DiseaseCodes表後,你一展身手。結束批處理並銷燬變量。然後你的EXEC在變量尚未被聲明的新批次中。

取出去DECLARE/INSERT後,改變你的EXEC這樣:

EXEC RetrieveRecords_CMS_1991_2006_Data @[email protected]; 
+0

How do我在執行語句中正確傳遞參數'@ DiseaseCodes'?我認爲在執行足夠前聲明參數並且'EXEC RetrieveRecords_CMS_1991_2006_Data @DiseaseCodes =(390,391,459)'或該語句的變體不起作用。 – Meli

+1

https://www.google。com /#q = sql + server + how + to + pass + table + to + stored + procedure –

+0

這不起作用:'EXEC RetrieveRecords_CMS_1991_2006_Data @DiseaseCodes GO'我得到以下錯誤,「必須聲明標量變量」@ DiseaseCodes 「」 – Meli

0

是直接跳到了唯一的東西......你不能有設置之間的批處理分隔符(GO) TVP和EXEC Proc,你必須將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 
+0

我需要這行'EXEC RetrieveRecords_CMS_1991_2006_Data @ DiseaseCodes = @ DiseaseCodes; '代碼除了去除GO之外還可以工作。雖然謝謝! – Meli

相關問題