sql - Syntax for STUFF/FOR XML PATH -


can see doing wrong syntactically getting

msg 156, level 15, state 1, line 33
incorrect syntax near keyword 'for'.

i boiled down query readability , still getting error in various ways i've tried execute it:

select      stuff((select *                            (select n',' + n'first'                  union                  select n',' + n'second')             xml path('')), 1, 1, '') 

also tried:

select      stuff((select *                            (select n',' + n'first'                  union                  select n',' + n'second'))             xml path(''), 1, 1, '') 

for while getting

the xml clause invalid in views, inline functions, derived tables, , subqueries when contain set operator. work around, wrap select containing set operator using derived table syntax , apply xml on top of it.

but amended code per exacthelp's post.

update

based on answer @nenadzivkovic came following did looking do:

with cte_perca_categories (     select          category = stuff((select ', ' + category                            perca_categories                            xml path('')), 1, 2, '') ) , cte_perca_tags (     select tag = (select ', ' + tag perca_tags xml path('')) ) select      concat((select * cte_perca_categories),            (select * cte_perca_tags)) 

output (where tag begins 'food'):

charitable giving, nondiscretionary spending, saving, discretionary spending, food, dining, groceries, bills 

your error has nothing neither stuff nor for xml. it's syntax error selecting subquery.

select * (select n',' + n'first' union select n',' + n'second')  

you need give alias query:

select * (select n',' + n'first' union select n',' + n'second') x 

after you'll error need name column 1 of 'x', should add alias well.

select * (select n',' + n'first' col1 union select n',' + n'second') x 

your whole query this:

select stuff((select *  (select n',' + n'first' col1 union select n',' + n'second') x xml path('')), 1, 1, '') 

it work, pretty sure result gives not looking for.

(no column name) -------------------------------------- col1>,first</col1><col1>,second</col1> 

you looking concatenate string sub-query? should this:

with cte_source  (     select n',' + n'first' col1 union select n',' + n'second' ) select distinct stuff (     (select '' + col1 cte_source xml path ('')), 1, 1, '' )  cte_source 

to get:

(no column name) --------------- first,second 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -