Solved

sql server 2005  debugging

Posted on 2009-05-18
7
215 Views
Last Modified: 2012-05-07
Hi,
I want to debug a lengthy stored procedure in SQL Server 2005 which uses lot of  Table variables and temporary tables and produces output finally.

Its been difficult to analzye what each query's data would be like.
Is there a way I could write it a file or something to see what data each query would produce. .. I am aware to use print statements for variables etc, but for Data being retrieved by all these temporary tables etc how could we know what data is being retrieved.
0
Comment
Question by:dotnet0824
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:dublingills
ID: 24415777
Hi,

If you're running your proc in Management Studio you can put select statements into the procedure to display the results of each query and table variable as the procedure runs, i.e.
select * from @TABLE, select * from #TABLE etc.

If you have access to Visual Studio you can debug a stored procedure line by line, see this link for help on doing that:
http://www.sqlteam.com/article/debugging-stored-procedures-in-visual-studio-2005

HTH
0
 

Author Comment

by:dotnet0824
ID: 24416368
Hi,
Thanks for it. As I know thats possible like quickwatch etc. But how could i just type in the SQL stored proc window  Select * from #temp (Which doesnot exist earlier) and check its result like .. would the vs.net 2005 allow u to embed statements while debugging. It doesnot allow u that right.!! correct me if i am wrong.
0
 
LVL 4

Expert Comment

by:dublingills
ID: 24416672
No you're not wrong on that score, and in fact the output window doesn't display inline select statement results until the procedure exits.

The only option I can suggest to you is the one that I use myself which is to create what I call a debug copy of the stored proc and put select statements inline to display the contents, you then call the debug procedure from management studio which does display inline selects as it goes.  Something like this (if you can understand my gibberish!):
create procedure spTest as
insert table1 (field1, field2) select field1, field2 from table2;
insert table1 (field1, field2) select field1, field2 from table3;
go
 
create procedure spTestDebug as
insert table1 (field1, field2) select field1, field2 from table2;
 
--debug bit
select * from table1;
 
insert table1 (field1, field2) select field1, field2 from table3;
go

Open in new window

0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 

Author Comment

by:dotnet0824
ID: 24416794
thanks a lot.  OR another way is to comment out everthing until u want the result to be printed. check it and uncomment rest and go by that way till end to understand.
0
 

Author Comment

by:dotnet0824
ID: 24416829
Another way I was thinking is
Is there a way we can write the results of queries to Textfiles or whatever like in SQLServer we can execute a query to a grid or table etc right..!!

Just wondering if we could put such kind of write statements in the stored proc so that it would be easier for us to view the output in textfiles or whatever. If you have done could u please send those statements
0
 
LVL 4

Accepted Solution

by:
dublingills earned 250 total points
ID: 24416891
Sure.  If you're not opposed to third party tools you may want to take a look at this:

http://www.sqlsolutions.com/products/sql-ultimate-debugger/


0
 
LVL 4

Expert Comment

by:dublingills
ID: 24416907
If xp_cmdshell is enabled try this in your stored proc:
EXEC xp_cmdshell 'bcp "SELECT * FROM [tableName]" queryout "C:\textfile.txt" -T -c -t,' 

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question