Skip to content

Examples

Sql Quantum Leap edited this page Dec 18, 2015 · 3 revisions

Here are some examples of running SimpleSqlExec that show some of the differences as compared to SQLCMD:


  1. No connection options uses Integrated Security and connects to local default instance, and the default database for the Login:

SimpleSqlExec.exe -Q "SELECT TOP (2) name,schema_id FROM master.sys.objects;"

Returns:

  name schema_id
  sysrscols 4
  sysrowsets 4
  1. No connection options uses Integrated Security and connects to local default instance, and the default database for the Login; default ApplicationName is "Simple SQL Exec":

SimpleSqlExec.exe -Q "SELECT TOP (2) name,schema_id FROM master.sys.objects; SELECT [program_name] FROM sys.dm_exec_sessions WHERE [session_id] = @@SPID;"

Returns:

  name schema_id
  sysrscols 4
  sysrowsets 4
  program_name
  Simple SQL Exec
  1. No connection options uses Integrated Security and connects to local default instance, and the default database for the Login; passing in the ApplicationName:

SimpleSqlExec.exe -Q "SELECT TOP (2) name,schema_id FROM master.sys.objects; SELECT [program_name] FROM sys.dm_exec_sessions WHERE [session_id] = @@SPID;" -an "TestRun"

Returns:

  name schema_id
  sysrscols 4
  sysrowsets 4
  program_name
  TestRun
  1. No connection options uses Integrated Security and connects to local default instance, and the default database for the Login; passing in the ApplicationName; saving "row(s) affected" to a file:

SimpleSqlExec.exe -Q "SELECT TOP (2) name,schema_id FROM master.sys.objects; SELECT [program_name] FROM sys.dm_exec_sessions WHERE [session_id] = @@SPID;" -an "TestRun" -ra rows.txt

Returns:

  name schema_id
  sysrscols 4
  sysrowsets 4
  program_name
  TestRun

Running type rows.txt returns:

  3
  1. No connection options uses Integrated Security and connects to local default instance, and the default database for the Login; passing in the ApplicationName; storing "row(s) affected" in a User-level environment variable:

SimpleSqlExec.exe -Q "SELECT TOP (2) name,schema_id FROM master.sys.objects; SELECT [program_name] FROM sys.dm_exec_sessions WHERE [session_id] = @@SPID;" -an "TestRun" -ra MyRows

(This does take a moment longer to run each time you create a new variable name since User-level environment variables are stored in the Registry. Updating an existing User-level environment variable is fairly quick.)

Returns:

  name schema_id
  sysrscols 4
  sysrowsets 4
  program_name
  TestRun

Running GetEnvironmentVariable.exe -name myrows returns:

  3

You can obtain GetEnvironmentVariable.exe (another free product from Sql Quantum Leap, and which can also retrieve the current value of Machine-level environment variables) from the following GitHub repository:

SqlQuantumLeap / GetEnvironmentVariable

Clone this wiki locally