sql - Stored procedure with a parameter in FROM clause -


is possible create stored procedure uses parameter in from clause?

for example:

create procedure [dbo].[getmaxid] @id varchar(50) @table varchar(50) begin select max(@id) @table end 

you cannot pass identifiers parameters query (neither table names nor column names). solution use dynamic sql. syntax suggests sql server, like:

create procedure [dbo].[getmaxid] (     @id varchar(50)     @table varchar(50) ) begin     declare @sql nvarchar(max);      set @sql = n'select max(@id) @table';     set @sql = replace(replace(@sql, '@id', quotename(@id)), '@table', quotename(@table));      exec sp_executesql @sql; end;  -- getmaxid 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -