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


Stored procedure returning wrong value ... -1


Stored procedure returning wrong value ... -1

Author
Message
om-471033
om-471033
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 27
Let me describe wat my Stored Procedure(SP) shown below is doing before i explain the prob....

CREATE procedure usp_msg_DeleteMultipleMsg
(
@MessageList varchar(500),@mode char(10)
)
AS
BEGIN
SET NOCOUNT ON

CREATE TABLE #TempList
(
MessageID numeric
)

DECLARE @MessageID varchar(10), @Pos int

SET @MessageList = LTRIM(RTRIM(@MessageList))+ ','
SET @Pos = CHARINDEX(',', @MessageList, 1)

IF REPLACE(@MessageList, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @MessageID = LTRIM(RTRIM(LEFT(@MessageList, @Pos - 1)))
IF @MessageID <> ''
BEGIN
INSERT INTO #TempList (MessageID) VALUES (CAST(@MessageID AS numeric)) --Use Appropriate conversion
END
SET @MessageList = RIGHT(@MessageList, LEN(@MessageList) - @Pos)
SET @Pos = CHARINDEX(',', @MessageList, 1)

END
END


if(@mode='sent')
update messagedetails set s_sentitems = 0
where messagedetail_pk in (select MessageID from #TempList) and s_sentitems is null

else if(@mode='read')
update messagedetails set s_read = 1
where messagedetail_pk in (select MessageID from #TempList) and s_read=0 and s_unread is null

else if(@mode='unread')
update messagedetails set s_unread = 1
where messagedetail_pk in (select MessageID from #TempList) and s_unread=0 and s_read is null

else if(@mode='r_pd')
update messagedetails set s_permanentlydeleted = 1
where messagedetail_pk in (select MessageID from #TempList) and (s_read=1 or s_unread=1)

else if(@mode='s_pd')
update messagedetails set s_sentitems = 1
where messagedetail_pk in (select MessageID from #TempList) and s_sentitems=0



if(@@error=0)
return (1)

else
return (0)


END
GO

It accepts comma seperated value(CSV) string containing a list of meggase id in @messagelist. First part of the SP parses the parameter @messagelist, picks up each message id in it and stores it in a temporary table, #templist. 2nd part of SP updates flags in my table "messagedetails" based on the parameter @mode. It is supposed to return 1 on success and 0 if there is any problem.

My problem is....it returns -1 when success and 0 on failure(which is correct). No matter what you return but it returns only -1 on success. I had this problem before in another SP which i overcame by removing the declared variables E.G. @messagelist and @pos. But it is not possible to get rid of it in this case......please help
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29493 Visits: 19002
What are you expecting to error? Or rather - what is the error condition you're expecting to capture?

Although not necessarily the prettiest code ever - it doesn't look to me that it should routinely "fail" anywhere.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
om-471033
om-471033
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 27
Thanks for the reply Matt,

I just want to return 1 if the last statement( which will be one of the statements in else-if block) is executed successfully but it returns -1 on success. Is it because of the use of variables @messagelist or @pos...?
As i have mentioned in my post earlier, I had a similar problem before in another SP which i overcame by removing all such variables(don't know why). It returns -1 on success no matter what is returned from the SP.

There is one more thing, if instead of returning any value, a print statement E.G. print 'success' is given, then it works fine.........please help....
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