SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Server Detach database


SQL Server Detach database

Author
Message
TDP
TDP
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 190
Hi

I am a SQL Server DBA and have never used Powershell before. I'm trying to detach a database using Powershell, but not getting very far.
Have read up a bit and most ppl do it like below. I tried to run this, but getting an error that I'm not finding a solution for, so I'm pretty sure I'm doing something very wrong or possibly not doing something that I need to add to the script. Below is what I'm running and the error I'm getting.
Please if anyone has any suggestions, please let me know.


PS SQLSERVER:\> [void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
PS SQLSERVER:\> $server = new-object Microsoft.SqlServer.Management.Smo.Server('SERVERNAME')
PS SQLSERVER:\> $server.DetachDatabase("databasename")
Cannot find an overload for "DetachDatabase" and the argument count: "1".
At line:1 char:1
+ $server.DetachDatabase("databasename")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Smile [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest


Thanks!
TDP
Tony Sweet
Tony Sweet
Valued Member
Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)

Group: General Forum Members
Points: 71 Visits: 66
Looks like you are missing a parameter in the DetachDatabase syntax.

See this: http://technet.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.detachdatabase.aspx

You need to add a Boolean for the update statistics parameter. Assuming you don't want to do this, it would look like

$server.DetachDatabase("databasename", FALSE)



- Tony Sweet
TDP
TDP
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 190
Thanks
Tried that, but got the below error:


PS SQLSERVER:\> $server.DetachDatabase("dbPSXTest", FALSE)
At line:1 char:36
+ $server.DetachDatabase("dbPSXTest", FALSE)
+ ~
Missing expression after ','.
At line:1 char:37
+ $server.DetachDatabase("dbPSXTest", FALSE)
+ ~~~~~
Unexpected token 'FALSE' in expression or statement.
At line:1 char:42
+ $server.DetachDatabase("dbPSXTest", FALSE)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (Smile [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingExpressionAfterToken

TDP
Gary Varga
Gary Varga
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16655 Visits: 6534
You need to use $false in place of FALSE.

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Tony Sweet
Tony Sweet
Valued Member
Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)

Group: General Forum Members
Points: 71 Visits: 66
Thanks Gary. I always forget Powershell is special Smile

- Tony Sweet
TDP
TDP
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 190
Thanks you so much! That did it.
Gary Varga
Gary Varga
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16655 Visits: 6534
You are most welcome. (Of course, Tony did the heavy lifting!!!)

Gaz

-- Stop your grinnin' and drop your linen...they're everywhere!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search