Constructing a user-defined-function that returns a table (or other alternatives), to make query more readable
12:57 17 Jul 2012

I currently have a section in my query that looks similar to this

, T3 AS (
          select 'FSA'            as tType, b.fsacd  as tBefore, c.fsacd  as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Scale'          as tType, b.scd    as tBefore, c.scd    as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Retail Source'  as tType, b.rsc    as tBefore, c.rsc    as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Mix Match'      as tType, b.mmcd   as tBefore, c.mmcd   as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Price Entry'    as tType, b.pecd   as tBefore, c.pecd   as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID 
    union select 'Qntty Entry'    as tType, b.qecd   as tBefore, c.qecd   as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID    
    union select 'Price 3 Decs'   as tType, b.p3d    as tBefore, c.p3d    as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID    
    union select 'Tare Entry'     as tType, b.tecd   as tBefore, c.tecd   as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID    
    union select 'Undiscountable' as tType, b.undsc  as tBefore, c.undsc  as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID    
    union select 'Foodstamp'      as tType, b.fds    as tBefore, c.fds    as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID    
    union select 'WIC'            as tType, b.wic    as tBefore, c.wic    as tAfter from T1 as a , T2 as b,T2 as c where a.beforeID = b.tID and a.afterID =c.tID    
)

it works fine, but I would like to make it look more compact. Is there a way to construct a function foo such that I coul accomplish the same results by doing something that may look like this

, T3 AS (
          foo('FSA'             ,fsacd  ,T1, T2 ) 
    union foo('Scale'           ,scd    ,T1, T2 ) 
    union foo('Retail Source'   ,rsc    ,T1, T2 ) 
    union foo('Mix Match'       ,mmcd   ,T1, T2 ) 
    union foo('Price Entry'     ,pecd   ,T1, T2 ) 
    union foo('Qntty Entry'     ,qecd   ,T1, T2 )   
    union foo('Price 3 Decs'    ,p3d    ,T1, T2 )   
    union foo('Tare Entry'      ,tecd   ,T1, T2 )   
    union foo('Undiscountable'  ,undsc  ,T1, T2 )   
    union foo('Foodstamp'       ,fds    ,T1, T2 )   
    union foo('WIC'             ,wic    ,T1, T2 )   
)
sql union sybase readability user-defined-functions