I have the code below which I am going to use in the setup of databases. It works but I want to parametrise the database name in the "if exists select * ..." statement. I am very new to SQL Server and have tried to find examples of basic selects using the "EXEC" command with where clauses that have varchar criteria checks without success. I have seen some examples that suggest using the SP_EXECUTESQL command. If anyone can give me some advice on this it would be appreciated. I want to have to avoid having 3 different scripts to do the same thing if I can parameterise things.
Many thanks,
Mark.
Mark Davies
Warwickshire County Council
Many thanks,
Mark.
Code:
--------------------------------------------------------------------------------
-- DECLARE VARIABLES
--------------------------------------------------------------------------------
-- The Database Name
DECLARE @DatabaseName VARCHAR(MAX)
SET @DatabaseName = 'Test'
-- Command variable, used for building statements and EXEC'ing them
DECLARE @cmd VARCHAR(MAX)
-- Check for table and delete
IF EXISTS (SELECT *
FROM [test].[dbo].[sysobjects]
WHERE sysobjects.type = 'U'
AND sysobjects.name='table1')
SET @cmd= 'USE '+@DatabaseName+ ' ' + 'DROP TABLE [dbo].[table1]'
EXEC (@cmd)
Mark Davies
Warwickshire County Council