Tag Archives: SQLCMD

Examples of XP_SMDSHELL Commands

1) XP_CMDSHELL Directory Search Let’s start out with a simple directory search example. Say you do not have access to the physical SQL Server machine, but you want to see all the files in the “C:\temp” directory that have a … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

:OUT /STDERR/STDOUT SQLCMD Command

:OUT <filename>/STDERR/STDOUT This command redirects all query results output that by default gets sent to standard output.

Posted in Uncategorized | Tagged , | Leave a comment

:LISTVAR and :LIST SQLCMD Command

:LISTVAR and :LIST The LISTVAR command was mentioned earlier during the introduction of scripting variables. It lists currently defined variables and, as such, is handy in troubleshooting SQLCMD. There is a different command, LIST, which enables you to list contents … Continue reading

Posted in Uncategorized | Tagged , , , | Leave a comment

:r SQLCMD Command

:r <filename> This option enables the script writer or interactive user to read the contents of a specified script file and include it in the current execution session. The included file is processed in-place; in other words, every command encountered … Continue reading

Posted in Uncategorized | Tagged , | Leave a comment

Mix SQLCMD command with T-SQL statements in the same batch

It is safe to mix SQLCMD command with T-SQL statements in the same batch because they all get pre-processed before T-SQL is submitted to tor any given batch, all SQLCMD commands are pre-processed and only after that is T-SQL sent … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

Automate Backup Process With SQLCMD

Automate Backup Process — can create SQL Scheduled Task that will call and execute this SQL (everynight for example) declare @Dt_DateAndTime Varchar(100), @backupfile sysname, DECLARE @ThreeDaysAgo VARCHAR(50) select @Dt_DateAndTime = convert(char(8), getdate(), 112)+”+ Replace(convert(char(12), getdate(), 14),’:’,”) select @Dt_DateAndTime select @backupfile … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

SQLCMD Command Overview

As mentioned earlier, in addition to command line options, SQLCMD also supports many powerful commands. A SQLCMD command always starts with a new line and is preceded by a colon. A colon is required to make a clear distinction between … Continue reading

Posted in Uncategorized | Tagged , | Leave a comment