SQL Server将列以分隔符分割后存到临时表

begin
    if object_id('tempdb..#t') is not null  drop table #t; 
    create table #t
    (
        filepath nvarchar(300)
    );
    declare @filePathStr nvarchar(max),
        @endIndex int = 1,
        @currentFilePath nvarchar(300),
        @sql nvarchar(max);
    set @filePathStr = '1;235;67456;2667;35;3;67';
    set @filePathStr += ';';
    while CHARINDEX(';',@filePathStr) > 0
    begin
        set @endIndex = CHARINDEX(';',@filePathStr) - 1;
        print(@endIndex);
        set @currentFilePath = SUBSTRING(@filePathStr,1,@endIndex);
        print(@currentFilePath);
        set @filePathStr = SUBSTRING(@filePathStr,@endIndex + 2,LEN(@filePathStr) - LEN(@currentFilePath));
        print(@filePathStr);
        insert into #t(filepath)
            select @currentFilePath;
    end;
    select * from #t;
end;

 运行后结果截图:

SQL Server将列以分隔符分割后存到临时表_第1张图片

 

转载于:https://www.cnblogs.com/JimmySeraph/p/11083082.html

你可能感兴趣的:(SQL Server将列以分隔符分割后存到临时表)