I am working on a proc which is present in one database, it picks table from other source database (passed as parameter) and insert missing values to other destination database (again passed as parameter). My insert query is a dynamic query and before executing it, I want to check existence of both the source and destination tables. I don't want to execute dynamic queries again just to check existence of tables as they are not advisable to use in bulk,.
Is there any way I can achieve something like below
USE @DbName
GO
IF EXISTS (
SELECT 1
FROM sys.tables
WHERE NAME = @table
AND type = 'U'
)
BEGIN
code here...
END
or
IF EXISTS (
SELECT 1
FROM @fulltableName -- where variable consists 'dbname.sys.tables'
WHERE NAME = @table
AND type = 'U'
)
BEGIN
code here...
END
with the help of only variables and without executing dynamic queries.