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;

 运行后结果截图:

 

posted @ 2019-06-25 15:26  ラピスラズリ(Dawn)  阅读(227)  评论(0编辑  收藏  举报