I've got very little experience in SQL server so I am learning this as I go along (hence a recent post you may have spotted regarding parameterising a create database statement).
I am trying to write a script to create a new database schema, add several tables and stored procedures. The script works fine syntaxically, no errors are generated. The issue I am having is the new database is created as I would expect but the part of the script that creates the tables and stored procedures does so in the MASTER schema not the newly database schema. I am not running the script as the database owner, because part of the script creates the database I am then trying to create objects in.
I am using the SQL Server management console to run my commands. You have to select a schema to run the script against that schema but I though with me using the "USE database" command it would create the tables in the correct schema. The whole script is massive so I am not going to include it all (1350 lines). I know that the code to create the new schema works because that has been tested and appears as a schema to be used. I have issued a GO statement between each section to make sure statements are being sent to the server for processing. The results of what I have tried so far are as follows:
The code below works and creates the table in the SAW_LIVE schema as expected (hardcoding the schema name in the create table statement).
The following also works, again hardcoding the USE statement to reference the Saw_Live (works with or without the GO):
If I parameterise the script so I can specify the database name in a variable and run this the table is created the database in the MASTER database schema not the SAW_LIVE schema which I require. Regardless of if the GO is included or not.
The only way I can get a parameterised script to work is if I build the entire CREATE TABLE command into a variable and execute that like so:
The main reasons for wanting to parameterise scripts is we may need to recreate the database on different servers with different name, locations etc and its useful to have a script I can change 3 or 4 references rather than having to do a search and replace excercise. I may just bite the bullet and have to put all the hardcoded values back in because building exec statements for each table is not what I want to do. It just seems silly that the DECLARE command isn't of much use outside EXEC commands. I understand for one offs this is a lot of work but I have used the opportunity as a learning experience to see what's possible so as to understand how things work on SQL server.
Thanks in advance,
Mark.
Mark Davies
Warwickshire County Council
I am trying to write a script to create a new database schema, add several tables and stored procedures. The script works fine syntaxically, no errors are generated. The issue I am having is the new database is created as I would expect but the part of the script that creates the tables and stored procedures does so in the MASTER schema not the newly database schema. I am not running the script as the database owner, because part of the script creates the database I am then trying to create objects in.
I am using the SQL Server management console to run my commands. You have to select a schema to run the script against that schema but I though with me using the "USE database" command it would create the tables in the correct schema. The whole script is massive so I am not going to include it all (1350 lines). I know that the code to create the new schema works because that has been tested and appears as a schema to be used. I have issued a GO statement between each section to make sure statements are being sent to the server for processing. The results of what I have tried so far are as follows:
The code below works and creates the table in the SAW_LIVE schema as expected (hardcoding the schema name in the create table statement).
Code:
CREATE TABLE [Saw_Live].[dbo].[adiin] (
[adiin_id] [INT] IDENTITY (1, 1) NOT NULL ,
[instructid] [VARCHAR] (15) DEFAULT '' COLLATE Latin1_General_CI_AS NOT NULL ,
[lastoffer] [INT] DEFAULT 0 NOT NULL
) ON [PRIMARY]
The following also works, again hardcoding the USE statement to reference the Saw_Live (works with or without the GO):
Code:
USE Saw_Live
GO
CREATE TABLE [dbo].[adiin] (
[adiin_id] [INT] IDENTITY (1, 1) NOT NULL ,
[instructid] [VARCHAR] (15) DEFAULT '' COLLATE Latin1_General_CI_AS NOT NULL ,
[lastoffer] [INT] DEFAULT 0 NOT NULL
) ON [PRIMARY]
If I parameterise the script so I can specify the database name in a variable and run this the table is created the database in the MASTER database schema not the SAW_LIVE schema which I require. Regardless of if the GO is included or not.
Code:
-- The Database Name
DECLARE @DataBaseName VARCHAR(MAX)
SET @DataBaseName = 'Saw_Live'
-- Command variable, used for building statements and EXEC'ing them
DECLARE @cmd VARCHAR(MAX)
-- Select the database
SET @cmd = 'USE ' + @DataBaseName
EXEC (@cmd)
GO
CREATE TABLE [dbo].[adiin] (
[adiin_id] [INT] IDENTITY (1, 1) NOT NULL ,
[instructid] [VARCHAR] (15) DEFAULT '' COLLATE Latin1_General_CI_AS NOT NULL ,
[lastoffer] [INT] DEFAULT 0 NOT NULL
) ON [PRIMARY]
The only way I can get a parameterised script to work is if I build the entire CREATE TABLE command into a variable and execute that like so:
Code:
-- The Database Name
DECLARE @DataBaseName VARCHAR(MAX)
SET @DataBaseName = 'Saw_Live'
-- Command variable, used for building statements and EXEC'ing them
DECLARE @cmd VARCHAR(MAX)
-- Select the database
SET @cmd= 'CREATE TABLE [' + @DataBaseName + '].[dbo].[adiin] ('
SET @cmd=@cmd+'[adiin_id] [INT] IDENTITY (1, 1) NOT NULL, '
SET @cmd=@cmd+'[instructid] [VARCHAR] (15) COLLATE Latin1_General_CI_AS NOT NULL , '
SET @cmd=@cmd+'[lastoffer] [INT] DEFAULT 0 NOT NULL '
SET @cmd=@cmd+') ON [PRIMARY]'
EXEC (@cmd)
The main reasons for wanting to parameterise scripts is we may need to recreate the database on different servers with different name, locations etc and its useful to have a script I can change 3 or 4 references rather than having to do a search and replace excercise. I may just bite the bullet and have to put all the hardcoded values back in because building exec statements for each table is not what I want to do. It just seems silly that the DECLARE command isn't of much use outside EXEC commands. I understand for one offs this is a lot of work but I have used the opportunity as a learning experience to see what's possible so as to understand how things work on SQL server.
Thanks in advance,
Mark.
Mark Davies
Warwickshire County Council