I would like to express my opinion on the best practices in SQL scripts for database installation.
PROBLEM A) In my script, I have several batches for creating tables. The tables have many foreign keys to each other, at the moment I have to organize the parties in the correct order to avoid conflict with the FK tables. I would like to know if it is good practice to create tables and all columns without FK at first, and at the end of the ALTER script, such tables add FK.
PROBLEM B) My script should be used to create different databases on different servers. The database may have a different name each time it is installed. Now in my script, I create a database using:
CREATE DATABASE NameX
and
USE NameX
to use it.
Because I will need to manually update the script for each installation. I thought it would be great to have a CENTRALIZED way to name the database inside the script. Thus, changing a simple variable would create a database with my name and all USE statements. I tried to use LOCAL VARIABLES, but to no avail, because after GO-statements they go beyond. I have no experience using sqlcmd and variables there.
Any idea how to solve it in my script? PS: I am using MS SQL 2008 and I will upload my script to MS SMS
Thanks guys for your help, this community is great :-)
source
share