sql server - how to pass table name as a parameter in UD scaller function? -
create function test ( @tblname sysname, @category varchar(50) ) returns int begin declare @val2 int select @val2=val1 @tblname val1=@tblname return @val2 end
whats wrong query, it's throwing following error
msg 1087, level 15, state 2, procedure test, line 11
must declare table variable "@tblname".
you can't parameterise table name. need use dynamic sql (not allowed in functions anyway).
you might able use view or cte union all
s fixed list of tables , constant whatever trying do. code have posted makes little sense (you want @val2
have same value @tblname
if table called value of @tblname
contains same value in val1
column?!) like
;with cte ( select 'table1' table_name, val1 table1 union select 'table2' table_name, val1 table2 ) select @val2=val1 cte table_name=@tblname , val1 = @tblname
Comments
Post a Comment