Badg3r1977 Posted October 19, 2011 Share Posted October 19, 2011 Database Name as Parameter / Variable Hi im using SQL Server Management Studio 2008 addition and im trying to specify a database name for a fix database script we run: ALTER DATABASE MyDatabase SET EMERGENCYDBCC checkdb('MyDatabase')ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATEDBCC CheckDB ('MyDatabase', REPAIR_ALLOW_DATA_LOSS)ALTER DATABASE MyDatabase SET MULTI_USER When I try to replace MyDatabase with a variable the code will not run! Does anybody have any ideas? FYI: Im just learning SQL; please treat me like an idiot! Thanks all Link to comment Share on other sites More sharing options...
Ingolme Posted October 19, 2011 Share Posted October 19, 2011 Does SQL have variables? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2011 Share Posted October 19, 2011 Yes, it does. Is that a user-defined function you're trying to run, a stored procedure, or neither? Link to comment Share on other sites More sharing options...
Badg3r1977 Posted October 19, 2011 Author Share Posted October 19, 2011 Its neither; run SQL Server Management Studio, connect to the database, select new query (a new query window open that points to the currently selected database). I would then just copy the above into the query window and hit execute. I currently have to specify the database name 5 times; I would like to only have to specify it once in a variable. I cant create stored procedures as its an enterprise database and I don’t own it! I just (in this case) need to repair suspect databases! Link to comment Share on other sites More sharing options...
justsomeguy Posted October 19, 2011 Share Posted October 19, 2011 Variables can't represent object names like databases and tables, only values in expressions. Your best bet may be an execute command, but I'm not sure if that will be able to do what you want. http://msdn.microsof...y/ms188332.aspx Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.