CREATE PROCEDURE dbo.sp_data_archive ( @DataBaseName varchar(100), ) AS BEGIN INSERT INTO [@DataBaseName].dbo.TESTTABLE INSERT INTO dbo.TestTable END Giving error :Invalid object name '@DataBaseName.dbo.TESTTABLE'.
How to solve this?
CREATE PROCEDURE dbo.sp_data_archive ( @DataBaseName varchar(100), ) AS BEGIN INSERT INTO [@DataBaseName].dbo.TESTTABLE INSERT INTO dbo.TestTable END Giving error :Invalid object name '@DataBaseName.dbo.TESTTABLE'.
How to solve this?
You should use SP_EXECUTESQL for this purpose
For reference, check the code given below.
alter procedure test_Sp ( @DBName1 nvarchar(100), @DBName2 nvarchar(100) ) as begin DECLARE @sql NVARCHAR(500) SET @sql = N'select * from ' + @DBName1 + '.sys.tables' EXEC SP_EXECUTESQL @SQL SET @sql = N'select * from ' + @DBName2 + '.sys.tables' EXEC SP_EXECUTESQL @SQL end