lundi 1 décembre 2014

Dropping a table with powershell


Trying to delete an existing table using powershell



$srv = new-Object Microsoft.SqlServer.Management.Smo.Server(".\CEF_2014_1")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("tempdb")

#drop the Table
$tb = new-object Microsoft.SqlServer.Management.Smo.Table($db, "listeningport")
$tb.Drop()


but getting an error



PS C:\Users\craig.efrein\Dropbox\Scripts\Powershell> .\drop_table.ps1
Exception calling "Drop" with "0" argument(s):
"Drop failed for Table 'dbo.listeningport'. "
At C:\Users\craig.efrein\Dropbox\Scripts\Powershell\create_table.ps1:11 char:11
+ $tb.Drop <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException


If I create the table first, and then drop it, that works.


Am I filling the $tb object correctly?



$tb = new-object Microsoft.SqlServer.Management.Smo.Table($db, "listeningport")


As a second question, how do I check for the existence of the table before deleting it in powershell?





Aucun commentaire:

Enregistrer un commentaire