使用2个值拆分已删除数据的字符串。SQL Server 2008。你能帮忙吗?

使用2个值拆分已删除数据的字符串。SQL Server 2008。你能帮忙吗?,sql,sql-server-2008,Sql,Sql Server 2008,我将一个带分隔符的字符串传递给一个存储过程,该存储过程由empId | ProductId+一个逗号作为分隔符组成 用于填充链接表。使用SQLServer2008 EmployeeOrderLink Table to be filled EmpId OrderId ProductId 可能的密钥示例 MyKeyIds="EmpId|ProductId, EG 2232|33,4555|111,43343|65 etc... 如何循

我将一个带分隔符的字符串传递给一个存储过程,该存储过程由empId | ProductId+一个逗号作为分隔符组成 用于填充链接表。使用SQLServer2008

     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
有没有关于如何拆分上述键并提取适当值的建议? 非常感谢

是SQL中拆分函数的一个示例。另一个使用它,我想我会在逗号处进行下面的拆分。然后,还必须在|处添加要拆分的代码


另一个SQL函数。基本上,拆分函数将从分隔列表创建一个表。然后,您可以对该表进行连接,就像它是一个真实的表一样。

在SQL Server 2008上,您可以使用它来完全避免此问题


如果您正在从ADO.NET调用存储的进程,则可能会很有用。

此代码将用逗号解析字符串,然后根据管道的位置拆分结果:

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

字符串解析的问题之一是试图处理所有边缘情况。您必须准备一些事情,比如缺少逗号、缺少管道、管道太多、确认您的值是数字等等。如果可能的话,我们也已经迁移到使用表值参数…

感谢您提供的链接。您能告诉我如何使用它来提取我的两个单独的值吗?正如所说,每个键都由我需要提取的两个值组成。感谢您的时间Hi感谢您的示例非常感谢。正如您所说,您可以使用拆分函数执行相同的操作。我已完成以下创建表tmpKeys setKeys varchar200 insert into tmpKeyssetKeys select*fromfnSplit@MyKeyIds,','从tmpKeys下拉表tmpKeys中选择*上面的将在逗号处拆分。很好。但是现在我需要在|处拆分。如何一次性完成并填充行?谢谢你的输入,你只需要做我已经做过的事情,用@IDset代替@MyKeyIds,设置@Delimiter,或者用另一个变量,设置为|,而不是,。我猜你必须在一段时间内把它放在某个地方。谢谢你的回复。我会用它来得到我的2个值。你能给我看一个片段吗?哇,谢谢你努力消化它。使用许多人建议的拆分函数,我成功地创建了一个包含一行键(如11 | 22等)的表。现在,假设我有一个这样的临时行表,我是否可以再次使用拆分函数,并从临时表中创建另一个包含两列(如EmpId和ProductId)的表?插入tmpKeyssetKeys select*fromfnSplit@MyKeyIds,“,”现在既然我有了这个表,我需要创建另一个包含EmpId和productid行的表。很抱歉,对sql的任何想法都有点生疏,如果你看下面的代码,/*myProductID就是右边的一切*/,您可以使用该选项或某些变体来确定管道分隔符左右两侧的内容,并将其应用于“设置键”列。。。
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