Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 creating sp with bool return

Author  Topic 

Jalla2
Starting Member

1 Post

Posted - 2008-08-27 : 02:14:19
Hi all!

I am trying to create a stored procedure that will return a bool value (true if its there and false if its not).

This is what I hav come up with :


CREATE PROCEDURE dbo.excists

(

@serialNr int,@text varchar(50)

)


AS

SELECT Serial, Description

FROM AADI_Elboard_Table

RETURN 'true' IF Serial = @serialNr AND Description = @text


But I get an error message.

Could someone pleas help me make this correct?


Regards
--------------------------------------------------------------------------------
John

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-08-27 : 02:17:04
Stored procedure can return integer values. You can use it like.

CREATE PROCEDURE dbo.excists
(
@serialNr int,@text varchar(50)
)
AS
BEGIN
IF EXISTS ( SELECT Serial, Description FROM AADI_Elboard_Table )
Return 1
ELSE
Return 0
END
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2008-08-27 : 04:51:31
Yes you could but why would you?

You have a prime candidate here for a function.


CREATE FUNCTION existsInAADI_Elboard_Table (
@serialNr int
, @text varchar(50)
)
RETURNS BIT AS BEGIN
DECLARE @exists BIT SET @exists = NULL

IF EXISTS (
SELECT
Serial
FROM
AADI_Elboard_Table
WHERE
Serial = @serialNr
AND Description = @text
)
SET @exists = 1

ELSE SET @exists = 0

RETURN @exists
END


Then you can use it in IF statements and the like

IF existsInAADI_Elboard_Table(14, 'bar') = 1 BEGIN
..
..
..

-------------
Charlie
Go to Top of Page
   

- Advertisement -