Smo.Server.ExecuteNonQuery throw an error executing a script with IF statement

I am trying to executed this script (see below) using Smo libraries with powershell (this script works in SSMS)

use master;
GO
use master;
if (not exists(select * from sys.databases where name = 'dbname'))
    alter database [dbname] set offline with rollback immediate;
GO

however, I always got an

Exception calling "ExecuteNonQuery" with "1" argument(s): "An exception occurred while executing a Transact-SQL statement or batch."

if I delete the if statement, the script execute without erros... is there any problem to use if statements?  I would really appreciate any help

regards.!


  • Edited by insdevelopment Wednesday, April 22, 2015 1:47 AM forget additional info
April 22nd, 2015 1:45am

How do you run the script using SMO with powershell?

I tried with sqlcmd and runs fine.

invoke-sqlcmd -inputfile "c:\mysqlfile.sql" -serverinstance "servername"

Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 9:47pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics