SlideShare a Scribd company logo
1 of 15
SQLCMD Mode   give it one more chance
Agenda Some irritating DBA tasks Go beyond plain SQL  		  with SQLCMD Mode Best practices
Switching between Explorer / command line  Small but irritating What is server IP ? Is folder exists ? Is file there ? Ping server Create folder Delete files Rename or copy files
Script executions on different servers Create staging table on Staging Server Create fact table on Datawarehouse Change connection Add indexes to some other server
Adding database code to source control Take procedures one by one Save to source control What if there are more than 100 procedures?
Moving procedure code to production Open file from SourceSafe Execute in Management Studio What if there are more than 100 procedures?
Backup & Restore Backup DB Create folder on destination server Copy backup file to destination server Restore DB
SQLCMD mode: T-SQL Statements Windows System Commands + in the same script  !
SQLCMD Scripting Regular T-SQL  CMD script SQLCMD script
Examples…
Writing and Editing SQLCMD Scripts Rules SQLCMD commands must be the first statement on a line. Only one SQLCMD command is permitted on each line. SQLCMD commands can be preceded by comments or white space.
Lessons learned Some stuff to remember Enabling SQLCMD mode turns off IntelliSense and the Transact-SQL debugger The Query Editor cannot respond to operating system prompts When using :r command, keep one empty line between each execution ( concatenation ) SQLCMD commands always execute before any TSQL commands in the batch.
Lessons learned Some stuff to remember :connect  it's not treated as an implicit batch separator. If you forget to put in GO all over the place, ALL the commands are executed against the last server.  Line number  in error output does not count the lines where SQLCMD commands and variables used SSMS uses Microsoft .NET Framework SqlClient for execution in regular and SQLCMD mode  SQLCMD command line uses the OLE DB provider. Because different default options may apply, it is possible to get different behavior while executing the same query in SQL Server Management Studio SQLCMD Mode, and in the SQLCMD utility.
Bottom line Execute CMD and T-SQL in one tool. Easy script executions Easy connection switches Keep it in the source control together with all other workflows
Email me : maria@conduit.com

More Related Content

Similar to ISUG SQL CMD Maria

Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
webhostingguy
 
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
webhostingguy
 
Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)
Antony T Curtis
 
Automating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellAutomating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShell
Concentrated Technology
 

Similar to ISUG SQL CMD Maria (20)

Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
 
Watch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML UtilitiesWatch Re-runs on your SQL Server with RML Utilities
Watch Re-runs on your SQL Server with RML Utilities
 
Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)Perl Stored Procedures for MySQL (2009)
Perl Stored Procedures for MySQL (2009)
 
Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 
Automating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShellAutomating Active Directory mgmt in PowerShell
Automating Active Directory mgmt in PowerShell
 
No-script PowerShell v2
No-script PowerShell v2No-script PowerShell v2
No-script PowerShell v2
 
SilverStripe Framework - Building without the CMS
SilverStripe Framework - Building without the CMSSilverStripe Framework - Building without the CMS
SilverStripe Framework - Building without the CMS
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Procedures
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Procedures
 
The best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresherThe best Teradata RDBMS introduction a quick refresher
The best Teradata RDBMS introduction a quick refresher
 
SQL Server Admin Best Practices with DMV's
SQL Server Admin Best Practices with DMV'sSQL Server Admin Best Practices with DMV's
SQL Server Admin Best Practices with DMV's
 
Under the Hood 11g Identity Management
Under the Hood  11g Identity ManagementUnder the Hood  11g Identity Management
Under the Hood 11g Identity Management
 
arch_mtg_sqlsig_hcotter_replication.ppt
arch_mtg_sqlsig_hcotter_replication.pptarch_mtg_sqlsig_hcotter_replication.ppt
arch_mtg_sqlsig_hcotter_replication.ppt
 
11g Identity Management - InSync10
11g Identity Management - InSync1011g Identity Management - InSync10
11g Identity Management - InSync10
 
SQL Cockpit 3.1 - Overview
SQL Cockpit 3.1 - OverviewSQL Cockpit 3.1 - Overview
SQL Cockpit 3.1 - Overview
 
SQL Server: Now It's Everywhere You Want to Be
SQL Server: Now It's Everywhere You Want to BeSQL Server: Now It's Everywhere You Want to Be
SQL Server: Now It's Everywhere You Want to Be
 
Handling Database Deployments
Handling Database DeploymentsHandling Database Deployments
Handling Database Deployments
 
Ordina SOFTC Presentation - SQL CLR
Ordina SOFTC Presentation - SQL CLROrdina SOFTC Presentation - SQL CLR
Ordina SOFTC Presentation - SQL CLR
 

More from sqlserver.co.il

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013
sqlserver.co.il
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cache
sqlserver.co.il
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013
sqlserver.co.il
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3
sqlserver.co.il
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2
sqlserver.co.il
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1
sqlserver.co.il
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
sqlserver.co.il
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStore
sqlserver.co.il
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DAC
sqlserver.co.il
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatial
sqlserver.co.il
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
sqlserver.co.il
 

More from sqlserver.co.il (20)

Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013Windows azure sql_database_security_isug012013
Windows azure sql_database_security_isug012013
 
Things you can find in the plan cache
Things you can find in the plan cacheThings you can find in the plan cache
Things you can find in the plan cache
 
Sql server user group news january 2013
Sql server user group news   january 2013Sql server user group news   january 2013
Sql server user group news january 2013
 
DAC 2012
DAC 2012DAC 2012
DAC 2012
 
Query handlingbytheserver
Query handlingbytheserverQuery handlingbytheserver
Query handlingbytheserver
 
Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012Adi Sapir ISUG 123 11/10/2012
Adi Sapir ISUG 123 11/10/2012
 
Products.intro.forum version
Products.intro.forum versionProducts.intro.forum version
Products.intro.forum version
 
SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3SQL Explore 2012: P&T Part 3
SQL Explore 2012: P&T Part 3
 
SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2SQL Explore 2012: P&T Part 2
SQL Explore 2012: P&T Part 2
 
SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1SQL Explore 2012: P&T Part 1
SQL Explore 2012: P&T Part 1
 
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended EventsSQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
SQL Explore 2012 - Tzahi Hakikat and Keren Bartal: Extended Events
 
SQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStoreSQL Explore 2012 - Michael Zilberstein: ColumnStore
SQL Explore 2012 - Michael Zilberstein: ColumnStore
 
SQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DACSQL Explore 2012 - Meir Dudai: DAC
SQL Explore 2012 - Meir Dudai: DAC
 
SQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: SpatialSQL Explore 2012 - Aviad Deri: Spatial
SQL Explore 2012 - Aviad Deri: Spatial
 
מיכאל
מיכאלמיכאל
מיכאל
 
נועם
נועםנועם
נועם
 
עדי
עדיעדי
עדי
 
מיכאל
מיכאלמיכאל
מיכאל
 
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf FraenkelBi303 data warehousing with fast track and pdw - Assaf Fraenkel
Bi303 data warehousing with fast track and pdw - Assaf Fraenkel
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 

ISUG SQL CMD Maria

  • 1. SQLCMD Mode   give it one more chance
  • 2. Agenda Some irritating DBA tasks Go beyond plain SQL with SQLCMD Mode Best practices
  • 3. Switching between Explorer / command line Small but irritating What is server IP ? Is folder exists ? Is file there ? Ping server Create folder Delete files Rename or copy files
  • 4. Script executions on different servers Create staging table on Staging Server Create fact table on Datawarehouse Change connection Add indexes to some other server
  • 5. Adding database code to source control Take procedures one by one Save to source control What if there are more than 100 procedures?
  • 6. Moving procedure code to production Open file from SourceSafe Execute in Management Studio What if there are more than 100 procedures?
  • 7. Backup & Restore Backup DB Create folder on destination server Copy backup file to destination server Restore DB
  • 8. SQLCMD mode: T-SQL Statements Windows System Commands + in the same script !
  • 9. SQLCMD Scripting Regular T-SQL CMD script SQLCMD script
  • 11. Writing and Editing SQLCMD Scripts Rules SQLCMD commands must be the first statement on a line. Only one SQLCMD command is permitted on each line. SQLCMD commands can be preceded by comments or white space.
  • 12. Lessons learned Some stuff to remember Enabling SQLCMD mode turns off IntelliSense and the Transact-SQL debugger The Query Editor cannot respond to operating system prompts When using :r command, keep one empty line between each execution ( concatenation ) SQLCMD commands always execute before any TSQL commands in the batch.
  • 13. Lessons learned Some stuff to remember :connect it's not treated as an implicit batch separator. If you forget to put in GO all over the place, ALL the commands are executed against the last server.  Line number in error output does not count the lines where SQLCMD commands and variables used SSMS uses Microsoft .NET Framework SqlClient for execution in regular and SQLCMD mode SQLCMD command line uses the OLE DB provider. Because different default options may apply, it is possible to get different behavior while executing the same query in SQL Server Management Studio SQLCMD Mode, and in the SQLCMD utility.
  • 14. Bottom line Execute CMD and T-SQL in one tool. Easy script executions Easy connection switches Keep it in the source control together with all other workflows
  • 15. Email me : maria@conduit.com

Editor's Notes

  1. Different deployment scriptsEven not deployment, data compare as well