0

有没有办法使用带有 SQL Server 存储过程的 OpenXML 来传递多个参数而不使用临时表(这真的让我们慢了下来?)?

例如,使用单个参数:

exec usp_FooBar 
'<Data><States><State StateID="16" StateName="Massachusetts" "/></States></Data>'

usp_FooBar我得到的StateID价值

Declare @StateID int
.
.
.

 SELECT @StateID = StateID FROM  
  OPENXML(@IDOC,'/Data/States/State',1)   
  WITH      
  (      
   StateID INT    
  ) 

但是,假设我想传递多个状态:

exec usp_FooBar 
'<Data><States><State StateID="16" StateName="Massachusetts" "/><State StateID="87" StateName="Minnesota" /></States></Data>'

有什么方法可以做类似于单状态场景的事情,我可以接受任意数量的参数,而不使用临时表(正如我所提到的,这对我的性能产生了重大影响)?

4

1 回答 1

1

例子

Declare @XML xml = '
<Data>
    <States>
        <State StateID="16" StateName="Massachusetts" />
        <State StateID="25" StateName="New York" />
        <State StateID="13" StateName="Rhode Island" />
    </States>
</Data>
'


Select Grp    = r.value('@StateID','int')
      ,Item   = attr.value('local-name(.)','varchar(100)')
      ,Value  = attr.value('.','varchar(max)') 
 From  @XML.nodes('/Data/States/State') as A(r)
 Cross Apply A.r.nodes('./@*') AS B(attr)

退货

Grp Item        Value
16  StateID     16
16  StateName   Massachusetts
25  StateID     25
25  StateName   New York
13  StateID     13
13  StateName   Rhode Island
于 2017-05-31T15:50:54.843 回答