Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:03:57+00:00 2026-05-16T05:03:57+00:00

I would like to have your opinions regarding best practices to adopt in SQL

  • 0

I would like to have your opinions regarding best practices to adopt in SQL scripting to install a Data Base.

PROBLEM A)
In my script I have several Batches to create Tables.
Tables have many Foreign Keys to each others, at the moment I must arranges batches in the right order to avoid conflict with FK Tables.
I would like to know if could be a good practice create Tables and all columns without FK first, and at the end of the script ALTER such tables adding FK.

PROBLEM B)
My script should be use to create different DB on different Servers.
Data Base could have different name on every installation.
Now in my script I create a Database using:

CREATE DATABASE NameX

and:

USE NameX

to use it.

Because I would need update manually the script for every installation. I was thinking would be great to have a CENTRALIZED way for naming the Data Base inside a the script.
In this way changing a simple variable would create the Data Base with my name and all USE statements.
I tried to use LOCAL VARIABLES, but without success because after GO statements they go out of scope.
I do not have any experience in using sqlcmd and variables there.

Any idea how to solve it inside my script?
PS: I use MS SQL 2008 and I will load my script in MS SMS

Thanks guys for your help, this community is great 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:03:58+00:00Added an answer on May 16, 2026 at 5:03 am
    • avoid using “USE DATABASE”
    • separate the database creating script and data object creating scripts
    • use some code (Setup, Deploy) to execute creating database script by replacing @database_name with real name

    alternative:

    • use some replacement tool to prepare scripts before deploy (it just replace your @@@database_name@@@ with real name)
    • use bat file to prepare scripts

    alternative

    • use Database Project in the Visual Studio. VS can generate some variables that setup projects can change in the deploy process..
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I would like have solved via a SQL query.
I would like to have your opinion and suggestion regarding the solution to this
I would like your opinions on what the best/fastest way to download files from
I would like to ask your opinions regarding the way I handle MultiTenancy. Im
I have a little experience with WCF and would like to get your opinion/suggestion
I have an architecture scenario and I would like to discuss to get your
I have one query on which I would like to get your valuable feedback.
I have read the documentation ( http://dev.mysql.com/doc/refman/5.1/en/partitioning.html ), but I would like, in your
I would just like your feedback on something. Basically I have a value called
I would like to know your opinions on which you find is a better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.