Executin SQL statement from variable in functions

Hello  All

Within my function depends on value forming a SQL statement

As Example

set @SqlString = 'select ' + @ColumnName + ' from ' + @tableName + ' where ' + @whereCond

How can i execute this Statement. within function

Thanks in advance

RK




Answer this question

Executin SQL statement from variable in functions

  • Pommie Phil

    looks like the site was down (http://www.smmarskog.se/dynamic_sql.html)

    is there a way i can a open cursor with dynamic sql.

    Our requirement is we have set of related tables with different data but all integer columns

    We are trying to create few statistical functions/procs  So user can pass Table name and column name and optionally any conditions,  so that my function/procs returns results.



  • Bogey1

    Sorry, typo
    http://www.sommarskog.se/dynamic_sql.html
    
    > looks like the site was down 
    
    
    


  • jhangeirakhtar300

    > Within my function depends on value forming a SQL statement
    
    Sorry, no can do, you cannot execute dynamic SQL in a function. Maybe you 
    meant to use a stored procedure, not a function. 
    
    
    


  • Hernan93

    I can change function to stored procedure  within storedprocedure how can i execute a dynamic SQL 

    My requirement is to open cursor with dynamic SQL.
    table name, columns list and conditions are depends on user parameters



  • Alain Vezina

    > My requirement is to open cursor with dynamic SQL.
    
    You need a cursor and dynamic SQL. Fantastic. First, please read this:
    
    http://www.smmarskog.se/dynamic_sql.html
    
    Then, if you can give better specs, maybe we can help.
    
    http://www.aspfaq.com/5006 
    
    
    


  • Executin SQL statement from variable in functions