Last active
July 9, 2016 17:52
-
-
Save rellips/6de08e75340404b2c7a0 to your computer and use it in GitHub Desktop.
Simple command line SQL interpreter for Microsoft SQL Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# runSQL.ps1 | |
# simple command line SQL interpreter for Microsoft SQL Server | |
# More info: http://www.jeffspiller.net/2015/12/command-line-sql-server-interpreter-sql.html | |
# Copyright 2015 Jeff Spiller. All rights reserved. | |
# Licensed under the MIT License -- License Details at https://opensource.org/licenses/MIT | |
############################# | |
### config #### | |
############################# | |
$sqlServer="mypc\sqlexpress" | |
$database="AdventureWorks2014" | |
$user="script_user" #only required for sql server authentication (line 101) | |
$pass="script_pass" #only required for sql server authentication (line 101) | |
############################# | |
### Functions #### | |
############################# | |
function CloseProgram { | |
$sqlConnection.Close() | |
exit | |
} | |
function handleError($location,$errObj) { | |
write-host 'ERROR: '$errObj' AT: '$location | |
$error.Clear() #clear error object so I can always pop the current error off of the top | |
} | |
function runQuery ($sqlCmd) { | |
$sqlCommand.CommandText=$sqlCmd | |
$sqlReader = $sqlCommand.ExecuteReader() | |
if ( $error) { handleError "SQL STMT: " $error[0] } | |
$datatable = New-Object System.Data.DataTable | |
$dataTable.Load($sqlReader) | |
$Datatable | format-table -AutoSize | |
#$Datatable | format-list //vertical | |
} | |
function runDef ($intbl) { | |
$tbl = $intbl -replace "def","" | |
$sqlCommand.CommandText="select top 1 * from $tbl" | |
$sqlReader = $sqlCommand.ExecuteReader() | |
if ( $error) { handleError "SQL STMT: " $error[0] } | |
$datatable = New-Object System.Data.DataTable | |
$dataTable.Load($sqlReader) | |
#$Datatable | format-table -AutoSize | |
$Datatable | Format-List -Force | |
} | |
function procInput ( $inval ) { | |
if ($inval -eq "quit" ) { | |
closeProgram | |
} | |
if ($inval.StartsWith("SELECT","CurrentCultureIgnoreCase") ){ | |
runQuery $inval | |
} | |
if ($inval.StartsWith("DEF","CurrentCultureIgnoreCase") ){ | |
runDef $inval | |
} | |
if ($inval -eq "help" ) { | |
Write-host "quit,select..." | |
} | |
} | |
############################# | |
### MAIN #### | |
############################# | |
$error.clear() | |
###SQL Connection | |
# Un-comment the line 101 if you want to use sqlserver authentication | |
$sqlConnection = new-object System.Data.SqlClient.SqlConnection "server=$sqlServer;database=$database;Integrated Security=SSPI" #TRUSTED AUTHENTICATION | |
#$sqlConnection = new-object System.Data.SqlClient.SqlConnection "server=$sqlServer;database=$database;User Id=$user;Password=$pass" #SQL SERVER AUTHENTICTION | |
$sqlConnection.Open() | |
$sqlCommand = $sqlConnection.CreateCommand() | |
if ( $error) { handleError "DBConnect" $error[0]; exit } | |
### main Looop | |
write-host "SQL Command Line V .02" | |
write-host "Type quit to exit" | |
While (1 -eq 1 ) { | |
write-host -NoNewline --> | |
$input=read-host | |
procInput($input) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment