2017-03-10 116 views
0

到刪除的記錄我有一個存儲過程中進行的插入記錄,但以逗號分隔的字符串,分析它,並加入個人記錄如何使用IN子句

 Create PROCEDURE [dbo].[Save_List_PMIDS] 
    @MemberID INT= 88888, 
    @PMIDList VARCHAR(MAX)= '12345,56789,67432' 

    AS 
    BEGIN 

    SELECT Split.a.value('.', 'VARCHAR(100)') AS PMID 
    INTO #pmids 
    FROM (SELECT CAST ('<M>' + REPLACE(@PMIDList, ',', '</M><M>') + '</M>' AS XML) AS String 
    ) AS A 
    CROSS APPLY String.nodes('/M') AS Split (a) 

    --DELETE FROM [dbo].[Publication.Person.Include] 
     --WHERE (MemberID = @MemberID) AND PMID IN (@PMIDList); 

INSERT INTO [dbo].[Publication.Person.Include] 
    (PersonID, PMID, MemberID) 
    SELECT @MemberID, PMID, @MemberID 
    FROM #pmids 

    DROP TABLE #pmids 

我的問題是與delete語句。如何將PMIDList字符串轉換爲IN列表中要使用的數字,以便我不添加重複項?

因此,它看起來像

DELETE FROM [dbo].[Publication.Person.Include] 
    WHERE (MemberID = @MemberID) AND PMID IN (12345,56789,67432); 

在此先感謝

+0

[傳遞一個varchar全逗號分隔值到SQL Server IN功能]的可能的複製(http://stackoverflow.com/questions/878833/passing -a-varchar-full-of-comma-delimited-values-to-a-sql-server-in-function) – AndreFeijo

+0

http://stackoverflow.com/a/10333589/630284 – SQLMason

+0

可能的重複[SQL in(@變量)查詢](http://stackoverflow.com/questions/10333319/sql-in-variable-query) – SQLMason

回答

2

爲什麼你會使用字符串?您的代碼只是把IDS中的表:

DELETE FROM [dbo].[Publication.Person.Include] 
    WHERE MemberID = @MemberID AND 
      PMID IN (SELECT PMID FROM #pmids); 
+0

你的權利 - 我正在努力,它需要成爲。您的代碼是否正確(選擇PMID IN #pmids);我必須將其更改爲從#pmid選擇PMID) – user1314159

+0

再次感謝您的幫助 – user1314159