Can I get SSMS to switch to the Messages tab programatically? - sql

I'm writing a fairly long running script, which prints progress messages as it goes.
However, SQL Server Management Studio by default shows the resultset tab, not the messages tab, so the user will have to click the messages tab after starting the script to see the progress as it happens.
Is there a way for my script to tell SSMS to show the messages tab automatically?
I know SSMS will remove the resultset tab once the script is complete, since it doesn't generate any resultsets, but I want the messages tab shown immediately, not at the end.
I'm not looking for a setting within SSMS, since this script will not be run on my machine once released.

There is not anything that you can include in your script to change the default behavior of the SSMS IDE, you might be able to handle it with a setting, but you noted that isn't a valid solution.

you should forget about trying to do this in SSMS and use the sqlcmd utility
Tutorial: sqlcmd Utility
Using SQL Server 2005 sqlcmd Utility

Related

Is there a way to set SSMS only allows to run when code is selected

I am pretty new to SSMS and I am starting to make scripts. Sometimes I click on Run without selecting my code and you can imagine that it has consequences on the database.
I wanted to ask you if there is a possible configuration on SSMS so that the run button is not accessible when we have code selected?
Kind Regards
Setting SET NOEXEC ON before the query guaranties that the SQL Server engine will check the syntax and object reference errors (during compilation), but it will not execute the actual query.
If you want prevent distract F5 or EXEC without have checked if you're executing something dangerous, put it before each script that you are making.

Datagrip ORACLE --- I cannot run Stored Procedures [duplicate]

How Can I "set server output" on in Jetbrains DataGrip IDE? I am able to do this in SQL Developer and run the SQL script successfully. The same script I try to run in DataGrip and it gives error. I know its the "set server output on" that is not working because I had to configure SQL Developer for it to work. Any advice would be appreciated. Thanks.
According to the documentation:
For Oracle, you can enable or disable showing the contents of the DBMS_OUTPUT buffer in the output pane. To do that, use on the toolbar of the Database Console tool window (Ctrl+F8).
They also show an image of the window with the button enabled;
Update: 2019
The icon to enable the output looks like:
On MacOS it can be enabled with Command+F8.
Server output is client side functionality that not all clients support. SQL*Plus and SQL Developer do, but it's not a given that any other client will.
The SQL*Plus client command set serveroutput on essentially calls the DBMS_OUTPUT.enable() stored procedure on your behalf. Additionally it signals to the client that is should inspect the DBMS_OUTPUT.get_lines() procedure for cached output after each code block is executed.
If DataGrip doesn't do this already, you may be able to manually code the calls to DBMS_OUTPUT.enable() and DBMS_OUTPUT.get_lines() as needed.
For version 2021.1 right click on the console list in the service window and there should be a enable DBMS_OUTPUT when you right click.

How To Set Server Output On in DataGrip

How Can I "set server output" on in Jetbrains DataGrip IDE? I am able to do this in SQL Developer and run the SQL script successfully. The same script I try to run in DataGrip and it gives error. I know its the "set server output on" that is not working because I had to configure SQL Developer for it to work. Any advice would be appreciated. Thanks.
According to the documentation:
For Oracle, you can enable or disable showing the contents of the DBMS_OUTPUT buffer in the output pane. To do that, use on the toolbar of the Database Console tool window (Ctrl+F8).
They also show an image of the window with the button enabled;
Update: 2019
The icon to enable the output looks like:
On MacOS it can be enabled with Command+F8.
Server output is client side functionality that not all clients support. SQL*Plus and SQL Developer do, but it's not a given that any other client will.
The SQL*Plus client command set serveroutput on essentially calls the DBMS_OUTPUT.enable() stored procedure on your behalf. Additionally it signals to the client that is should inspect the DBMS_OUTPUT.get_lines() procedure for cached output after each code block is executed.
If DataGrip doesn't do this already, you may be able to manually code the calls to DBMS_OUTPUT.enable() and DBMS_OUTPUT.get_lines() as needed.
For version 2021.1 right click on the console list in the service window and there should be a enable DBMS_OUTPUT when you right click.

How to change the connection in Sql Server Data Tools Editor in Visual Studio

My goal is to keep SQL Server stored procedures under source control. I also want to stop using SQL Server Management Studio and use only Visual Studio for SQL related development.
I've added a new SQL Server Database project to my solution. I have successfully imported my database schema into the new project, and all the SQL objects (tables, stored procedures) are there in their own files.
I know that now if I run (with F5) the .sql files then my changes will be applied to my (LocalDB). This if fine, but what if I want to very quickly run something on another machine (like a dedicated SQL Server shared by the entire team)? How can I change the connection string of the current .sql file in the Sql Server Data Tools editor?
I have the latest version of Sql Server Data Tools extension for Visual Studio 2012 (SQL Server Data Tools 11.1.31203.1). I don't know if this is related to the current version, but I cannot find anymore the Transact-SQL Editor Toolbar.
I have also tried to Right-click on the sql editor, choose Connection -> Disconnect. If I do the reverse (Connection -> Connect...) the editor directly connects automatically (probably to my LocalDB), without asking me a dialog to choose my connection.
Another strange thing I've observed, if I try to run a simple SQL query (like select * from dbo.ApplicationUser I receive the following message (even if the autocomplete works):
Thanks.
(Note: I have the same issue with Visual Studio 2013)
Inspired by srutzky's comments, I installed the latest SSDT pack (12.0.41025). And bingo, like srutzky said there is a Change Connection option. But what's more, you can specify your Target DB by right clicking on the Project in the Solution Explorer, and going to Properties->Debug and changing the Target Connection String! If you're stuck on an older SSDT, then the below instructions will still work.
For SSDT 12.0.3-
I've also been plagued by this problem! My solution is below, but it has some Pros and Cons to it...
SOLUTION
I'm assuming that you are using a SQL Server Project in VS (I'm using VS2013 and SQL Server 2012).
Right click on your .sql file in the Solution Explorer and view Properties.
Change Build Action to None.
If the file is open for editing, then close it.
Reopen the file, and the T-SQL Editor should appear at the top.
Click Connect, and you will connect to your (localdb).
Click Disconnect.
Click Connect again and the SQL Server Connection dialog should appear.
Switch the connect string of '(localdb)\Whatever' to '.' (for some reason using '(localhost)' didn't work for me).
Voila, you should now be able to query against your SQL Server DBs! Rinse and repeat for every file you want this capability with... :/
PROS
You can finally run queries directly against your SQL Server DB
Your code can be organized in a nice VS solution (SSMS doesn't allow folders! :/)
You can (after switching Build Action setting back) Build the project
CONS
I'm not seeing any autocomplete/intellisense against the remote DB, although if you import your DB, then you could gain the intellisense from that
Requires each file to switch Build Action to None
This should be a fairly simple and straight-forward thing to do, that is, if you are using SSDT version 12.0.41025.0 (or newer, one would suppose):
Do either:
Go to the SQL menu at the top of the Visual Studio window
Right-click inside of the SQL editor tab
Go to Connection ->
Select Change Connection
Then it will display the "Connect to Server" modal dialog window.
If you do not see the options for "Disconnect All Queries" and "Change Connection...", then you need to upgrade your SSDT via either:
Visual Studio:
Go to the "TOOLS" menu and then "Extensions and Updates..."
Direct download:
Go to: http://msdn.microsoft.com/en-us/data/tools.aspx
The fastest way to achieve this is create a new SQL Connection, copy and paste the code then execute.
What I do is Tools->SQL Server->New Query.
Enter the database credentials (And make sure that the Database at the top is correct - I have hundreds of sp's in my master db on local :) )
Copy the source code from the editor, paste into the new query window.
Then Execute (CRTL-Shift-E).
You can leave this 'scratch' window open and pinned for easy access for subsequent executes.
If you want to deploy (i.e. publish) the entire database then you can setup a publish destination for each server, right click on the xml and select publish..

See queries that hit SQL

Is there a way using sql 2008 Management Studio to look at the queries that hit the server? I'm trying to debug a program and I get messages like "Incorrect syntax near the keyword 'AND'". Since the queries are being dynamically generated it's a hassle to figure out what is going to the server.
Any help is appreciated!
There is a tool called Profiler that will tell you all information that you'll need. MSDN: http://msdn.microsoft.com/en-us/library/ms187929.aspx
I'm not aware of any method to do this using SQL Server Management Studio, but if you installed SSMS then you probably also installed the SQL Profiler. If you fire that up and run the TSQL_SPs profiler template, you can see every statement that's hitting the database.
Since the queries are being dynamically generated it's a hassle to figure out what is going to the server.
Why not just put the query that's generated into a message box, or print it to the console, or webpage, etc. ??
Trying to catch it at the DB server seems to be the long-way-around to debugging some simple ad-hoc queries.
Go to Management...Activity Monitor in the object explorer.
It's not live though, you will have to refresh it manually.
start up profiler from SSMS (Tools-->SQL Server Profiler), run a trace and select the T-SQL events
One option is to use SQL Server Profiler to run a trace. However, in some shops SQL Server permissions are set so only DBAs can run traces.
If you don't have sufficient rights to run a trace, then another option is to view the network traffic between the application that generates the SQL and box SQL Server is running on. WireShark works great for that.