passing table name and column name as a parameter

  • hi, all

    i am trying to make a function in which i want to pass a coloumn and a table .

    these columnn and table will be used inside the function body.

    so i want to know how can i pass a column and a table as a paramerer in creating a function or a stored procedure . what are the datatypes which can be used for this???

    i am new to sql server so plz be simple in language so that i can understand it better..

    thanx

    hv fun 🙂

  • Use datatype SYSNAME.

  • i have used sysname but it still not working

    can u give me the syntax

    i have used like below :

    CREATE FUNCTION fun_name (@column_name sysname , @table_name sysname)

    RETURNS sysname

    BEGIN

    RETURN (SELECT @column_name FROM @table_name)

    END

    how should i pass the parameter while calling the function ???

    thanx 4 the reply

  • You can't have dynamic SQL in a function.

    http://www.sommarskog.se/dynamic_sql.html


    N 56°04'39.16"
    E 12°55'05.25"

  • I think perhaps you've missed something here. You write a subroutine (stored procedure, function, view, etc) in SQL to interact with a specific set of tables. Passing table names around often indicates some lack of understanding or inappropriate database design. Could you please indicate what you're trying to achieve (apart from the obvious which is retrieve some data from a random column in a random table) and we may be able to guide you to avoid the function altogether.

  • thanx 4 asking more 🙂

    actually i am new to sql server so i am trying to go through all aspects of that.

    i was trying to make a function which can perform addition operation on all the data of a given column of a given table .......... i know there is a built-in function 4 that i.e SUM but i was trying to make my own function .

    so create that function i need column and table as a variable.

    i can make a stored procedure but i am not able to create a function 4 the same

    thanx 🙂

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply