Solved

Best practices for calling sp_executesql

Posted on 2006-07-20
8
363 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
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
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.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

806 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