sql解析xml

时间:2023-03-08 19:15:09
sql解析xml

我们有时候需要在sql中解析xml,xml解析sql实例如下:

  DECLARE @params xml
  DECLARE @customparams xml = null
  -- 0、解析输入参数
  DECLARE @filter nvarchar (max)  
  SET @filter = @params. value( '(/params/filter)[1]' , 'nvarchar(max)') 
  
  -- 0.2、 (2=2)
  DECLARE @customfilter nvarchar (max)  
  SET @customfilter = @params. value ( '(/params/customfilter)[1]', 'nvarchar(max)')
  IF @customfilter = N''
   SET @customfilter = N'2=2'
  
  -- 0.3、每页显示记录数
  DECLARE @pagesize int  
  SET @pagesize = @params. value( '(/params/pagesize)[1]' , 'int') 
  
  -- 0.4、页码
  DECLARE @pagenum int  
  SET @pagenum = @params. value( '(/params/pagenum)[1]' , 'int') 
  
  -- 0.5、排序字段
  )  
  SET @sortcol = @params. value( '(/params/sortcol)[1]' , 'nvarchar(100)') 
  IF @sortcol IS NULL OR @sortcol = ''
   SET @sortcol = N' p_Provider.ProviderGUID' -- 注意:根据XML中的定义设置默认排序 !!!
  ELSE
   SET @sortcol = @sortcol + N' ,p_Provider.ProviderGUID'    -- 注意:如果 XML中配置了实体主键,这里要拼接主键排序
  
  -- 0.6、当前公司
  )  
  SET @buguid = @customparams. value ( '(/params/BUGUID)[1]', 'nvarchar(100)') -- 注意:变量大小写与vb代码一致 !!!
  
  -- 0.7、当前产品服务 Code
  )  
  SET @productcode = @params. value ( '(/params/customfilter2)[1]', 'nvarchar(4000)')