Is there any way to debug a stored procedure on SQL Server 2008?
I have access to use SQL Server Management Studio 2008 and Visual Studio 2008 (not sure whether either provides this functionality).
Generally I use the SQL profiler to find the parameters passed to the stored proc, however would like to be able to step through the procedure to see where it is failing.
Is this possible?
What is the best way? (in terms of quickly finding location of bugs)
Well the answer was sitting right in front of me the whole time.
In SQL Server Management Studio 2008 there is a Debug button in the toolbar. Set a break point in a query window to step through.
I dismissed this functionality at the beginning because I didn't think of stepping INTO the stored procedure, which you can do with ease.
SSMS basically does what FinnNK mentioned with the MSDN walkthrough but automatically.
So easy! Thanks for your help FinnNK.
Edit:
I should add a step in there to find the stored procedure call with parameters I used SQL Profiler on my database.
Yes you can (provided you have at least the professional version of visual studio), although it requires a little setting up once you've done this it's not much different from debugging code. MSDN has a basic walkthrough.
Yes, although it can be tricky to get debugging working, especially if trying to debug SQL on a remote SQL server from your own development machine.
In the first instance I'd recommend getting this working by debugging directly on the server first, if possible.
Log to the SQL server using an account that has sysadmin rights, or ask your DBA to to do this.
Then, for your own Windows account, create a 'login' in SQL Server, if it isn't already there:
Right-click the account > properties - ensure that the login is a member of the 'sysadmin' role:
(also ensure that the account is 'owner' of any databases that you want to debug scripts (e.g. stored procs) for:
Then, login directly onto the SQL server using your Windows account.
Login to SQL server using Windows Authentication (using the account you've just used to log into the server)
Now 'Debug' the query in SQL management studio, setting breakpoints as necessary. You can step into stored procs using F11:
Here's a useful guide to debugging:
http://blogs.msdn.com/b/billramo/archive/2009/04/11/transact-sql-debugger-for-sql-server-2008-part-1.aspx
If you need to remotely debug, then once you've got this part working, you can try setting up remote debugging:
http://blogs.msdn.com/b/billramo/archive/2009/04/11/transact-sql-debugger-for-sql-server-2008-part-2.aspx
One requirement for remote debugging is that the windows account used to run SSMS be part of the sysadmin role. See this MSDN link: http://msdn.microsoft.com/en-us/library/cc646024%28v=sql.105%29.aspx
MSDN has provided easy way to debug the stored procedure. Please check this link-
How to: Debug Stored Procedures
Related
https://msdn.microsoft.com/en-us/library/hh272701(v=vs.103).aspx
From the above link, it seems it is possible to debug a stored procedure in SSMS.
I am accessing a SQL Server instance using SQL Server authentication with admin privileges. I want to debug a stored procedure in SSMS, but I don't get that option.
Somebody told me that it works only with Windows authentication (while connecting to the database). I don't know to what extent this is true.
What are the prerequisites for allowing to debug a stored procedure? Because in deployment, stored procedure might have 50k lines and it would be hard to debug by print selective statement to get the clue and all (workaround).
Following are true in my case:
SSMS is professional edition.
SQL Server user for accessing is admin user
(IMHO, there needs to be a way to debug a stored procedure with SQL Server authentication mode too).
So, I would really appreciate, If someone can take me through that process
Yes, you can debug... I'm doing it right now with SQL Server 2016 and SSMS 17.6.
I'm using Windows Authentication.
Check the following printscreen.
In my case problem was firewall. In mostly companies Window's firewall is disabled and some other tools such as symtanec or third party application firewall are used.
It is tough for IT security to turn on or set those extra filter for SQL Server.
So, I had to screw my head everyday with same crap
This is not available in SSMS. You need SQL Server Data Tools (SSDT) to use the debug in stored procedure. You can download it here.
You can connect as Windows Authentication or SQL Authentication. Just make sure you have proper read/write access to view and execute the stored proc in Development Environment. Avoid using this tool in Prod.
Regarding the following images, I have obviously installed the Microsoft SQL Server, but I can't find the exe-file or anything named Microsoft SQL Server in the start menu.
Anyone who knows what to do to solve this, or do I need to install something more?
SQL Server runs as a service. You can start and stop it from windows services. SSMS is a gui tool for managing ddl (creating and altering tables and index's for example) and dml (writing queries to inquire, update or add data to databases).SSMS comes free with every edition of SQL server - perhaps you didn't tick a box when installing? You can also do this stuff the old fashioned way using sqlcmd from command line(DBAs love this for some reason) but for us mortals SSMS is much less of a struggle. As an aside there are other guis such as Toad (expensive) and Heidisql (free) amongst others which will do the job.
Looks like you have the engine. What you've got to do now is install SQL Server Management Studio: https://msdn.microsoft.com/hr-hr/library/mt238290.aspx
Looks like you have the various SQL Server components that are installed by Visual Studio (recent version) or SQL Server Data Tools (as used by VS).
I'm installing a program that often throws an exception due to either a wrong parameter in an INSERT statement or permission problems on the database. I do have full administrative rights on the SQL Server and it would be nice to log failed INSERTs etc. and the reason why they failed. The stack trace from the C# code shows only that a parameter is too long, but not which one of them and what the value was. Therefore it would be the easiest solution to solve SQL problems via SQL Server Studio Management rather than making code changes to a release of a product.
The values of parameters can be viewed in SQL Server Profiler (Sql Server Management Studio: Tools -> SQL Server Profiler). It can be configured to save trace to db (if you can't reproduce an issue on test environment), and catch only certain calls (for example you can specify the name of your stored procedure).
Here is screen shot of profiler with stored procedures calls with all parameters:
How does one debug a T-SQL stored procedure in a multi-tier application in Visual Studio 2012?
To be clear, I want to set a breakpoint in a sproc in VS 2012, and hit it when the sproc is called from an ASP.NET WebForms app in the same debugging session.
When following the same steps as for VS 2010, the breakpoints aren't hit inside the sproc.
Debugging T-SQL in a sproc on a SQL Server 2008 R2 Express database works as expected in Visual Studio 2010.
To be sure everything was enabled properly, I went over the instructions for VS 2010 (here), but no such page exists for VS 2012 or .NET 4.5.
It seems the missing step is to enable "Application Debugging", but no such option exists in the Server Explorer > Data Connections context menu in VS 2012.
VS 2010 Application Debugging
VS 2012 No Application Debugging
You need to open "SQL Server Object Explorer. Not "Server Explorer". That is what is different between 2010 & 2012. Then right click on the server and select "Application Debugging".
This is for VS2012 and SQL2012. Yes things are somewhat different for other versions, but kinda follow similar setup. It is tricky because one has to have various settings just right or it won't work.
vanilla install of both VS and SQL with all correct options (how to do this is outside scope of this article)
full admin rights to entire environment and sysadmin rights to sql (fundamentally a development environment; you would not want to do this in a production environment)
i always debug .net web apps under full local IIS which is a windows feature installed from control panel
go .net project properties, web, use local iis web server (i also stick to default port 80)
while i develop my databases etc using VS sql server database project, i always work under full standalone local SQL; i use the schema compare to refresh that sql with changes made in VS (how to do this is outside scope of this article)
do not place your breakpoint on that copy of the sp; that version of the sp is only a blueprint and not the runtime version
go .net project properties, web, debuggers (at bottom) enabled for .net and sql server
view sql server object explorer (not server explorer)
click add sql server, add your full local sql server using sysadmin credentials (i always use sa for such work)
right click on your full local sql server and enable application debugging
there go find the sp and right click and view code, then add your breakpoint there; this is runtime version of sp (note that i never make changes to sp there, i go do these in my master copy under the database project)
rebuild solution, and execute .net web app in debug mode from within ide by clicking green arrow internet explorer
I hope I have not forgotten anything. If I have I'll come revised my post.
All this may sound complicated. It is. But with a little discipline and patience it is priceless.
Good luck.
I believe the easiest way to do this would be to add DB as a project to your solution. You can do this by right clicking on the DB in the SQL Server Object Explorer (SSOX). This is a really good way to develop and debug your DB's. After you have added the DB to your project, you can add breakpoints anywhere you want, and debug against LocalDB (or another target if you wish). The largest drawback to this approach is that your existing data will not migrate with you (although you will be able to easily publish any changes back to the SQL DB at will). See the documentation on SSDT on MSDN for further guidance.
The following in detail article explains exactly how to enable debugging of a Stored Procedure when a .Net application is executed.
http://www.sqlmag.com/content1/topic/debugging-stored-procedures-142054/catpath/sql-server
Is is possible to write MS SQL Server add-in? I'm thinking of some application integrated with database server available form SQL Server Enterprise Manager.
Have you looked into using extended stored procedures to provide add-in like functionality?
The only hang up would be to see it in the Enterprise Manager, but you can probably use Martin's post as a way to do that.
http://support.microsoft.com/kb/190987
You might consider researching extended stored procedures. They are a way to integrated C++ logic into SQLServer. It appears that they are being phased out in favor of CLR integration but there might be some really good ideas to be found is seeing what people have done with them.
The one thing that always scared me off of extended stored procedures is that if your code crashed the entire database goes down. They run in the SQL Server process and there is limited protection.
MSDN link:
Database Engine Extended Stored Procedure Programming
I'm not sure if this is too late but here are some links that I've used to get started in writing SSMS plug-ins.
http://jcooney.net/archive/2007/11/26/55358.aspx
http://www.karpach.com/ViewArticle.aspx?ArticleFileName=SQL-Server-Managment-Studio-Object-Explorer-Search-Add-In.htm
http://sqlblogcasts.com/blogs/jonsayce/archive/2008/01/15/building-a-sql-server-management-studio-addin.aspx
http://aspalliance.com/1374_Extend_Functionality_in_SQL_Server_2005_Management_Studio_with_Addins.all
I hope this helps
You can include a lot into MS SQL server 2005 and 2008 database. Including .Net code.
But there is no real way to extend (create plugin for) the Management Studio. (Formerly Enterprise Manager.)
However there are database management addins for Visual Studio (especially in Team System). Maybe there you can implement what you need.
Nothing directly that I know of. In SQL Server Management Studio (for 2005 and later) at least there's a pretty simple system for adding external tools to the menu. Enterprise Manager probably has something similar. So you could build an application that does whatever you want and add it to your menu.
The trick is deployment. There's almost certainly a way to add your app to the menu as part of an install package, but I haven't had to mess with it myself. Additionally, it sounds like what you really want to do is have the system deployed into the database itsefl, and have Enterprise Manager automatically detect it. I'm pretty sure you can't do that.
SQL Server Enterprise Manager is just an instance of Microsoft Management Console. If you look at the shorcut its command line is this:
C:\WINDOWS\system32\mmc.exe /32 "C:\WINDOWS\system32\SQLServerManager.msc"
You should be able to create a new Snap In that sits beside SQL Server in the tree hierachy.
Take a look at http://msdn.microsoft.com/en-us/library/ms692755(VS.85).aspx to see how this is done.