• 拆分json格式数组


    把转换成

    declare @key nvarchar(3);
    declare @data nvarchar(max);
    declare    @code nvarchar(50) 
    declare    @countrycn nvarchar(100) 
    declare    @countryus nvarchar(100)
    declare    @provincecn nvarchar(100) 
    declare    @provinceus nvarchar(100) 
    declare    @citycn nvarchar(100) 
    declare    @cityus nvarchar(100) 
    declare    @cityid nvarchar(50) 
    declare @str nvarchar(1000);
    declare my_cursor cursor --定义游标
    for (select [key] from airportdemo  ) -- 把数据集导入游标
    
    open my_cursor  -- 打开游标
    fetch next from my_cursor into @key  --把游标中第一条放到变量key中
    while @@fetch_status=0      --循环遍历
    begin 
        select @code='',@countrycn='',@countryus='',@countrycn='',@provincecn='',@provinceus='',@citycn='',@cityus='',@cityid='';--初始化变量
        
        
        select @data = data from airportdemo where [key]=@key;
        print @data ;
        select @code = [key] from dbo.GF_StringSplit(@data);--根据data获得key,data数据表,并把key赋给@code;
        select @str = data from dbo.GF_StringSplit(@data);--@str获得data数据
        declare @firstData nvarchar(1000); 
        select @firstData= [string] from dbo.uf_StrSplit(@str,',') where str_id = 1;
        declare @secondData nvarchar(1000); 
        select @secondData= [string] from dbo.uf_StrSplit(@str,',') where str_id = 2;--把data按 ,分割开来 
        declare @thirdData nvarchar(1000);
        select @thirdData =[string] from dbo.uf_StrSplit(@str,',') where str_id = 3;
        declare @thirdCount int;
        set @thirdCount = dbo.Get_StrArrayLength(@thirdData,'|');
        declare @count int ;
        set @count = dbo.Get_StrArrayLength(@secondData,'|');
        if(@count = 4)
        begin
            select @countrycn= [string] from dbo.uf_StrSplit(@str,',') where str_id = 3;
            select @countryus = [string] from dbo.uf_StrSplit(@secondData,'|') where str_id = 1;--把 CHINA|阿里(NGQ)|97 阿里按'|'来分割 ,取下标为1的数据
            set       @cityus=substring(@firstData,2,len(@firstData)-1);
            select @cityid = [string] from dbo.uf_StrSplit(@secondData,'|') where str_id = 3; 
            select @citycn = [string] from dbo.uf_StrSplit(@secondData,'|') where str_id = 4; 
            
        end
        else if(@thirdCount=4)
        begin 
    
            select @countrycn= [string] from dbo.uf_StrSplit(@str,',') where str_id=4;
    
            select @countryus = [string] from dbo.uf_StrSplit(@thirdData,'|') where str_id = 1;
    
            select @cityid = [string] from dbo.uf_StrSplit(@thirdData,'|') where str_id = 3; 
            
            select @cityus = substring(@firstData,2,len(@firstData)-1);
            
            select @citycn = [string] from dbo.uf_StrSplit(@thirdData,'|') where str_id = 4;
    
            select @provincecn = [string] from dbo.uf_StrSplit(@str,',') where str_id=5;
    
            select @provinceus = [string] from dbo.uf_StrSplit(@str,',') where str_id=2;
    
        end
        else 
        begin 
            select @cityid=[string] from dbo.uf_StrSplit(@firstData,'|') where str_id = 3;
            select @citycn=[string] from dbo.uf_StrSplit(@firstData,'|') where str_id = 4;
            select @countryus=[string] from dbo.uf_StrSplit(@firstData,'|') where str_id = 1;
        end
        insert into dbo.TurnAirPort(code, countrycn, countryus, provincecn, provinceus, citycn, cityus, cityid) values(@code, @countrycn, @countryus, @provincecn, @provinceus, @citycn, @cityus, @cityid);--把数据插入数据表
        fetch next from my_cursor into @key; 
    end
    
    close my_cursor --关闭游标
    deallocate my_cursor  --删除游标
  • 相关阅读:
    一个有关FWT&FMT的东西
    【XSY2762】插线板 分块
    【XSY1905】【XSY2761】新访问计划 二分 树型DP
    【XSY2760】nonintersect 计算几何
    【XSY2787】Mythological VII 贪心
    【XSY2786】Mythological VI 数学
    【XSY2785】模型
    【BZOJ4126】【BZOJ3516】【BZOJ3157】国王奇遇记 线性插值
    【XSY2751】Mythological IV 线性插值
    【XSY2750】Mythological V 2-sat
  • 原文地址:https://www.cnblogs.com/hejinyang/p/3075983.html
Copyright © 2020-2023  润新知