xml parsing - Sybase ASE xmltable - pattern not working -
i trying following sybase ase 15.0.2 finding hard through:
the motive extract contents of <tables> tag. me this?
declare @purgetableinfo varchar(16300) select @purgetableinfo = '<purge> <start-time>00:00:000</start-time> <end-time>03:00:000</end-time> <tables> <table> <table_name>table1</table_name> <owner>dbo</owner> <columns> <column> <column_name>column1</column_name> <column_value>121212xdfsdsdsdsd</column_value> <column_condition>like</column_condition> </column> <column> <column_name>column2</column_name> <column_value>121212xdfsdsdsdsd</column_value> <column_condition>like</column_condition> </column> </columns> </table> </tables> </purge>' select * xmltable('/purge/tables/table/columns/column' passing @purgetableinfo columns columnname varchar(255) path 'column_name', tablename varchar(255) pattern '../../table_name') purgeinputdetails
in above question, trying avoid looping , trying use xmltable() query. but, turns out that, xmltable() little rigid more complex structures multiple tables , columns xml (in question, mentioned base case of single table , multiple columns xml). so, going "xmlextract()+looping" way. here solution multiple table/ multiple columns xml:
declare @i int declare @extractedtable varchar(50) declare @j int declare @extractedcolumn varchar(50) select @i = 1 select @extractedtable = '' while (@extractedtable != null) begin select @extractedtable=convert(varchar(50),xmlextract ('/purge/tables/table['+convert(varchar(5),@i)+']/table_name/text()', @purgetableinfo)) print "%1!", @extractedtable if(@extractedtable != null) begin select @j = 1 select @extractedcolumn = '' while (@extractedcolumn != null) begin select @extractedcolumn=convert(varchar(50),xmlextract ('/purge/tables/table['+convert(varchar(5),@i)+']/columns/column['+convert(varchar(5),@j)+']/column_name/text()', @purgetableinfo)) print "%1!", @extractedcolumn if(@extractedcolumn != null) begin insert tabcols select @extractedtable tablename, @extractedcolumn columnname select @j = @j+1 end end select @i = @i+1 end end commit
Comments
Post a Comment