This procedure has three parameters. But when I try to execute by passing parameters it shows me an error. Please help me.
create procedure queryfunctions @Tabname varchar(150),@colname varchar(150),@valuesname varchar(150)
as
begin
declare @sql varchar(4000)
select @sql='select * from @Tabname where @colname=@valuesname'
exec(@sql)
end
exec queryfunctions 'education','eduChildName','Revathi'
Error :
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable “@Tabname”.
Here is a much safer alternative:
What did I change?
dboprefix when creating / referencing objects.NVARCHARand can be longer than 150 characters. Much safer to allow the parameters to accommodate a table someone might add in the future.SET NOCOUNT ONas a guard against network overhead and potentially sending erroneous result sets to client.@sqlshould always beNVARCHAR.QUOTENAMEaround entity names such as tables or columns to help thwart SQL injection and also to guard against poorly chosen names (e.g. keywords).