Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Run Transact SQL using SMO.

Reply
 
Old 09-26-2007   #1 (permalink)
NJC


 
 

Run Transact SQL using SMO.

Hi,

I'm just wondering what is the best method of running a Transact SQL
statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
working.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
| out-null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null

$server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
$db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
"MyDatabase")

$sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
$script = sr.ReadToEnd

$db.ExecuteNonQuery($script)

I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
argument". I'm a bit confused because the in the documentation it states that
one sql argument can be passed with this function call. I'm not sure if it is
a syntax error or I’m just trying to do something that isn't possible.

Is this best approach? If so can someone please advise me on where I’m going
wrong?

Thanks in Advance,


My System SpecsSystem Spec
Old 09-27-2007   #2 (permalink)
RichS


 
 

RE: Run Transact SQL using SMO.

It doesn't seem able to pass in a script file in the way you want

ExecuteNonQuery() and ExecuteWithResults() are expecting TSQL statements

What is in your CreateTabletest.sql if you have multiple SAQL batches it
may be getting confused

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"NJC" wrote:
Quote:

> Hi,
>
> I'm just wondering what is the best method of running a Transact SQL
> statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> working.
>
> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> | out-null
> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
>
> $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> "MyDatabase")
>
> $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> $script = sr.ReadToEnd
>
> $db.ExecuteNonQuery($script)
>
> I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> argument". I'm a bit confused because the in the documentation it states that
> one sql argument can be passed with this function call. I'm not sure if it is
> a syntax error or I’m just trying to do something that isn't possible.
>
> Is this best approach? If so can someone please advise me on where I’m going
> wrong?
>
> Thanks in Advance,
>
My System SpecsSystem Spec
Old 09-27-2007   #3 (permalink)
NJC


 
 

RE: Run Transact SQL using SMO.

Hey Rich, thanks for you reply.

CreateTableTest.sql is just a basic script to create a new table with 3
columns:

/****** Object: Table [dbo].[Test] Script Date: 09/26/2007 11:09:47
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Test](
[TestID] [int] NOT NULL,
[Col1] [int] NOT NULL,
[Col2] [int] NOT NULL
) ON [PRIMARY]

Ultimately I’ll have a fairly large SQL script which is going to be used to
setup a database with the required entities, triggers etc…

Rich, what would you suggest to be the best method or practice for executing
a SQL script in SMO. I tried the ExecuteNonQuery approach because I found a
code snippet in VB.Net which appeared similar to the functionality I was
looking for:

Dim sr As StreamReader = New StreamReader("C:\script.sql")
Dim script As String = sr.ReadToEnd
Dim SMOServer As Server = New Server
Dim db As Database = SMOServer.Databases("northwind")
db.ExecuteNonQuery(script)

As always any advice would be greatly appreciated,


"RichS" wrote:
Quote:

> It doesn't seem able to pass in a script file in the way you want
>
> ExecuteNonQuery() and ExecuteWithResults() are expecting TSQL statements
>
> What is in your CreateTabletest.sql if you have multiple SAQL batches it
> may be getting confused
>
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "NJC" wrote:
>
Quote:

> > Hi,
> >
> > I'm just wondering what is the best method of running a Transact SQL
> > statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> > Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> > working.
> >
> > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> > | out-null
> > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
> >
> > $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> > $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> > "MyDatabase")
> >
> > $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> > $script = sr.ReadToEnd
> >
> > $db.ExecuteNonQuery($script)
> >
> > I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> > argument". I'm a bit confused because the in the documentation it states that
> > one sql argument can be passed with this function call. I'm not sure if it is
> > a syntax error or I’m just trying to do something that isn't possible.
> >
> > Is this best approach? If so can someone please advise me on where I’m going
> > wrong?
> >
> > Thanks in Advance,
> >
My System SpecsSystem Spec
Old 09-27-2007   #4 (permalink)
RichS


 
 

RE: Run Transact SQL using SMO.

It looks like you need to use a StringCollection to hold the commands and
pass in some execution type info.

I'll test it tonight and get back to you
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"NJC" wrote:
Quote:

> Hey Rich, thanks for you reply.
>
> CreateTableTest.sql is just a basic script to create a new table with 3
> columns:
>
> /****** Object: Table [dbo].[Test] Script Date: 09/26/2007 11:09:47
> ******/
> SET ANSI_NULLS ON
> GO
> SET QUOTED_IDENTIFIER ON
> GO
> CREATE TABLE [dbo].[Test](
> [TestID] [int] NOT NULL,
> [Col1] [int] NOT NULL,
> [Col2] [int] NOT NULL
> ) ON [PRIMARY]
>
> Ultimately I’ll have a fairly large SQL script which is going to be used to
> setup a database with the required entities, triggers etc…
>
> Rich, what would you suggest to be the best method or practice for executing
> a SQL script in SMO. I tried the ExecuteNonQuery approach because I found a
> code snippet in VB.Net which appeared similar to the functionality I was
> looking for:
>
> Dim sr As StreamReader = New StreamReader("C:\script.sql")
> Dim script As String = sr.ReadToEnd
> Dim SMOServer As Server = New Server
> Dim db As Database = SMOServer.Databases("northwind")
> db.ExecuteNonQuery(script)
>
> As always any advice would be greatly appreciated,
>
>
> "RichS" wrote:
>
Quote:

> > It doesn't seem able to pass in a script file in the way you want
> >
> > ExecuteNonQuery() and ExecuteWithResults() are expecting TSQL statements
> >
> > What is in your CreateTabletest.sql if you have multiple SAQL batches it
> > may be getting confused
> >
> > --
> > Richard Siddaway
> > Please note that all scripts are supplied "as is" and with no warranty
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "NJC" wrote:
> >
Quote:

> > > Hi,
> > >
> > > I'm just wondering what is the best method of running a Transact SQL
> > > statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> > > Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> > > working.
> > >
> > > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> > > | out-null
> > > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
> > >
> > > $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> > > $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> > > "MyDatabase")
> > >
> > > $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> > > $script = sr.ReadToEnd
> > >
> > > $db.ExecuteNonQuery($script)
> > >
> > > I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> > > argument". I'm a bit confused because the in the documentation it states that
> > > one sql argument can be passed with this function call. I'm not sure if it is
> > > a syntax error or I’m just trying to do something that isn't possible.
> > >
> > > Is this best approach? If so can someone please advise me on where I’m going
> > > wrong?
> > >
> > > Thanks in Advance,
> > >
My System SpecsSystem Spec
Old 09-27-2007   #5 (permalink)
NJC


 
 

RE: Run Transact SQL using SMO.

Great stuff! Cheers Rich

"RichS" wrote:
Quote:

> It looks like you need to use a StringCollection to hold the commands and
> pass in some execution type info.
>
> I'll test it tonight and get back to you
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "NJC" wrote:
>
Quote:

> > Hey Rich, thanks for you reply.
> >
> > CreateTableTest.sql is just a basic script to create a new table with 3
> > columns:
> >
> > /****** Object: Table [dbo].[Test] Script Date: 09/26/2007 11:09:47
> > ******/
> > SET ANSI_NULLS ON
> > GO
> > SET QUOTED_IDENTIFIER ON
> > GO
> > CREATE TABLE [dbo].[Test](
> > [TestID] [int] NOT NULL,
> > [Col1] [int] NOT NULL,
> > [Col2] [int] NOT NULL
> > ) ON [PRIMARY]
> >
> > Ultimately I’ll have a fairly large SQL script which is going to be used to
> > setup a database with the required entities, triggers etc…
> >
> > Rich, what would you suggest to be the best method or practice for executing
> > a SQL script in SMO. I tried the ExecuteNonQuery approach because I found a
> > code snippet in VB.Net which appeared similar to the functionality I was
> > looking for:
> >
> > Dim sr As StreamReader = New StreamReader("C:\script.sql")
> > Dim script As String = sr.ReadToEnd
> > Dim SMOServer As Server = New Server
> > Dim db As Database = SMOServer.Databases("northwind")
> > db.ExecuteNonQuery(script)
> >
> > As always any advice would be greatly appreciated,
> >
> >
> > "RichS" wrote:
> >
Quote:

> > > It doesn't seem able to pass in a script file in the way you want
> > >
> > > ExecuteNonQuery() and ExecuteWithResults() are expecting TSQL statements
> > >
> > > What is in your CreateTabletest.sql if you have multiple SAQL batches it
> > > may be getting confused
> > >
> > > --
> > > Richard Siddaway
> > > Please note that all scripts are supplied "as is" and with no warranty
> > > Blog: http://richardsiddaway.spaces.live.com/
> > > PowerShell User Group: http://www.get-psuguk.org.uk
> > >
> > >
> > > "NJC" wrote:
> > >
> > > > Hi,
> > > >
> > > > I'm just wondering what is the best method of running a Transact SQL
> > > > statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> > > > Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> > > > working.
> > > >
> > > > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> > > > | out-null
> > > > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
> > > >
> > > > $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> > > > $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> > > > "MyDatabase")
> > > >
> > > > $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> > > > $script = sr.ReadToEnd
> > > >
> > > > $db.ExecuteNonQuery($script)
> > > >
> > > > I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> > > > argument". I'm a bit confused because the in the documentation it states that
> > > > one sql argument can be passed with this function call. I'm not sure if it is
> > > > a syntax error or I’m just trying to do something that isn't possible.
> > > >
> > > > Is this best approach? If so can someone please advise me on where I’m going
> > > > wrong?
> > > >
> > > > Thanks in Advance,
> > > >
My System SpecsSystem Spec
Old 09-27-2007   #6 (permalink)
urkec


 
 

RE: Run Transact SQL using SMO.

"RichS" wrote:
Quote:

> It looks like you need to use a StringCollection to hold the commands and
> pass in some execution type info.
>

I think this line

$script = sr.ReadToEnd

should be like this:

$script = $sr.ReadToEnd ()


I am new around here so I hope I'm not talking nonsense.


--
urkec
My System SpecsSystem Spec
Old 09-27-2007   #7 (permalink)
RichS


 
 

RE: Run Transact SQL using SMO.

OK had a go at this

This will work

$script = New-Object -Type System.Collections.Specialized.StringCollection
$script.Add("SET ANSI_NULLS On")
$script.Add("SET QUOTED_IDENTIFIER ON")
$script.Add("CREATE TABLE [dbo].[Test1]([TestID] [int] NOT NULL, [Col1]
[int] NOT NULL, [Col2] [int] NOT NULL) ON [PRIMARY]")
$null =
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
$null =
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
$db = $server.Databases["Test"]
$extype =
[Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
$db.ExecuteNonQuery($script, $extype)

If you want it to fail on error change the Execution Type to Default

If you want to read the script from a file then do it this way

$sr = New-Object System.IO.StreamReader("C:\scripts\sqlscript.txt")
$script2 = $sr.ReadToEnd()
$null =
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
$null =
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
$null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
$db = $server.Databases["Test"]
$extype =
[Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
$db.ExecuteNonQuery($script2, $extype)

as urkec said it needs the () on the ReadToEnd

Strangely it didn't work with get-content. Need to have a think about that
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"NJC" wrote:
Quote:

> Hi,
>
> I'm just wondering what is the best method of running a Transact SQL
> statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> working.
>
> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> | out-null
> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
>
> $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> "MyDatabase")
>
> $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> $script = sr.ReadToEnd
>
> $db.ExecuteNonQuery($script)
>
> I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> argument". I'm a bit confused because the in the documentation it states that
> one sql argument can be passed with this function call. I'm not sure if it is
> a syntax error or I’m just trying to do something that isn't possible.
>
> Is this best approach? If so can someone please advise me on where I’m going
> wrong?
>
> Thanks in Advance,
>
My System SpecsSystem Spec
Old 09-28-2007   #8 (permalink)
NJC


 
 

RE: Run Transact SQL using SMO.

Ah fantastic... works like a dream. Big thanks Rich and Urkec for your
comments, as always very much appreciated.

Rich, I initially tired to use cmdlet get-content because I have used this
before for parsing an XML file but I couldn't get it working either.

Cheers again,


"RichS" wrote:
Quote:

> OK had a go at this
>
> This will work
>
> $script = New-Object -Type System.Collections.Specialized.StringCollection
> $script.Add("SET ANSI_NULLS On")
> $script.Add("SET QUOTED_IDENTIFIER ON")
> $script.Add("CREATE TABLE [dbo].[Test1]([TestID] [int] NOT NULL, [Col1]
> [int] NOT NULL, [Col2] [int] NOT NULL) ON [PRIMARY]")
> $null =
> [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
> $null =
> [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
> $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
> $Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
> $db = $server.Databases["Test"]
> $extype =
> [Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
> $db.ExecuteNonQuery($script, $extype)
>
> If you want it to fail on error change the Execution Type to Default
>
> If you want to read the script from a file then do it this way
>
> $sr = New-Object System.IO.StreamReader("C:\scripts\sqlscript.txt")
> $script2 = $sr.ReadToEnd()
> $null =
> [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
> $null =
> [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
> $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
> $Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
> $db = $server.Databases["Test"]
> $extype =
> [Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
> $db.ExecuteNonQuery($script2, $extype)
>
> as urkec said it needs the () on the ReadToEnd
>
> Strangely it didn't work with get-content. Need to have a think about that
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "NJC" wrote:
>
Quote:

> > Hi,
> >
> > I'm just wondering what is the best method of running a Transact SQL
> > statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> > Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> > working.
> >
> > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> > | out-null
> > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
> >
> > $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> > $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> > "MyDatabase")
> >
> > $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> > $script = sr.ReadToEnd
> >
> > $db.ExecuteNonQuery($script)
> >
> > I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> > argument". I'm a bit confused because the in the documentation it states that
> > one sql argument can be passed with this function call. I'm not sure if it is
> > a syntax error or I’m just trying to do something that isn't possible.
> >
> > Is this best approach? If so can someone please advise me on where I’m going
> > wrong?
> >
> > Thanks in Advance,
> >
My System SpecsSystem Spec
Old 09-28-2007   #9 (permalink)
RichS


 
 

RE: Run Transact SQL using SMO.

Its something to do with the way get-content reads the file contents. Always
soemthing new to dig into ;-)
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"NJC" wrote:
Quote:

> Ah fantastic... works like a dream. Big thanks Rich and Urkec for your
> comments, as always very much appreciated.
>
> Rich, I initially tired to use cmdlet get-content because I have used this
> before for parsing an XML file but I couldn't get it working either.
>
> Cheers again,
>
>
> "RichS" wrote:
>
Quote:

> > OK had a go at this
> >
> > This will work
> >
> > $script = New-Object -Type System.Collections.Specialized.StringCollection
> > $script.Add("SET ANSI_NULLS On")
> > $script.Add("SET QUOTED_IDENTIFIER ON")
> > $script.Add("CREATE TABLE [dbo].[Test1]([TestID] [int] NOT NULL, [Col1]
> > [int] NOT NULL, [Col2] [int] NOT NULL) ON [PRIMARY]")
> > $null =
> > [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
> > $null =
> > [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
> > $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
> > $Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
> > $db = $server.Databases["Test"]
> > $extype =
> > [Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
> > $db.ExecuteNonQuery($script, $extype)
> >
> > If you want it to fail on error change the Execution Type to Default
> >
> > If you want to read the script from a file then do it this way
> >
> > $sr = New-Object System.IO.StreamReader("C:\scripts\sqlscript.txt")
> > $script2 = $sr.ReadToEnd()
> > $null =
> > [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
> > $null =
> > [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
> > $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
> > $Server = new-object Microsoft.SqlServer.Management.Smo.Server("RSSQL1")
> > $db = $server.Databases["Test"]
> > $extype =
> > [Microsoft.SqlServer.Management.Common.ExecutionTypes]::ContinueOnError
> > $db.ExecuteNonQuery($script2, $extype)
> >
> > as urkec said it needs the () on the ReadToEnd
> >
> > Strangely it didn't work with get-content. Need to have a think about that
> > --
> > Richard Siddaway
> > Please note that all scripts are supplied "as is" and with no warranty
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "NJC" wrote:
> >
Quote:

> > > Hi,
> > >
> > > I'm just wondering what is the best method of running a Transact SQL
> > > statement in SMO? I've tried using ExecuteNonQuery with belongs to Namespace
> > > Microsoft.SqlServer.Management.Smo.Database but I can't seem to get it
> > > working.
> > >
> > > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO")
> > > | out-null
> > > [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.COMMON") | out-null
> > >
> > > $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') 'Home'
> > > $db = new-object ('Microsoft.sqlServer.Management.Smo.Database') ($server,
> > > "MyDatabase")
> > >
> > > $sr = new-object System.IO.StreamReader("C:\CreateTableTest.sql")
> > > $script = sr.ReadToEnd
> > >
> > > $db.ExecuteNonQuery($script)
> > >
> > > I keep on getting an error saying "Exception calling ExecuteNonQuery with 1
> > > argument". I'm a bit confused because the in the documentation it states that
> > > one sql argument can be passed with this function call. I'm not sure if it is
> > > a syntax error or I’m just trying to do something that isn't possible.
> > >
> > > Is this best approach? If so can someone please advise me on where I’m going
> > > wrong?
> > >
> > > Thanks in Advance,
> > >
My System SpecsSystem Spec
Old 09-28-2007   #10 (permalink)
urkec


 
 

RE: Run Transact SQL using SMO.

"RichS" wrote:
Quote:

> Its something to do with the way get-content reads the file contents. Always
> soemthing new to dig into ;-)

Sorry to barge in again, but I think I figured it out. When you use
Get-Content it returns an array, but $db.ExecuteNonQuery() with one argument
expects a string. Maybe it is possible to use "foreach" to concatenate all
strings in the array, inserting "`n" in between, and pass the resulting
string to ExecuteNonQuery(string) as the argument. (I'm afraid my knowledge
of PowerShell is insufficient to demonstrate this).

But even if it works it is probably easier to use ExecuteNonQuery overload
that accepts StringCollection as an argument, as you showed. Perhaps it would
be convenient to create the script from an existing (test) database using
Smo.Transfer.ScriptTransfer(), which returns a StringCollection, and to pass
it to Smo.Database.ExecuteNonQuery(StringCollection, ExecutionTypes)

--
urkec
My System SpecsSystem Spec
Reply

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46