sql server - How can I run a SQL query iteratively for every row in a table? -


i have following query:

declare @accstring varchar(max) set @accstring=''   select @acctring=@accstring + description + '  [   ]    '  tl_sb_accessoryinventory ai join tl_sb_accessory on a.accessoryid = ai.accessoryid userid=6   select userid, servicetag, model, @accstring accessories tl_sb_oldlaptop ol join tl_sb_laptoptype lt on ol.laptoptypeid = lt.laptoptypeid userid=6 

which outputs this:

enter image description here

what want able run every userid in table tl_sb_user.

the statement userids is:

select userid tl_sb_user 

how can output row above each user?

you trying string concatenation subquery. in sql server, need string concatenation using correlated subquery for xml path. arcane, works.

the results this:

select userid, servicetag, model, @accstring accessories,        stuff((select  '  [   ]    ' + description               tl_sb_accessoryinventory ai join                    tl_sb_accessory                    on a.accessoryid = ai.accessoryid               a.userid = ol.userid               xml path ('')              ), 1, 11, '') accessories tl_sb_oldlaptop ol join      tl_sb_laptoptype lt      on ol.laptoptypeid = lt.laptoptypeid; 

you don't have table aliases identifying columns come from, guessing a.userid = ol.userid references right tables.

also, substitutes characters html forms. notably '<' , '>' turn things '&lt;' , '&gt;'. when encounter problem, use replace() replace values.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -