Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Best practices for calling sp_executesql

Posted on 2006-07-20
8
Medium Priority
?
396 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
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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 500 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

885 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