Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Not able to create table in database using powershell

$
0
0

HI

I have sql server 2012 express edition. Using power shell I try to create a database and then create a table. In the below script the database is created but fails to create the table. I get the following error

Exception calling "ExecuteNonQuery" with "2" argument(s): "ExecuteNonQuery failed for Database 'TestDatabase'. "
At C:\Users\kumar\Desktop\PowerShell Scripts\CreateTable.ps1:26 char:5
+ $Database.ExecuteNonQuery($TableScript,$ExecutionType)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FailedOperationException

Script ------------------------------------------

[String]$DatabaseServer = "Kumar-DT\SQLEXPRESS" 
[String]$DatabaseName = "TestDatabase"


try {add-type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" -EA Stop; $smoVersion = 10}
catch {add-type -AssemblyName "Microsoft.SqlServer.Smo"; $smoVersion = 9}

$SQLServer = New-Object Microsoft.SqlServer.Management.Smo.Server $DatabaseServer
$Database = Create_Database $SQLServer $DatabaseName $DatabaseServer

$TableScript = New-Object -Type System.Collections.Specialized.StringCollection
$TableScript.Add("CREATE TABLE [dbo].[testTable] ([date] [datetime] NULL,[time] [datetime] NULL ,[s-sitename] [varchar1] (255) NULL,[s-computername] [varchar] (255) NULL,[time-taken] [int] NULL)") | Out-Null
$ExecutionType = [Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
$Database.ExecuteNonQuery($TableScript,$ExecutionType)


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>