如何将分号分隔的长字符串存储到多个列中?

时间:2017-09-14 06:16:50

标签: sql sql-server tsql

基本上由于对实时服务器的限制,我不能使用文本文件,所以我需要在T-SQL代码中硬编码数据。

所以首先我从文本文件中创建了这样的字符串:

("001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24", .... )  

我有这个表结构:

DECLARE @Product TABLE(ProductCode INT NOT NULL, Description nvarchar(100) NOT NULL)

首先,我需要在表变量中存储代码和描述。完成后,我可以轻松地将其映射到物理表并更新记录。

我怎样才能达到类似的目的:

insert into @Product(ProductCode, Description) 
values ("001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24", .... )
 Code        Description

 001122      Sale Item1
 001123      Sale Item2
 001124      Sale Item3

3 个答案:

答案 0 :(得分:1)

如果你有像示例中那样的固定格式,那么只需使用CHARINDEXSUBSTRING

即可获得所需的输出
SELECT 
    SUBSTRING(description, 0, CHARINDEX(';', DESCRIPTION, 0)) code,
    SUBSTRING(description, CHARINDEX(';', DESCRIPTION, 0)+1, LEN(DESCRIPTION)) Description  
FROM @Product

输出:

 code       Description
------------------------
001122      Sale Item 1
001123      Sale Item 23
001124      Sale Item 24

答案 1 :(得分:0)

我们在这里继续:

DECLARE @MyString nvarchar(max) = '"001122;Sale Item 1", "001123;Sale Item 23", "001124;Sale Item 24"';

DECLARE @delimiter1   nvarchar(1)
      , @delimiter2   nvarchar(1)
      , @Start1       int
      , @End1         int
      , @Length       int
      , @cNEXTVALUE   nvarchar(1000)
      , @cNEXTVALUE2  nvarchar(1000)
      , @StringLength int;

DECLARE @Product TABLE ( 
                       ProductCode int NOT NULL
                     , Description nvarchar(100) NOT NULL );

SET @MyString = RTRIM(LTRIM(@MyString));
SET @MyString = REPLACE(@MyString, CHAR(13), '');
SET @MyString = REPLACE(@MyString, CHAR(10), '');

SET @delimiter1 = ';';
SET @delimiter2 = ',';
SET @MyString = REPLACE(@MyString, '"', '')+@delimiter2;
SET @StringLength = LEN(@MyString);

SELECT @Start1 = 0
     , @End1 = CHARINDEX(@delimiter1, @MyString, 1);

SELECT @Length = @End1 - @Start1;

WHILE @Length > 0
    BEGIN

        SET @cNEXTVALUE = SUBSTRING(@MyString, @Start1, @Length);
        SET @Start1 = @End1 + 1;

        SET @End1 = CHARINDEX(@delimiter2, @MyString, @Start1);
        SELECT @Length = @End1 - @Start1;

        SET @cNEXTVALUE2 = SUBSTRING(@MyString, @Start1, @Length);

        IF LEN(RTRIM(LTRIM(@cNEXTVALUE))) > 0
            BEGIN
                INSERT INTO @Product
                       ( ProductCode
                       , Description
                       ) 
                VALUES
                       ( @cNEXTVALUE, @cNEXTVALUE2 );
            END;

        SET @Start1 = @End1 + 2;
        SET @End1 = CHARINDEX(@delimiter1, @MyString, @Start1);
        SELECT @Length = @End1 - @Start1;

    END;

SELECT *
  FROM @Product;

答案 2 :(得分:0)

我为您准备了一个样品,请查看

c1      c2
001123  Sale Item 23
001124  Sale Item 24
001122  Sale Item 1

输出

jmeter.save.saveservice.output_format=xml
jmeter.save.saveservice.response_data=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.responseHeaders=true