Saturday, October 3, 2015

Variable table name ms sql server

no allowed use dynamic sql instead.

build dynamic sql and then use exec or sp_executesql to run query

EXEC | EXECUTE

Example
DECLARE @table VARCHAR(50) = 'HR.Employees';
EXEC ( 'SELECT * FROM '+@table );

Example 2 [Stored Procedure]
DECLARE @ID INT = 34;
EXEC ( '[dbo].[p__Clean] '''+@ID+'''' );

No comments:

Post a Comment