Link to home
Start Free TrialLog in
Avatar of dotnet0824
dotnet0824

asked on

sql server 2005 debugging

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.
Avatar of dublingills
dublingills
Flag of Ireland image

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
Avatar of dotnet0824
dotnet0824

ASKER

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.
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

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.
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
ASKER CERTIFIED SOLUTION
Avatar of dublingills
dublingills
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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