In SQL Server Management Studio what is SQLCMD mode? - sql-server

In SQL Server Management Studio I would like to know what is SQLCMD mode?

I did some more research, so here's my understanding of this to extend what has been written so far:
What is SQLCMD
SQLCMD.exe is a console utility included in the instalation of SQL Server 2005 and higher. You can typically find it in a path like c:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE.
It is a simple scripting environment that allows automation of tasks related to SQL server. For example, you can write and execute a script that will login to a specific instance of SQL Server, execute scripts from a given directory on this connection and store the output in a specified file.
Invoke-Sqlcmd cmdlet was introduced with SQL Server 2008 as a mean to replace this tool with a standardized, Powershell-based approach, preserving most of the original syntax and functionality.
What is SQLCMD mode in SSMS
In SSMS, SQLCMD mode is a script execution mode that simulates the sqlcmd.exe environment and therefore accepts some commands that are not part of T-SQL language.
Unlike sqlcmd.exe, it contacts the database using SqlClient (the same way as SSMS), not ODBC data provider, so in some aspects it might have different behaviour than sqlcmd.exe.
Executing scripts in SQLCMD mode allows to use commands typical to sqlcmd.exe environment. However, there's no IntelliSense or debugging support for SQLCMD mode, so maintaining scripts that mix clean T-SQL with SQLCMD-specific code can be a pain. Therefore, it should be used only when it's necessary.
Example use case
Let's suppose that a company has a naming convention for databases that include environment in the name, eg: MyDb_Prod, MyDb_Test, MyDb_Dev. This convention might be used to minimize chance of mistakes.
When a developer writes a T-SQL script, it will have to be executed in different environments in deployment/testing process, which would require many versions of the code:
SELECT *
FROM [MyDb_Dev].[dbo].[MyTable1] -- MyDb_Dev -> MyDb_Test -> MyDb_Prod
Instead, we can assume that database name will be provided as a SQLCMD variable in deployment process and have exactly the same file deployed to all environments:
-- :setvar databaseName "MyDb_Dev" -- uncomment for testing in SSMS
SELECT *
FROM [$(databaseName)].[dbo].[MyTable1]
(in this simple example database name could be omitted altogether, but if you have cross-database joins, using database name is necessary)

Exactly what it sounds like.
It is a mode that lets you author SQLCMD scripts.
From MSDN - Editing SQLCMD Scripts with Query Editor:
To use the Database Engine Query Editor to write or edit SQLCMD scripts, you must enable the SQLCMD scripting mode.
In SQL Server Management Studio, set this via the Query menu (Query -> SQLCMD Mode).

"You use SQLCMD scripts when you have to process Windows System commands and Transact-SQL statements in the same script."
"By default, SQLCMD mode is not enabled in the Query Editor. You can enable scripting mode by clicking the SQLCMD Mode icon in the toolbar or by selecting SQLCMD Mode from the Query menu."
Reference: MSDN

Message Transact-SQL IntelliSense is not active for this editor because the editor is in SQLCMD mode.
I was publishing SQL Server Project from Visual Studio to Database to synchronize changes and got an error above. Publishing was failing.
I closed all open files in VS and it resolved the issue.
I hope real life example explained why people use SQLCMD mode.

Related

SQL script for DB creation from T-SQL or command line

I searched SO for the solution but all answers means using interactive mode (SSMS) or third-party tools.
I need to obtain database creation script (with tables, SPs, triggers etc.) not from SSMS in interactive mode but from T-SQL or command line. There was SqlPubWiz utility in MSSQL older versions, but I cannot find it in 2014 (I use Express Edition).
So what should I do? Thank you.

Built-in scheduler functionality in SQL Server 2008

My MVC3 application is using an SQL Server 2008 to store data. In particular - support ticket management data.
I have a table in a database - Tickets.
I are reviewing a possibility of implementing a recurring ticket registration, using an SQL Server features.
Is there a built-in SQL Server functionality, that would allow me to schedule, a, for example, once a week creation of a row in a database table?
I would use the SQL Server Agent and run a Transact-SQL Job Step.
Note that the agent runs as a service under a particular account, which will need rights to be able to carry out whatever operations your need.
Unfortunately you haven't mentioned your SQL Server Edition. If you have Express then there is no built-in scheduler so you need to use the Windows scheduler to run a batch file or other program that connects to SQL Server.
If you have any other edition, then you have SQL Agent which is a full scheduler with support for just about any task including running SQL statements.
You can easily use the Windows SchedTask Control Panel to schedule a batch file to run periodically:
In the batch file create a SQL string like so:
SET SQLSTRING=INSERT INTO Persons^
VALUES (4,'Nilsen', 'Johan', 'Bakken 2',^
'Stavanger');
Then, just enable delayed expansion and use something like this:
sqlcmd.exe -b -S localhost -E -d !DBNAME! -Q "!SQLSTRING!" -W

How to import .sql file into SQL Server Express

I have a plain sql file with some SQL INSERT statements.
Is it possible to import it in my local SQL Server Express instance?
You can use Management Studio Express edition. You can download the latest version here - which will work against SQL Express 2005, 2008 and 2008 R2.
If you don't want to install SSMSE then you can use sqlcmd at a command prompt, e.g. something like this (assuming Windows auth and an instance called "SQLEXPRESS"):
sqlcmd -S .\SQLEXPRESS -E -i "C:\path\file.sql"
The easiest way would be simply open the file in the Sql Management Studio and run it. Since the target table is already created, of course.
You can open it via Query analyser and run
Here is the tool Sql_Server_Script_Executor
You can add single/multiple file/folder and your files will comes up in the list. Click the execute button and done
It contains three transaction modes.
1. Execute scripts within one transaction
2. Execute scripts on separate transaction
3. No transaction
All you have to do is open Microsoft SQL Server 2008 Management Studio.
Then use File -> Open.
Open the file from the proper location and you'll get all the SQL statements there. After that you can execute them.
Hope this helps.

executing queries from ado.net like I do from sql management studio

I am developing a small utility for my team which I want to use while deploying a database release. The developers are using management studio and whenever they make changes in an SP or table, they are creating scripts from management studio (DROP and CREATE) option.
In my utility, I use that script and when I run that script with ADO.Net execute non query, it throws several exceptions like - 'GO' is not correct keyword etc.
Can anyone please suggest what could be a way to run those scripts as is?
Thanks
Use of the Microsoft tools to run these scripts - they understand the GO command (which is not SQL) and strip it out before sending to SQL Server.
See sqlcmd and the older (deprecated) osql utilities.

Run sql script from console and not from query analyzer in SQL server

I have written an sql script for updating a database that runs in SQL server 2005.
I want to make those changes to the production DB server but I dont want to run the query from the query analyzer. Is there a way to run the sql script from a console?
create a batch file that points to the script or scripts that you created and run that batch file.
Here follow this tutorial link
You can use sqlcmd. It gets installed together with installing the Query Analyzer.
The sqlcmd utility lets you enter
Transact-SQL statements, system
procedures, and script files at the
command prompt, in Query Editor in
SQLCMD mode, in a Windows script file
or in an operating system (Cmd.exe)
job step of a SQL Server Agent job.
This utility uses OLE DB to execute
Transact-SQL batches.

Resources