how i set Table name and Column name dynmicalyy in Ms sql sp
-
hi all i have develping an application in vb.net with mssql express 2005, now i want to fetch records from a table with different conditions i want to use a parameter as a column name in which i want to pass differnt field names and an other parametr to pass condition value. here is the Sp by which i trying to fecth the data but could not.... pl help me CREATE PROCEDURE TEMP1EMPINFO --CREATE PROCEDURE TEMP1EMPINF ( @Tbl_col_name varchar(100), @Col_val varchar(100) ) AS Begin Select * from table where @tbl_col_name= @col_val end GO thanks
rmshah Developer
-
hi all i have develping an application in vb.net with mssql express 2005, now i want to fetch records from a table with different conditions i want to use a parameter as a column name in which i want to pass differnt field names and an other parametr to pass condition value. here is the Sp by which i trying to fecth the data but could not.... pl help me CREATE PROCEDURE TEMP1EMPINFO --CREATE PROCEDURE TEMP1EMPINF ( @Tbl_col_name varchar(100), @Col_val varchar(100) ) AS Begin Select * from table where @tbl_col_name= @col_val end GO thanks
rmshah Developer
You will have to build a SELECT statement dynamically and then execute it using sp_executesql[^]. But as far as I know it's not very good performancewise. Google for "dynamic SQL" and read about it. How many different values of
@Tbl_col_name
will you be using? Wouldn't it be easier to create a separate SP for each case? -
hi all i have develping an application in vb.net with mssql express 2005, now i want to fetch records from a table with different conditions i want to use a parameter as a column name in which i want to pass differnt field names and an other parametr to pass condition value. here is the Sp by which i trying to fecth the data but could not.... pl help me CREATE PROCEDURE TEMP1EMPINFO --CREATE PROCEDURE TEMP1EMPINF ( @Tbl_col_name varchar(100), @Col_val varchar(100) ) AS Begin Select * from table where @tbl_col_name= @col_val end GO thanks
rmshah Developer
Sure you can try this One... Declare @qry VarChar(1000), @clmname VarChar(100), @tabname VarChar(100) Set @qry = (Select @clmname from @tabname) Exec(@qry) hAVE A nICE DAY.... bY... :-D