1

I need to execute the following statement

ALTER DATABASE DatabaseName ...

in my .sql script. But I want to keep my script neutral to the specific database. So I want to make ALTER DATABASE work on the current database. I hoped DatabaseName is optional parameter, but according to documentation, it is not.

5 Answers 5

3

Database name is required and you cannot even put it in a parameter.

But you can use 'exec' instead

    declare @stmt varchar(max)
    set @stmt = 'alter database....'
    exec (@stmt)

It is not very elegant, but i believe it is the only way to do it

Sign up to request clarification or add additional context in comments.

5 Comments

Got a slight tyop in there. <sic> But the approach is perfectly sound.
You said I can't put database name in parameter... Why?
I don't know the motivation for the design, but 'alter database @name...' is simply not valid.
But there is another kind of parameters: msdn.microsoft.com/en-us/library/ms188714.aspx
Cool, that is of course useful. But strictly speaking I guess it wouldn't be a SQL parameter but a scripting variable. It's definately useful though!
1

db_name() returns the current database, which you can then stuff into an EXEC statement

DECLARE @dbname sysname = db_name()

EXEC('ALTER DATABASE ' + @dbname + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE') 

Comments

0

Well,

This is one of the reasons why so many ORMs appeared for almost all the languages possible. To be sure you offer as much DB independence as possible, I recommend you to use one of them.

2 Comments

Code-first schema generation aside, I don't believe issuing DDL statements is really a common use case for ORMs.
@cpedros - indeed it might not be, but those are the tools that might help the OP to reach DB structure creation independence in the fastest possible manner
0

Can't you just use a scripting language to wrap this sql call? It would be much easier and cleaner.

You can accomplish this through a series of convoluted SQL commands. If you're using MySQL, an example of creating your own SQL statement using variables and CONCAT is shown here:

http://blog.mclaughlinsoftware.com/2011/01/19/prepared-statement-failure/

As it says in the article, for ALTER statements, you have to CONCAT the database name into the statement variable. You can then use other SQL commands to get the database name you want, like DATABASE().

Comments

0

EDIT: I retract my answer, the answers using EXEC are the right idea in this case.

Haven't need this in a while, but I believe you can use SELECT db_name() as a subquery, like so:

ALTER DATABASE (SELECT db_name()) ...

To run the relevant command on the current database, if that works for your use case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.