SQL XML根据行数

时间:2017-05-15 15:13:35

标签: sql xml tsql

这有点难以解释......但我会尽我所能.....

我有一个非常好的SQL XML导出,但我需要添加一个额外的字段来记录一个wav文件位置。如果只有一个wav位置,这不会有问题,但是可能有一个或几个需要录制的wav位置。

在我的提取SQL中我想做类似于If语句或Case的事情当这样的时候只有一个文件位置我只有XML文件中的一个XML容器文件位置,如果有两个 - 我有我基本上不想要空容器。有人知道这是否可行?

我附上了一个代码示例,希望能让您了解我所要求的内容。

(SELECT --Agreement-- '一些文字'作为AdditionalInformation,

If @RecordingCount = 2  
    (SELECT 
            (Select 
                REC1.Recording_Location as MainRecordingRef
                FOR XML PATH('MainRecordingRef'), TYPE),
            (Select
                REC1.Recording_Location as MainRecordingRef
                FOR XML PATH('MainRecordingRef'), TYPE)
        FOR XML PATH('MainRecordingRefs'), TYPE),
End

If @RecordingCount = 3  
    (SELECT 
            (Select 
                REC1.Recording_Location as MainRecordingRef
                FOR XML PATH('MainRecordingRef'), TYPE),
            (Select
                REC1.Recording_Location as MainRecordingRef
                FOR XML PATH('MainRecordingRef'), TYPE),
                (Select
                REC1.Recording_Location as MainRecordingRef
                FOR XML PATH('MainRecordingRef'), TYPE)
        FOR XML PATH('MainRecordingRefs'), TYPE),
End

FOR XML PATH(' Agreement'),TYPE)

上面的代码不起作用,但想知道我是否需要采取不同的方法。任何建议将不胜感激。

修改

这是我目前输出的内容......

Agreement AgreementReference PP20170510-122300 AgreementReference MainRecordingRefs MainRecordingRef RecRef LOGOS-PP20170510-122300-9174.wav /RecRef /MainRecordingRef MainRecordingRef RecRef LOGOS-PP20170510-122400-9175.wav /RecRef /MainRecordingRef MainRecordingRef MainRecordingRef MainRecordingRef MainRecordingRefs Agreement

在上面我导出五个录音参考,其中两个有内容,其他三个是空的,但实际上只想导出非空的录音参考,所以它看起来像......

Agreement AgreementReference PP20170510-122300 AgreementReference MainRecordingRefs MainRecordingRef RecRef LOGOS-PP20170510-122300-9174.wav /RecRef /MainRecordingRef MainRecordingRef RecRef LOGOS-PP20170510-122400-9175.wav /RecRef /MainRecordingRef MainRecordingRefs Agreement

我可以在XML中做些什么来只显示非空的录音引用吗?

1 个答案:

答案 0 :(得分:1)

您的问题不太清楚(样本数据+预期输出最有帮助)。但是我的神奇晶体球告诉我,你想要将一个Wav放入你的XML中,并将更多的Wavs - 如果有的话 - 放入另一个节点集合......

这样的东西?

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/m                           akemigrations.py", line 111, in handle
    convert_apps=app_labels or None,
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/autodetector.py", line 42, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/autodetector.py", line 109, in _detect_changes
    self.old_apps = self.from_state.render(ignore_swappable=True)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 67, in render
    model.render(self.apps)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 312, in render
    body,
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 284, in __new__
    new_class._prepare()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 344, in _prepare
    signals.class_prepared.send(sender=cls)
  File "/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.7/dist-packages/model_utils/fields.py", line 57, in prepare_class
    % sender.__name__
AssertionError: To use StatusField, the model 'ShellRequest' must have a STATUS choices class attribute.

结果

DECLARE @mockup1 TABLE(ID INT,SomeData VARCHAR(100));
INSERT INTO @mockup1 VALUES (1,'Some 1 has 1 WAV')
                           ,(2,'Some 2 has 2 WAVs')
                           ,(3,'Some 3 has 3 WAVs');
DECLARE @mockup2 TABLE(ID INT,ID1 INT,WavUrl VARCHAR(100));
INSERT INTO @mockup2 VALUES(1,1,'URL 1')
                          ,(2,2,'URL 2-1')
                          ,(3,2,'URL 2-2')
                          ,(4,3,'URL 3-1')
                          ,(5,3,'URL 3-2')
                          ,(6,3,'URL 3-3');

SELECT m1.ID
      ,m1.SomeData
      --The first Wav goes in one extra node
      ,(SELECT w.ID
              ,w.WavUrl
        FROM @mockup2 AS w 
        WHERE w.ID1=m1.ID AND w.ID=(SELECT MIN(x.ID) FROM @mockup2 AS x WHERE x.ID1=m1.ID)
        FOR XML PATH('MainRecord'),TYPE
       )
      --All later Wavs go into a collection
      ,(SELECT w2.ID
              ,w2.WavUrl
        FROM @mockup2 AS w2
        WHERE w2.ID1=m1.ID AND w2.ID<>(SELECT MIN(x.ID) FROM @mockup2 AS x WHERE x.ID1=m1.ID)
        FOR XML PATH('Record'),ROOT('MoreRecords'),TYPE
        )
FROM @mockup1 AS m1
FOR XML PATH('Test'),ROOT('root');