0
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?

9
  • 1
    You might have to use sp_executesql for this. Pass your sql string in executesql and then try t execute it. Commented Apr 13, 2015 at 10:13
  • can you provide an example? Commented Apr 13, 2015 at 10:14
  • Avoid the use of the sp_ prefix when naming procedures. This prefix is used by SQL Server to designate system procedures. Commented Apr 13, 2015 at 10:21
  • Thanks for the info and what about the real problem? Commented Apr 13, 2015 at 10:22
  • check this thread stackoverflow.com/questions/20678725/… Commented Apr 13, 2015 at 10:30

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.