Konuyu Oyla:
  • Derecelendirme: 4.5/5 - 2 oy
  • 1
  • 2
  • 3
  • 4
  • 5
SQL de JSON Islemleri
#7
Json oluşturmak için Farklı bir Metotdda ben paylaşayım :
 
tsql -> for json auto ile  bir ihtiyaçtan hasıl olan 3 farklı tablodan iç içe geçmiş nested related child json object oluşturma kodunu örnekleyip paylaşıyorum. 
bu mantıkla çok daha fazla derinlikli json objectler de oluşturabilirsiniz.
Önce tsql cursorlar ile yapayim dedim , sonra datasetlerle falan,  ama bi yığın koda döngülere gerek yok diye daha sade olan bu birkaç satırlık sqli yazdım. Belki işinize yarar.

önce test amaçlı ilişkisel örnek verimizi oluşturuyoruz.
declare @t1 table ( ad varchar(30) )
insert into @t1 values ('abdullah')
insert into @t1 values ('Emrah')
insert into @t1 values ('Emrullah')
declare @t2 table ( ad varchar(30) , yas int )
insert into @t2 values ('abdullah' ,30)
insert into @t2 values ('abdullah' ,31)
insert into @t2 values ('Emrah' ,40)
insert into @t2 values ('Emrah' ,41)
insert into @t2 values ('Emrullah' ,90)
insert into @t2 values ('Emrullah' ,91)
declare @t3 table ( ad varchar(30) , yas int , op int )
insert into @t3 values ('abdullah' ,30 , 300)
insert into @t3 values ('abdullah' ,30 , 301)
insert into @t3 values ('abdullah' ,30 , 302)
insert into @t3 values ('abdullah' ,31 , 310)
insert into @t3 values ('abdullah' ,31 , 311)
insert into @t3 values ('abdullah' ,31 , 312)
insert into @t3 values ('Emrah' ,40 , 401)
insert into @t3 values ('Emrah' ,40 , 402)
insert into @t3 values ('Emrah' ,41 , 403)
insert into @t3 values ('Emrah' ,41 , 404)
insert into @t3 values ('Emrullah' ,90 , 901)
insert into @t3 values ('Emrullah' ,90 , 902)
insert into @t3 values ('Emrullah' ,91 , 903)
insert into @t3 values ('Emrullah' ,91 , 904)



 bu üstteki 3 tabloyu bağlayarak iç içe geçmiş 3 seviyeli nested related child json objectimizi oluşturuyoruz.
select * from (
select t1.ad ,
( select yas
, ( select op from  @t3 t3 where t3.ad = t2.ad  and t2.yas = t3.yas for json auto, include_null_values ) child
from  @t2 t2
where t2.ad = t1.ad  for json auto, include_null_values ) child  
from @t1 t1  
) q for json auto, include_null_values 


Sonuç : 
[
{"ad":"abdullah","child":[
                                     {"yas":30,"child":
                                       [
                                          {"op":300},
                                          {"op":301}, 
                                          {"op":302}
                                       ]},
                                     {"yas":31,"child":
                                        [
                                           {"op":310},
                                           {"op":311},
                                           {"op":312}
                                        ]
                                     }
                                  ]
 },
{"ad":"Emrah","child":[{"yas":40,"child":[{"op":401},{"op":402}]},{"yas":41,"child":[{"op":403},{"op":404}]}]},
{"ad":"Emrullah","child":[{"yas":90,"child":[{"op":901},{"op":902}]},{"yas":91,"child":[{"op":903},{"op":904}]}]}
]

İmkanın sınırı, imkansızın yanıbaşındadır. Denemeden bilemezsin.
Cevapla


Bu Konudaki Yorumlar
SQL de JSON Islemleri - Yazar: adelphiforumz - 16-04-2019, Saat: 00:42
SQL de JSON Islemleri - Yazar: Tuğrul HELVACI - 16-04-2019, Saat: 08:27
SQL de JSON Islemleri - Yazar: adelphiforumz - 16-04-2019, Saat: 10:27
Cvp: SQL de JSON Islemleri - Yazar: TescilsizUzman - 16-04-2019, Saat: 11:48
SQL de JSON Islemleri - Yazar: adelphiforumz - 16-04-2019, Saat: 12:25
Cvp: SQL de JSON Islemleri - Yazar: Bay_Y - 16-04-2019, Saat: 12:26
Cvp: SQL de JSON Islemleri - Yazar: bydelphi - 21-06-2023, Saat: 14:27

Konu ile Alakalı Benzer Konular
Konular Yazar Yorumlar Okunma Son Yorum
  MSSQL Data downgrade (Alt sürüme veri aktarma) işlemleri adelphiforumz 0 540 23-03-2023, Saat: 11:13
Son Yorum: adelphiforumz
  SQL 2014 ve Öncesi için JSON Parse konusunda yardım adelphiforumz 2 1.143 08-07-2022, Saat: 11:40
Son Yorum: hi_selamlar
  MSSQL Network Uzerine Backup ve Restore işlemleri adelphiforumz 5 3.716 19-03-2020, Saat: 13:39
Son Yorum: adelphiforumz



Konuyu Okuyanlar: 1 Ziyaretçi