2010-07-07 28 views
2

我將分隔字符串傳遞給由empId | ProductId +逗號作爲分隔符 組成的存儲過程,其目的是填充鏈接表。使用SQL Server 2008使用2個值分割一個帶有分隔符的字符串.SQL Server 2008。你能幫我嗎?

 EmployeeOrderLink Table to be filled 
    EmpId 
    OrderId 
    ProductId 

的可能的密鑰

 MyKeyIds="EmpId|ProductId, 
    EG 2232|33,4555|111,43343|65 etc... 

示例如何遍歷字符串分割並插入到表如

while MyKeyIds ??? 
    Logic --PLEASE NOTE THAT EACH KEY IS COMPOSED BY 2 VALUES 
AND SEPARATED BY THE COMMA.DELIMETER IS USED TO SEPARATE THE INNER VALUES OF THE KEY 

     @myEmpID=--Get EmpId from split string 
     @myProductId =Get productId from split string 

     INSERT EmployeeOrderLinkend(EmpId,OrderId,ProductId) 
     VALUES(@myEmpID,@OrderIdPassedAsParamInSP, @myProductId) 
    END 

如何任何建議拆分上面的鍵並提取適當的值? 非常感謝

回答

2

Here是SQL中分割函數的一個例子。另外tutorial is this。使用它我想我會做下面的分割在逗號。您將不得不添加代碼來分割|

DECLARE @MyKeyIds NVARCHAR(40) 
DECLARE @IDset NVARCHAR(40) 
DECLARE @Pos INT 
DECLARE @NextPos INT 
DECLARE @Delimiter NVARCHAR(40) 

SET @Delimiter = ',' 
Set @MyKeyIds='2232|33,4555|111,43343|65'+ @Delimiter 
SET @Pos = charindex(@Delimiter, @MyKeyIds) 

WHILE (@pos <> 0) 
BEGIN 
    SET @IDset = substring(@MyKeyIds,1,@Pos - 1) 
    SELECT @IDset -- Show Results 
    SET @MyKeyIds = substring(@MyKeyIds,@Pos+1,len(@MyKeyIds)) 
    SET @Pos = charindex(@Delimiter,@MyKeyIds) 
END 
+0

感謝您的鏈接 你能告訴我你將如何使用它來提取我的2個單獨的values.As說每個鍵是由兩個值組成我需要提取。 感謝您的時間 – user9969 2010-07-07 21:09:14

+0

嗨感謝您的例子非常grateful.As你說你可以做同樣的分裂function.I做了以下 CREATE TABLE #tmpKeys(setKeys VARCHAR(200)) \t \t \t INSERT INTO #tmpKeys(setKeys) \t \t從fnSplit選擇*(@MyKeyIds, '') \t \t \t \t SELECT * FROM #tmpKeys \t \t \t \t DROP TABLE #tmpKeys 以上將分割爲逗號。但現在我需要在|分割。 我該如何做到這一點,並填充我的行? 感謝您的任何輸入 – user9969 2010-07-08 05:20:04

+0

您只需要做我已經擁有的只需使用@IDset作爲@MyKeyIds的位置並設置@Delimiter,或設置一個不同的變量,設置爲|代替 ,。猜測你將不得不把它放在某個地方。 – Kyra 2010-07-08 14:28:04

0

另一個SQL split函數。基本上,split()函數將從您的分隔列表中創建一個表格。然後,您可以參與該表格,就好像它是一個「真正的」表格一樣。

+0

感謝您的回覆。我會使用它並獲得我的2個值。你能告訴我一個片段嗎? – user9969 2010-07-07 21:13:18

0

正如你在SQL Server 2008上,你可以使用Table-Valued Parameters完全避免這個問題。

如果您從ADO.NET調用存儲過程this link可能會有用。

1

這段代碼解析由逗號的字符串,然後分割基於管道的位置,結果是:

SET NOCOUNT ON 

DECLARE 
    @keyPair VARCHAR(1000), 
    @myEmpID VARCHAR(1000), 
    @myProductID VARCHAR(1000) 

DECLARE @myKeyIDs VARCHAR(1000) 
SET @myKeyIDs = '2232|33,4555|111,43343|65' 

DECLARE 
    @len INT, 
    @pos INT, 
    @found INT 

SELECT 
    @len = LEN(@myKeyIDs), 
    @pos = 1 

SET @myKeyIDs = @myKeyIDs + ',' 

/* Find the first instance of a comma */ 
SET @found = CHARINDEX(',', @myKeyIDs, @pos) 

WHILE @found > 0 
BEGIN 

    /* The key pair starts at the @pos position and goes */ 
    /* to the @found position minus the @pos position */ 
    SET @keyPair= SUBSTRING(@myKeyIDs, @pos, @found - (@pos)) 

    /* Double-check that pipe exists to avoid failure */ 
    /* If no pipe exists, assume value is myEmpID  */ 
    IF CHARINDEX('|',@keyPair) = 0 
    BEGIN 
    SET @myEmpID = NULLIF(@keyPair, '') 
    SET @myProductID = NULL 
    END 
    ELSE 
    BEGIN 
    /* myEmpID is everything left of the pipe */ 
    /* myProductID is everything on the right */ 
    SET @myEmpID = NULLIF(SUBSTRING(@keyPair, 1, 
     CHARINDEX('|', @keyPair) - 1), '') 
    SET @myProductID = NULLIF(SUBSTRING(@keyPair, 
     CHARINDEX('|', @keyPair) + 1, LEN(@keyPair) - 1), '') 
    END 

    /* 
    INSERT EmployeeOrderLinkend(EmpId,OrderId,ProductId) 
    VALUES(@myEmpID,@OrderIdPassedAsParamInSP, @myProductId) 
    */ 
    SELECT @myEmpID AS myEmpID, @myProductID AS myProductID 

    /* Move to the next position and search again */ 
    SET @pos = @found + 1 
    SET @found = CHARINDEX(',', @myKeyIDs, @pos) 

END 

一個與字符串解析試圖處理所有的邊緣情況的問題。您必須爲丟失逗號,丟失管道,過多管道,確認您的值是數字等等做好準備。如果可能,我們也遷移到使用表值參數...

+0

哇,謝謝你試圖消化它的努力。 使用一個分割函數儘可能多的建議我設法創建一個表中有一排鍵,例如11 | 22等 現在給出我有一個像這樣的行的臨時表 我可以以某種方式再次使用分割功能,並創建另一個表中有兩列EG EmpId和ProductId從我的臨時表中? – user9969 2010-07-08 05:47:49

+0

INSERT INTO #tmpKeys(setKeys) \t \t SELECT * FROM fnSplit(@MyKeyIds,「」)現在給出 ,我有表,我需要以某種方式創建另一個表EMPID行和產品ID在SQL 對不起有點生疏任何想法 – user9969 2010-07-08 05:51:47

+0

如果你看看下面的代碼/ * myProductID是右邊的所有東西* /,你可以使用這個(或一些變化)來確定管道分隔符的左側和右側的內容,並將其應用於setKeys列... – 8kb 2010-07-08 06:00:42

相關問題