簡體   English   中英

在SQL Server中將兩個表合並為一個表

[英]Merge two tables into one table in sql server

我有這些架構:

CREATE TABLE Ndc
(
    [NdcId] INT NOT NULL PRIMARY KEY IDENTITY(1,1), 
    [Code] VARCHAR(256) NULL,
)

CREATE TABLE [RxCui]
(
    [RxCuiId] INT NOT NULL PRIMARY KEY IDENTITY(1,1), 
    [Code] VARCHAR(256) NULL, 
)

CREATE TABLE [RxCuiNdc]
(
    [RxCuiNdcId] INT NOT NULL PRIMARY KEY IDENTITY(1,1), 
    [RxCuiId] INT NOT NULL, 
    [NdcId] INT NOT NULL,
    CONSTRAINT [FK_RxCuiNdc_Ndc] FOREIGN KEY (NdcId) REFERENCES Common.Ndc(NdcId), 
    CONSTRAINT [FK_RxCuiNdc_RxCui] FOREIGN KEY (RxCuiId) REFERENCES Common.RxCui(RxCuiId), 
)

我有一個JSON,它是葯品標簽的列表,每個標簽都與兩個字符串數組相關聯。 一個數組代表RxCui的列表,另一個數組代表Ndc的列表。 我將獲取JSON文件,並將每個RxCui插入到RxCui表中,並從上面的模式插入Ndc表中的每個Ndc。 假設表具有所有Ndc和RxCui,並且沒有任何重復項,那么我想將它們映射到RxCuiNdc表。

這是代表JSON的類

public class DrugLabel
    {
        public OpenFda openfda { get; set; }
    }

public class OpenFda
    {
        public IList<string> product_ndc { get; set; } = new List<string>();
        public IList<string> rxcui { get; set; } = new List<string>();
    }

public class DrugLabels
    {
        public List<DrugLabel> results { get; set; } = new List<DrugLabel>();
    }

我嘗試了一個存儲過程來創建RxCui和Ndc之間的映射。

CREATE PROCEDURE MapRxCuiToNdc
    @rxCuiList varchar(MAX),
    @ndcList varchar(MAX)
AS
    DECLARE @rxCuiTable TABLE (RxCuiCode varchar(max)) 

    -- Insert statement
    INSERT INTO @rxCuiTable
    SELECT Value
    FROM  string_split(@rxCuiList, ',')  

    DECLARE @ndcTable TABLE (NdcCode varchar(max)) 

    -- Insert statement
    INSERT INTO @ndcTable
    SELECT Value
    FROM  string_split(@ndcList, ',')  

    MERGE INTO [Common].[RxCuiNdc] T
       USING (SELECT A.RxCuiId, A.Code AS RxCuiCode, B.NdcId, B.Code AS NdcCode
                FROM Common.RxCui A cross join Common.Ndc B
                WHERE A.Code IN (SELECT RxCuiCode FROM @rxCuiTable) AND B.Code IN (SELECT NdcCode FROM @ndcTable )
            ) S
          ON T.RxCuiId = S.RxCuiId AND T.NdcId = S.NdcId
    WHEN MATCHED THEN 
       UPDATE
          SET T.NdcId = S.NdcId, T.RxCuiId = S.RxCuiId
    WHEN NOT MATCHED THEN
       INSERT ( NdcId, RxCuiId ) VALUES ( S.NdcId, S.RxCuiId);

我遇到的問題是獲取正確的映射。 在我的C#代碼中,我試圖找到一種方法來發送每個RxCui和Ndc的列表,而不必在每個標簽上進行foreach。

我到目前為止是

var data = new DrugLabels();
var productsNdc = data.results.Where(x => x.openfda?.product_ndc?.Count > 0).SelectMany(x => x.openfda.product_ndc).ToArray();
var rxCui = data.results.Where(x => x.openfda?.rxcui?.Count > 0).SelectMany(x => x.openfda.rxcui).ToArray();
await MedicationSearchStoredProcedure(string.Join(",", rxCui), string.Join(",", productsNdc));

前面的代碼只是將存儲過程映射到我發送的每個Ndc和每個rxCui。 我想要的是根據標簽映射它們,這是我想到的唯一方法是使用foreach,諸如此類。

foreach(var label in data.results)
{
    await MedicationSearchStoredProcedure(string.Join(",", label.openfda.rxcui), string.Join(",", label.openfda.product_ndc));
}

但這就是我不想做的。

有什么建議么? 是foreach唯一的方法嗎?

我建議您得到一個結果數組,然后撥打電話。

List<string> rxcui = new List<string>();
List<string> product_ndc = new List<string>();

foreach(var label in data.results)
{
    rxcui.AddRange(string.Join(",", label.openfda.rxcui));
    product_ndc.AddRange(string.Join(",", label.openfda.product_ndc));
}

await MedicationSearchStoredProcedure(rxcui, product_ndc);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM