Solved

Best practices for calling sp_executesql

Posted on 2006-07-20
8
359 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Ok, i'll keep checking both. Thanks for your input.

Ian
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:ipendlebury
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now