如何通过逗号分隔将字符串值插入SQL Server表

时间:2018-04-26 08:53:28

标签: sql sql-server

int QcEditorId = 21
string Freelancer = '2,3,4,5'

我想将此值插入SQL Server表中,如

QcEditorId  Freelancer
----------------------
  21           2
  21           3
  21           4
  21           5

请帮我查询或存储过程

4 个答案:

答案 0 :(得分:1)

String_split starting in SQL server 2016 have ability to split your strings 

示例:

 declare @table table (QcEditorId int, Freelancer varchar (100))

    insert into @table
    select 21, '2,3,4,5'
    declare @freelancer varchar(100)
    = (select freelancer from @table)

    select QcEditorId,x.value Name  from @table
    cross apply(
    select *  from string_split(@freelancer,',') ) x
       -- Use this function to split strings and , as delimiter

- 或者对于以前的版本,创建一个表值函数,很多  ----可在网上找到

 Example:

    CREATE FUNCTION [dbo].[splitstring] ( @stringToSplit VARCHAR(MAX) )
    RETURNS
     @returnList TABLE ([Name] [nvarchar] (500))
    AS
    BEGIN

     DECLARE @name NVARCHAR(255)
     DECLARE @pos INT

     WHILE CHARINDEX(',', @stringToSplit) > 0
     BEGIN
      SELECT @pos  = CHARINDEX(',', @stringToSplit)  
      SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

      INSERT INTO @returnList 
      SELECT @name

      SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
     END

     INSERT INTO @returnList
     SELECT @stringToSplit

     RETURN
    END

解决方案:

declare @table table (QcEditorId int, Freelancer varchar (100))

insert into @table
select 21, '2,3,4,5'
declare @freelancer varchar(100)
= (select freelancer from @table)

select QcEditorId,x.Name  from @table
cross apply(
select *  from [dbo].[SplitString](@freelancer) ) x

答案 1 :(得分:0)

DECLARE @FLXML AS XML 
SET @FLXML = cast(('<a>'+replace(@FreelancerId,',' ,'</a><a>')
            +'</a>') AS XML)
INSERT INTO [QMT_UserMaping](QcEditor_ID,Freelancer_ID)
SELECT @QCId,A.VALUE('.', 'varchar(max)')
    FROM @FLXML.nodes('a') AS FN(a)

答案 2 :(得分:0)

SQL用户定义的拆分功能

ALTER FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX)) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1

        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN 
END

INSERT命令

INSERT INTO YourTable(QcEditorId,Freelancer) 
SELECT 21,splitdata FROM [dbo].[fnSplitString]('2,3,4,5' ,',' )

STRING_SPLIT(MSSQL Server 2016)

无需自定义用户定义的功能

INSERT INTO YourTable(QcEditorId,Freelancer) 
SELECT 21,value FROM STRING_SPLIT('2,3,4,5' ,',' ) 

答案 3 :(得分:0)

I have a SQL Table like this:
| SomeID         | OtherID     | Data
+----------------+-------------+-------------------
| abcdef-.....   | cdef123-... | 18,20,22
| abcdef-.....   | 4554a24-... | 17,19
| 987654-.....   | 12324a2-... | 13,19,20
Is there a query where I can perform a query like SELECT OtherID, SplitData WHERE SomeID = 'abcdef-.......' that returns individual rows, like this?
| OtherID     | SplitData
+-------------+-------------------
| cdef123-... | 18
| cdef123-... | 20
| cdef123-... | 22
| 4554a24-... | 17
| 4554a24-... | 19
Basically split my data at the comma into individual rows?
create table Testdata(SomeID int, OtherID int, Data varchar(max))
insert Testdata select 1, 9, '18,20,22'
insert Testdata select 2, 8, '17,19'
insert Testdata select 3, 7, '13,19,20'
insert Testdata select 4, 6, ''
--The query
;with tmp(SomeID, OtherID, DataItem, Data) as (
select SomeID, OtherID, LEFT(Data, CHARINDEX(',',Data+',')-1),
    STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from Testdata
union all
select SomeID, OtherID, LEFT(Data, CHARINDEX(',',Data+',')-1),
    STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp
where Data > ''
)
select SomeID, OtherID, DataItem
from tmp
order by SomeID
-- OPTION (maxrecursion 0)
-- normally recursion is limited to 100. If you know you have very long
-- strings, uncomment the option
Output
SomeID  OtherID  DataItem
1       9        18
1       9        20
1       9        22
2       8        17
2       8        19
3       7        13
3       7        19
3       7        20
4       6   
9       11       1
9       11       2
9       11       3
9       11       4
etc.