Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run sql script using powershell?

I wanted to run a sql script using powershell but getting an error "The term 'Invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t he spelling of the name, or if a path was included, verify that the path is correct and try again."

I have found the below snippet from some website.but this is only for one sql command..But i wanted to run a sql script.

Could someone please help in modifying the below for sql script or any better suggestion ?

   SQLServer = "ABCD\ABC" 
    $SQLDBName = "ABC_1223"
    $SqlQuery = "select * from table" 

    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $SQLServer; Database  =$SQLDBName;uid=$SQLDBName;pwd= $pwd; Integrated Security = True" 

    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $SqlQuery
    $SqlCmd.Connection = $SqlConnection 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd 
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet) 
    $SqlConnection.Close()
    clear 
    $DataSet.Tables[0]
like image 995
user2075017 Avatar asked Aug 18 '14 10:08

user2075017


People also ask

Can you run a SQL script in PowerShell?

Use SQL Server Agent to run SQL Server PowerShell scripts at scheduled times. There are two SQL Server PowerShell modules; SqlServer and SQLPS. The SqlServer module is the current PowerShell module to use.

How do I run a script in PowerShell?

In File Explorer (or Windows Explorer), right-click the script file name and then select "Run with PowerShell". The "Run with PowerShell" feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

How do I run a SQL script?

Open SQL Server Management Studio > File > Open > File > Choose your . sql file (the one that contains your script) > Press Open > the file will be opened within SQL Server Management Studio, Now all what you need to do is to press Execute button.


1 Answers

You can use your code as it is, just put your SQL script in a Here-String (string literal). In addition I would wrap the call to the SQL database in a Try/Catch/Finally construct to ensure your connections are closed and disposed. Finally, consider using Application Name in your connection string to give your DBA's a clue as to what your connection is related to. Putting that together:

Try{

  $SQLServer = "ABCD\ABC" 
  $SQLDBName = "ABC_1223"

#This is the here-string
  $SqlQuery = @"
    select * from table
    where we can select stuff
    and filter it
    and join
    etc
"@

  $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  $SqlConnection.ConnectionString = "Server = $SQLServer; Database  =$SQLDBName;Application Name = 'user2075017_db_call';uid=$SQLDBName;pwd= $pwd; Integrated Security = True" 

  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = $SqlQuery
  $SqlCmd.Connection = $SqlConnection 
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $SqlCmd 
  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet) 
  $DataSet.Tables[0]
}
Catch{
}
Finally{
  $SqlConnection.Close()
  $SqlConnection.Dispose()
}
like image 200
nimizen Avatar answered Oct 01 '22 08:10

nimizen