Monthly Archives: August 2009

:SETVAR [""] SQLCMD Command

:SETVAR <variable name> ["<variable value>"] The SETVAR option, already mentioned earlier during the discussion on scripting variables, enables you to set their values programmatically inside the script. One interesting aspect of the SETVAR command is that it is possible to … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

[:]EXIT[(statement)] SQLCMD Command

[:]EXIT[(statement)] This function has three distinctly different formats. *      :EXIT supplied without parenthesis has the same affect as QUIT. *      :EXIT() first executes previously accumulated statements and only then exits the session. *      :EXIT(query) first executes previously accumulated statements, then … Continue reading

Posted in Uncategorized | Tagged , | Leave a comment

[:]QUIT SQLCMD Command

[:]QUIT This command immediately ends the current SQLCMD session without running any statements in the statement cache.

Posted in Uncategorized | Tagged , | Leave a comment

[:]!! SQLCMD Command

[:]!! <command> Typing !! allows the user to pass essentially any operating system command straight to the command line interpreter for direct execution and passes the output back to your display. Warning This is a very powerful option that can … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

[:]ED SQLCMD Command

[:]ED This command enables the user to call an editor on the current query buffer. The editor of choice is defined by the SQLCMDEDITOR environment variable. The default editor is “edit,” provided by the operating system. After the editor exits, … Continue reading

Posted in Uncategorized | Tagged | Leave a comment

[:]RESET SQLCMD Command

[:]RESET All T-SQL statements are held in the statement cache until a batch terminator is encountered. The RESET command clears the statement cache. The statement cache can also be cleared by typing the ^C escape sequence at the console. However, … 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

Get a list of all of the servers on the network using SQLCMD vs CMD commands

SQLCMD Console 1. Start RUN command 2. Type in SQLCMD — it will start prompt in SQLCMD mode 3. Paste the following: :serverlist CMD Console 1. Start RUN command 2. Type in CMD — it will start prompt in CMD … Continue reading

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