JSON 을 자동 으로 SQL 표 데이터 로 변환 *

21423 단어 데이터 뱅 크
JSON 을 SQL 표 데이터 사용자 로 자동 으로 전환 하면 서로 다른 수요 에 따라 변경 할 수 있 으 며 데이터 원본 은 최종 적 으로 조회 되 었 을 뿐 지정 한 사용자 표를 삽입 하지 않 았 습 니 다.
 
 --          ,          
create function Get_StrArrayLength
(
  @str varchar(5000),  --       
  @split varchar(10)  --    
)
returns int
as
begin
 declare @location int
 declare @start int
 declare @length int


 set @str=ltrim(rtrim(@str))
 set @location=charindex(@split,@str)
 set @length=1
 while @location<>0
 begin
   set @start=@location+1
   set @location=charindex(@split,@str,@start)
   set @length=@length+1
 end
 return @length
end
--------------------- 
--          ,               
create function Get_StrArrayStrOfIndex
(
  @str varchar(5000),   --       
  @split varchar(10),   --    
  @index int --      
)
returns varchar(5000)
as
begin
 declare @location int
 declare @start int
 declare @next int
 declare @seed int
 
 
 set @str=ltrim(rtrim(@str))
 set @start=1
 set @next=1
 set @seed=len(@split)
 
 set @location=charindex(@split,@str)
 while @location<>0 and @index>@next
 begin
   set @start=@location+@seed
   set @location=charindex(@split,@str,@start)
   set @next=@next+1
 end
 if @location =0 select @location =len(@str)+1 
 return substring(@str,@start,@location-@start)
end
 
IF OBJECT_ID('sp_getJSONFields') IS NOT NULL
DROP PROCEDURE sp_getJSONFields
GO

CREATE PROCEDURE sp_getJSONFields
@Json VARCHAR(MAX)
AS
BEGIN
SELECT @Json=REPLACE(@Json,'{','')
SELECT @Json=REPLACE(@Json,'}',',')
DECLARE @temp VARCHAR(100)
DECLARE @objName VARCHAR(30)
DECLARE @objValue VARCHAR(30)
DECLARE @fieldSql VARCHAR(MAX)
SET @fieldSql='select '

WHILE LEN(@Json)>0
BEGIN
    SELECT @temp=SUBSTRING(@Json,0,CHARINDEX(',',@Json,0))
    --PRINT @temp 
    SELECT @Json=RIGHT(@Json,LEN(@Json)-LEN(@temp)-1)
    --PRINT @Json
    
    set @objName =left(@temp,CHARINDEX(':',@temp,0)-1)
    set @objValue =right(@temp,len(@temp)-CHARINDEX(':',@temp,0))
    
    --PRINT  @objName+'='+ @objValue+';'
    set @fieldSql=@fieldSql+REPLACE(@objValue,'"','''')+' as '+REPLACE(@objName,'"','')+','
    
    --PRINT '------------------'    
END
SET @fieldSql=LEFT(@fieldSql,LEN(@fieldSql)-1)

--EXEC sp_executesql @fieldSql
EXEC (@fieldSql)
END
GO
 

DROP TABLE #sql
CREATE TABLE #sql(
	id INT IDENTITY(1,1) NOT NULL,
	TXT nvarchar(max)
)
DECLARE @str varchar(5000) ---    JSON
set @str='{"VIN_Invalid":"1","VIN_ID":"427658","Veh_TypeCode":"CTRK","Year":"2011","Make":"TOYOTA","Veh_Model":"TUNDRA","Body_CD":"PK"},													{"VIN_Invalid":"1","VIN_ID":"427658","Veh_TypeCode":"CTRK","Year":"2011","Make":"TOYOTA","Veh_Model":"TUNDRA","Body_CD":"PK"}'
print dbo.Get_StrArrayLength(@str,'},')
declare @next int 
declare @s varchar(100) 
set @next=1
while @next<=dbo.Get_StrArrayLength(@str,'},')
BEGIN 
  INSERT INTO #sql VALUES(dbo.Get_StrArrayStrOfIndex(@str,'},',@next) +'}')
  PRINT dbo.Get_StrArrayStrOfIndex(@str,'},',@next) +'}'----       
  SET @next=@next+1
END
UPDATE #sql SET TXT=left(TXT,len(TXT)-1)  WHERE id=(SELECT TOP 1  MAX(id) FROM #sql)
 
--     
DECLARE @Json NVARCHAR(max) 
DECLARE cursor_name CURSOR FOR --    
		SELECT TXT FROM    #sql  
OPEN cursor_name --    
FETCH NEXT FROM cursor_name INTO  @Json
WHILE @@FETCH_STATUS = 0

 BEGIN
	 EXEC sp_getJSONFields @json
	 FETCH NEXT FROM cursor_name INTO @Json
END
CLOSE cursor_name --    
DEALLOCATE cursor_name --    

원문:https://blog.csdn.net/lijingrong_ljr / article / details / 47339353https://www.cnblogs.com/wancy86/p/JSON_FIELDS.html

좋은 웹페이지 즐겨찾기