SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Why does my stored procedure return -1?


Why does my stored procedure return -1?

Author
Message
junk.mail291276
junk.mail291276
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2253 Visits: 120
Hello,

I have a stored procedure. The call to it returns -1. What does this mean?

Here is the code that calls the stored procedure (auto-generated by Entity Framework):


public virtual int DeleteProjectData(Nullable<int> projectId, string deleteType, string username)
{
var projectIdParameter = projectId.HasValue ?
new ObjectParameter("projectId", projectId) :
new ObjectParameter("projectId", typeof(int));

var deleteTypeParameter = deleteType != null ?
new ObjectParameter("deleteType", deleteType) :
new ObjectParameter("deleteType", typeof(string));

var usernameParameter = username != null ?
new ObjectParameter("username", username) :
new ObjectParameter("username", typeof(string));

int result = ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("DeleteProjectData", projectIdParameter, deleteTypeParameter, usernameParameter);
return result; // result is -1
}


Here is the stored procedure:

ALTER PROCEDURE [dbo].[DeleteProjectData]
@projectId INT,
@deleteType VARCHAR(10),
@username NVARCHAR(255)
AS

SET NOCOUNT ON

BEGIN TRY

DECLARE @realProjectId INT = NULL,
@isTemplate BIT,
@ErrorMessage NVARCHAR(4000),
@deleteTypeAll VARCHAR(10),
@deleteTypeNodes VARCHAR(10)

DECLARE @usernameBinary VARBINARY(255)
SET @usernameBinary = CAST(@username AS VARBINARY(255))
SET CONTEXT_INFO @usernameBinary

SELECT @deleteTypeAll = 'All',
@deleteTypeNodes = 'Nodes'

-- ensure the input project exists and is not a template
SELECT @realProjectId = ProjectId
FROM Project
WHERE ProjectId = @projectId

IF @deleteType NOT IN (@deleteTypeAll, @deleteTypeNodes)
BEGIN
-- create a the error message for a project that does not exist
SELECT @ErrorMessage = N'The input Delete Type is not valid. Valid values are: ''' + @deleteTypeAll
+ ''' and ''' + @deleteTypeNodes + ''''

-- raise the error
RAISERROR (@ErrorMessage, 16, 1)

END

IF @realProjectId IS NULL
BEGIN
-- create a the error message for a project that does not exist
SELECT @ErrorMessage = N'The input ProjectId does not have a corresponding Project record. No calculations will be performed. ProjectId: ' + CAST(@projectId AS VARCHAR)

-- raise the error
RAISERROR (@ErrorMessage, 16, 1)

END

BEGIN TRANSACTION

-- If deleting all, delete all reports associated with project first:
IF @deleteType = @deleteTypeAll
BEGIN

DELETE [dbo].[ReportComment]
WHERE ReportId in (SELECT ReportId
FROM [dbo].[Report]
WHERE ProjectId = @realProjectId)

DELETE [dbo].[ReportMetric]
WHERE ReportId in (SELECT ReportId
FROM [dbo].[Report]
WHERE ProjectId = @realProjectId)

DELETE [dbo].[ReportTopN]
WHERE ReportId in (SELECT ReportId
FROM [dbo].[Report]
WHERE ProjectId = @realProjectId)

DELETE [dbo].[Report]
WHERE ReportId in (SELECT ReportId
FROM [dbo].[Report]
WHERE ProjectId = @realProjectId)

END
ELSE
BEGIN

DELETE [dbo].[ReportTopN]
WHERE ReportId in (SELECT ReportId
FROM [dbo].[Report]
WHERE ProjectId = @realProjectId)

END

IF @deleteType IN (@deleteTypeAll, @deleteTypeNodes)
BEGIN
DECLARE @Keys TABLE (
ProjectId INT,
NodeId INT,
DeviationId INT,
CauseId INT,
ConsequenceId INT,
SafeguardId INT,
RecommendationId INT,
RemarkId INT,
DrawingId INT,
RiskDataId INT,
BowtieLoopId INT,
BowtieId INT)

-- build a list of keys associated with the project (simplifies delete queries)
INSERT INTO @Keys (ProjectId,
NodeId,
DeviationId,
CauseId,
ConsequenceId,
SafeguardId,
RecommendationId,
RemarkId,
DrawingId,
RiskDataId,
BowtieLoopId,
BowtieId)
SELECT p.ProjectId,
n.NodeId,
d.DeviationId,
ca.CauseId,
co.ConsequenceId,
s.SafeguardId,
r.RecommendationId,
re.RemarkId,
dr.DrawingId,
rd.RiskDataId,
bl.BowtieLoopId,
b.BowtieId
FROM Project p
LEFT OUTER JOIN Node n
ON p.ProjectId = n.ProjectId
LEFT OUTER JOIN Deviation d
ON n.NodeId = d.NodeId
LEFT OUTER JOIN Cause ca
ON d.DeviationId = ca.DeviationId
LEFT OUTER JOIN Consequence co
ON ca.CauseId = co.CauseId
LEFT OUTER JOIN Safeguard s
ON co.ConsequenceId = s.ConsequenceId
LEFT OUTER JOIN Recommendation r
ON co.ConsequenceId = r.ConsequenceId
LEFT OUTER JOIN Remark re
ON co.ConsequenceId = re.ConsequenceId
LEFT OUTER JOIN Drawing dr
ON r.RecommendationId = dr.RecommendationId
LEFT OUTER JOIN Bowtie b
ON b.ProjectId = p.ProjectId
LEFT OUTER JOIN BowtieLoop bl
ON bl.BowtieId = b.BowtieId
LEFT Outer JOIN RiskData rd
ON rd.BowtieLoopId = bl.BowtieLoopId

WHERE p.ProjectId = @realProjectId

-- delete the data that was imported

DELETE FROM Drawing
WHERE DrawingId IN (SELECT DISTINCT DrawingId
FROM @Keys)

DELETE FROM Recommendation
WHERE RecommendationId IN (SELECT DISTINCT RecommendationId
FROM @Keys)

DELETE FROM Safeguard
WHERE SafeguardId IN (SELECT DISTINCT SafeguardId
FROM @Keys)

DELETE FROM Remark
WHERE RemarkId IN (SELECT DISTINCT RemarkId
FROM @Keys)

DELETE FROM Consequence
WHERE ConsequenceId IN (SELECT DISTINCT ConsequenceId
FROM @Keys)

DELETE FROM CauseToBowtieLoopDetails
Where CauseId IN (Select Distinct CauseId
FROM @Keys)

DELETE FROM Cause
WHERE CauseId IN (SELECT DISTINCT CauseId
FROM @Keys)

DELETE FROM Deviation
WHERE DeviationId IN (SELECT DISTINCT DeviationId
FROM @Keys)

DELETE FROM Node
WHERE NodeId IN (SELECT DISTINCT NodeId
FROM @Keys)

DELETE FROM RiskData
WHERE RiskDataId IN (SELECT DISTINCT RiskDataId
FROM @Keys)

DELETE FROM BowtieLoop
WHERE BowtieLoopId IN (SELECT DISTINCT BowtieLoopId
FROM @Keys)

DELETE FROM BowtieToEquipmentLookup
WHERE BowtieId IN (SELECT DISTINCT BowtieId
FROM @Keys)

DELETE FROM Bowtie
WHERE BowtieId IN (SELECT DISTINCT BowtieId
FROM @Keys)

DELETE FROM ProjectDeviation
WHERE ProjectId = @realProjectId

DELETE FROM ProjectSafeguard
WHERE ProjectId = @realProjectId

DELETE FROM ProjectRecommendation
WHERE ProjectId = @realProjectId

-- also delete from the metrics tables

DELETE FROM ProjectMetrics
WHERE ProjectId = @realProjectId

DELETE FROM CauseMetrics
WHERE ProjectId = @realProjectId

DELETE FROM ConsequenceMetrics
WHERE ProjectId = @realProjectId

DELETE FROM RecommendationMetrics
WHERE ProjectId = @realProjectId

DELETE FROM SafeguardMetrics
WHERE ProjectId = @realProjectId

END

IF @deleteType = @deleteTypeAll
BEGIN

-- delete the project specific data (i.e., data not imported)

DELETE FROM RiskMatrixAxis
WHERE ProjectId = @realProjectId

DELETE FROM SafeRecCategory
WHERE ProjectId = @realProjectId

DELETE FROM Participant
WHERE ProjectId = @realProjectId

DELETE FROM RiskRanking
WHERE ProjectId = @realProjectId

DELETE FROM Category
WHERE ProjectId = @realProjectId

DELETE FROM ImportFile
WHERE ProjectId = @realProjectId

DELETE FROM Project
WHERE ProjectId = @realProjectId

END

COMMIT TRANSACTION

END TRY

BEGIN CATCH

IF(@@TRANCOUNT > 0)
BEGIN
-- rollback all changes if any error occurred
ROLLBACK TRANSACTION
END

-- raise the original error
EXEC RethrowError;
END CATCH


Can anyone see why the stored procedure is returning -1? And am I right in interpreting -1 as an error?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)

Group: General Forum Members
Points: 401795 Visits: 43466
What does the procedure RethrowError do?


Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sergiy
Sergiy
SSC Guru
SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)

Group: General Forum Members
Points: 100767 Visits: 14351
Negative return value means error happened during the execution.
Unless you explicitly change the meaning of @ReturnValue in the proc code.
Like
IF @Status = 'Feeling Kinda Silly'
RETURN -1

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)

Group: General Forum Members
Points: 897371 Visits: 48321
I'll also add that all those IN (SELECT DISTINCTs are a waste of clock cycles because IN will always stop at the first occurrence of whatever it is you're looking for except NULL.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
GilaMonster
GilaMonster
SSC Guru
SSC Guru (920K reputation)SSC Guru (920K reputation)SSC Guru (920K reputation)SSC Guru (920K reputation)SSC Guru (920K reputation)SSC Guru (920K reputation)SSC Guru (920K reputation)SSC Guru (920K reputation)

Group: General Forum Members
Points: 920178 Visits: 48865
Jeff Moden - Tuesday, March 6, 2018 8:03 PM
I'll also add that all those IN (SELECT DISTINCTs are a waste of clock cycles because IN will always stop at the first occurrence of whatever it is you're looking for except NULL.


The optimiser's smarter than that, it knows they're not needed.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


junk.mail291276
junk.mail291276
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2253 Visits: 120
Thanks everyone,

Turns out this was causing the return of -1:
SET NOCOUNT ON
If I set NOCOUNT to OFF, it returns the number of rows.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)SSC Guru (897K reputation)

Group: General Forum Members
Points: 897371 Visits: 48321
junk.mail291276 - Wednesday, March 7, 2018 7:42 AM
Thanks everyone,

Turns out this was causing the return of -1:
SET NOCOUNT ON
If I set NOCOUNT to OFF, it returns the number of rows.


Possibly a bad mistake especially if your app is relying on the single return of error number.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)SSC Guru (401K reputation)

Group: General Forum Members
Points: 401795 Visits: 43466
Not only that, you still didn't answer my question.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search