You cannot use the case .. when you need to switch between the table in the FROM clause (as you can in the conditionalORDER BY ). i.e. as follows:
select * from
case when 1=1
then t1
else t2
end;
will not work.
, SQL. , , , @id :
DECLARE @sql NVARCHAR(MAX)
SET @sql = 'select [filename] from **TABLE** where id = @id';
SET @sql = REPLACE(@sql, '**TABLE**', @table);
sp_executesql @sql, N'@id INT', @id = @id;
Sql, , unparameterized, (, @table), Sql Injection. , , @table , , @table .