• KGJ-Dev (11/29/2015)


    Hi Jeff,

    Thanks for your reply and basically i am planning to create generic function which will take "table name","column name to makecomma separated" and "where condition" and send the result as comma separated values.

    I can show you the dynamic SQL to pull this off but 1) it will always have to be a real table or a temp table because dynamic SQL doesn't like table variables (different scope) and 2) you won't be able to build a generic FUNCTION out of it because it's not possible (I hate saying that because someone will find a work around) to execute dynamic SQL in any kind of user defined function.

    As already pointed out, it will also need to be protected from SQL Injection.

    With all that in mind, I'd like to recommend that you just build a template around the code from the following article and write hard-code when you need it.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)