Solved

Best practices for calling sp_executesql

Posted on 2006-07-20
8
370 Views
Last Modified: 2008-02-01
I'm quite new to writing sp's. I'm making extensive use of exec sp_executesql and want to make sure that I get to know when things don't go right.

I know that sp_executesql returns zero on success, and also the @@error gets set to a non-zero value if there's a problem. Currently i'm checking both these values. Do I need to check both? Can problems occur that will cause one of them to be non-zero while the other is zero?

I just want to know what's the established way of trapping problems.

Ian
0
Comment
Question by:ipendlebury
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17144638
check both. there could be errors with the call of sp_executesql that are not passed into @@error or vice-versa...
0
 
LVL 12

Assisted Solution

by:Einstine98
Einstine98 earned 125 total points
ID: 17144724
Debugging dynamic SQL is not easy (to me is always a nightmare) ... one way to go around it is to use dynamic SQL to build stored procedures on the fly and trap the errors there... but that works in certain cases and doesn't work in many others...

Anyway, no easy way, check both outcomes...
0
 

Author Comment

by:ipendlebury
ID: 17144745
Ok, i'll keep checking both. Thanks for your input.

Ian
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17144748
here is an example
create table test (i int )
GO

DECLARE @err int
exec sp_executeSQL N'EXEC @err = sp_rename ''a'',''aa'' ', N'@err int output' ,@Err output

select @@ERROR Error, @Err InsideDynSqlErr


exec sp_executeSQL N'EXEC @err = sp_rename ''test'',''test1'' ', N'@err int output' ,@Err output
select @@ERROR, @Err InsideDynSqlErr
0
 

Author Comment

by:ipendlebury
ID: 17144924
aneeshattingal:

I understand perfectly the exec statements, but what purpose do the Error and InsideDynSqlErr perform in the subsequent select statements?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17145023
ipendlebury,
> insideDynSqlErr perform in the subsequent select statements?

Did you run my example,
In the first part, i was trying to rename an object which was not there .. if you run the statement inside the sp_execute, ie. sp_rename 'a','aa' ,  and check the value , it will returnn the error value > 0, i.e. the statement was a fail.
But when you run the first statement , then you wont be able to cath the error inside the SP_execute, the @@Error will retun the error value of the 'sp_execute' statement and not the statements inside the sp_executeSQl
0
 

Author Comment

by:ipendlebury
ID: 17145125
I'm sorry i'm a beginner. It's the syntax I didn't understand.

select @@Error as Error, @Err as InsideDynSqlError is what i'm used to.

I understand now. Thanks for your help.

Ian
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17145406
>select @@Error as Error, @Err as InsideDynSqlError

This is just for showing the @@Error values

Here @@Error returns the value after thesp_executeSQL call and shows as 'Error'
the otherone , the value we caught inside the sp_executeSQl statement ...
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

752 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