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

  • Home
  • SEARCH
  • 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 168771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:30:43+00:00 2026-05-11T12:30:43+00:00

I use the database name in several places in my script, and I want

  • 0

I use the database name in several places in my script, and I want to be able to quickly change it, so I’m looking for something like this:

DECLARE @DBNAME VARCHAR(50) SET @DBNAME = 'TEST'  CREATE DATABASE @DBNAME GO ALTER DATABASE @DBNAME SET COMPATIBILITY_LEVEL = 90 GO ALTER DATABASE @DBNAME SET RECOVERY SIMPLE  GO 

But it doesn’t work. So what’s the correct way to write this code?

  • 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. 2026-05-11T12:30:43+00:00Added an answer on May 11, 2026 at 12:30 pm

    Put the entire script into a template string, with {SERVERNAME} placeholders. Then edit the string using:

    SET @SQL_SCRIPT = REPLACE(@TEMPLATE, '{SERVERNAME}', @DBNAME) 

    and then run it with

    EXECUTE (@SQL_SCRIPT) 

    It’s hard to believe that, in the course of three years, nobody noticed that my code doesn’t work!

    You can’t EXEC multiple batches. GO is a batch separator, not a T-SQL statement. It’s necessary to build three separate strings, and then to EXEC each one after substitution.

    I suppose one could do something ‘clever’ by breaking the single template string into multiple rows by splitting on GO; I’ve done that in ADO.NET code.

    And where did I get the word ‘SERVERNAME’ from?

    Here’s some code that I just tested (and which works):

    DECLARE @DBNAME VARCHAR(255) SET @DBNAME = 'TestDB'  DECLARE @CREATE_TEMPLATE VARCHAR(MAX) DECLARE @COMPAT_TEMPLATE VARCHAR(MAX) DECLARE @RECOVERY_TEMPLATE VARCHAR(MAX)  SET @CREATE_TEMPLATE = 'CREATE DATABASE {DBNAME}' SET @COMPAT_TEMPLATE='ALTER DATABASE {DBNAME} SET COMPATIBILITY_LEVEL = 90' SET @RECOVERY_TEMPLATE='ALTER DATABASE {DBNAME} SET RECOVERY SIMPLE'  DECLARE @SQL_SCRIPT VARCHAR(MAX)  SET @SQL_SCRIPT = REPLACE(@CREATE_TEMPLATE, '{DBNAME}', @DBNAME) EXECUTE (@SQL_SCRIPT)  SET @SQL_SCRIPT = REPLACE(@COMPAT_TEMPLATE, '{DBNAME}', @DBNAME) EXECUTE (@SQL_SCRIPT)  SET @SQL_SCRIPT = REPLACE(@RECOVERY_TEMPLATE, '{DBNAME}', @DBNAME) EXECUTE (@SQL_SCRIPT) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MySQL uses the USE database_name to change the active database. Does that work on
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I got this: SET @databasename = (SELECT DATABASE()); USE @databasename; ...SQL Querys... But this
So I've got this audit table, looks like this: USE [DatabaseName] GO /****** Object:
I want to use Access database with query for pivot table in Excel. How
I'd like to use object database to persist some classes created in IronPython. The
In a database I use throughout several projects, there is a field that should
When is it best to use database as a session store in PHP? I
I use H2 Database as DBMS from a remote computer,so I enabled remote access
What components can I use to use SQlite database with Delphi 6?

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.