T-SQL XML:如何以下面列出的格式获得结果?

时间:2018-06-13 13:25:17

标签: sql sql-server xml tsql

这是我第一次来这里。我需要以下列格式获取XML格式:

>>> print myfunc('1980', '3', '4')
('1980 is Ge Sh', 'and its l is somehow', 'the n is CND, the o is CHN', 'The p is Wrong input! and the q is Wrong input!.')

>>> print myfunc(1980, 3, 4)
('1980 is Ge Sh', 'and its l is somehow', 'the n is CND, the o is CHN', 'The p is CHN and the q is GBP.')

一旦我得到这种格式,那么我需要为表格的每一行生成这个XML。以下是预期结果集的屏幕截图。

Expected Result Set

提前谢谢。

2 个答案:

答案 0 :(得分:0)

虽然你的问题还不清楚,但我的magig水晶球告诉我,你可能正在寻找这样的东西:

import kotlinx.android.synthetic.main.your_custom_layout.*

class SelectCountryBottomSheet : BottomSheetDialogFragment() {

  override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
      val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
      dialog.setContentView(R.layout.your_custom_layout)
      dialog.some_custom_close_button.setOnClickListener { dismiss() }
      return dialog
  }
}

结果

DECLARE @someTable TABLE(SomeDate DATE, SomeQrt INT, SomeWeek INT, SomeCode INT);
INSERT INTO @someTable VALUES('20180122',1,4,101);

DECLARE @SomeRelatedTable TABLE(SomeText VARCHAR(100),SomeCode INT);
INSERT INTO @SomeRelatedTable VALUES('Row 1',101)
                            ,('Row 2',101)
                            ,('Other code',999);

SELECT YEAR(t.SomeDate) AS [@Year]
      ,t.SomeQrt AS [@QrtNum]
      ,t.SomeWeek AS [@WeekNum]
      ,t.SomeDate AS [@Date]
      ,t.SomeCode AS [@Code]
      ,(
        SELECT SomeText AS [@InnerText]
        FROM @SomeRelatedTable rt
        WHERE rt.SomeCode=t.SomeCode
        FOR XML PATH('InnerNode'),TYPE
       )
FROM @someTable t           
FOR XML PATH('Summary');

一些解释

<Summary Year="2018" QrtNum="1" WeekNum="4" Date="2018-01-22" Code="101"> <InnerNode InnerText="Row 1" /> <InnerNode InnerText="Row 2" /> </Summary> 允许通过列的别名和FOR XML PATH / PATH扩展程序完全控制XML生成。

使用相关的子查询创建相关(内部)节点。

答案 1 :(得分:0)

我能够解决我的问题。请看下面的场景。

drop table #test
create table #test(id int identity(1,1),locationcode varchar(6),businessdate date, quarternum int, weeknum int)

insert into #test(locationcode,businessdate, quarternum, weeknum)
values('000001',GETDATE(),2,2),
('000001',GETDATE(),2,2),
('000002',GETDATE(),2,2),
('000003',GETDATE(),2,2),
('000004',GETDATE(),2,2),
('000005',GETDATE(),2,2),
('000006',GETDATE(),2,2),
('000007',GETDATE(),2,2)

SELECT 
    (SELECT YEAR(b.businessdate) AS '@Year',b.QuarterNum AS '@QtrNum',b.WeekNum AS '@WeekNum',FORMAT(b.BusinessDate,'MM/DD/YYYY') AS '@Date'
                ,b.LocationCode  AS '@Code' 
     FROM #test c where b.id = c.id FOR XML PATH('DailySummary'),TYPE) AS Row
FROM #test b