Debugging stored procedures in SQL Server Management Studio - sql

Is there a way to step into the stored procedure code in SQL Server Management Studio? I know this is possible with Visual Studio, but I am looking for a dependable debugging solution from within Management Studio

Only with SQL 2008 and SSMS 2008. Select from menu 'Debug\Start Debugging' or press Alt+F5 and will launch the step by step T-SQL debugger.
On 2005 the only way is to attach Profiler and monitor for the SP:StmtCompleted event, which is not exactly debugger step-by-step, but at least you'll see the execution flow. Not to be done on a production machine, obviously.

I have written a pretty detailed blog post about it here:
http://www.diaryofaninja.com/blog/2010/11/23/debugging-sql-queries-function-amp-stored-procedures-with-sql-management-studio
Basically the gist of it is that you enter your sql query to execute your stored procedure, and instead of pressing F5 or hitting the exclamation, you hit the play button and use F10 and F11 to step through and step into your stored procs.
This is very handy but no one seems to use it.

From SSMS 17 onwards version, You can not debug any SQL.

The ability to debug sprocs, which was in Query Analyzer in SQL Server 2000 was not put into SQL Server 2005. Microsoft realized this mistake and put the functionality back in in SQL Server 2008.
In SSMS 2008 you can start the debugger by either clicking the debug button on the toolbar or pressing ALT+F5.
Unfortunately, running SSMS 2008 against a 2005 database will not allow you to debug, so
you'll have to stick with Visual Studio.

Watch out for debugging stored procedures using Visual Studio. The way that this is implemented is to use an incredible amount of locking so that you should never do this on a production system.

Yes, in SSMS 2008 you can definitely step into T-SQL code (code block, stored func, stored proc) and debug it.
See this excellent article Management Studio Improvements in SQL Server 2008 for details - it also deals with debugging (towards the end of the article).

Related

Is there a way to connect all query editors to the same connection in SQL Server Management Studio (SSMS v18.10)

When I am composing a script in SQL Server Management Studio v18.10, more often than not, I end up with plethora of recalcitrant Query Editor windows that each test one aspect of the problem (or my patience). Invariably I will need to test this ungovernable lot of scripts on different instances of SQL Server. I recently found out that there is a button to Disconnect All Queries...
...Is there a button to Reconnect All Queries?

What tool to capture values passed into SQL Server procedure

I have a DLL, for which I do not have the source code. It is making calls to a stored procedure in a SQL Server 2005 database. I need to know what values it is passing in as the parameters of the call.
Is there a free tool, or one that comes with SQL Server that is able to monitor and record the calls into a database?
Thanks.
You can use for this SQL Server Profiler which comes with SQL Server.
If you have an access to Microsoft SQL Server Management Studio, you find it by clicking Tools → SQL Server Profiler.
When it starts, click Connect. On Trace properties dialog, click Run. It then collects every call to the SQL Server and displays the summary at the top, and the query with its parameters at the bottom of the window.
You can also:
Pause or stop the profiler (icons in the toolbar),
Clear the current trace.
Do not forget to clear the trace before launching the action which will call the stored procedure and do not forget to pause/stop the profiling after you get the results to avoid getting too much information to analyze.
As pointed in the comments, if you are using SQL Server Express, the profiler may be missing. You may want to install a third party profiler for SQL Express instead.
Check the [Tools] menu in SQL Server Management Studio:
If you don't see this option, you most likely have SQL Server EXPRESS which does not come with Profiler.

Run SQL Server 2000 Job programmatically

I know you can execute a Job in SQL Server 2005/2008 by sending a call to the Stored Procedure msdb.dbo.sp_start_job.
However is it possible to do this programmatically for SQL Server 2000?
I am using VB.NET however I believe the language I'm using is irrelevant on how to go about it.
If someone knows how to do it in VB.NET (or C#, I can convert it), I'd appreciate it.
Edit: Appears it does exist in 2000. I couldn't locate it in Management Studio, MSDN for the 2005 "Other Versions" link only had 2005 and 2008, and google was only bringing up the 2005 version. Sorry for wasting people's time!
http://msdn.microsoft.com/en-us/library/aa260289(SQL.80).aspx
Suggests that sp_start_job is supported in SQL Server 2000
sp_start_job is available in 2000 as well.

Step into stored procedure - canceled by user

I am trying to debug a long stored procedure in Visual Studio 2008. I have followed all the steps on this link, but I still get the same error:
'Canceled by user' and it never hits the breakpoint.
Visual Studio 2008 and SQL Server 2005 are running on 2 different servers and domains although I can see the SQL Server from the PC running Visual Studio.
I think that you are already on the right track, as that article on sql team is exactly what I would have pointed you to. Can you verify that you've read and followed the instructions he points you at:
Setting Up SQL Debugging
Every single time that I've encountered that lame excuse for an error message, I've found it to be a configuration problem of one sort or another.
In SQL Server execute sp_addsrvrolemember '[Domain]\[ID]', 'sysadmin' to add yourself as a system administrator to the database.
a colleague just got the 'Canceled by user' when Stepping into a Stored Procedure. The solution in this case:
Shift + right click to open Visual Studio as Administrator

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.