Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return bool from stored proc

Tags:

I'm trying to work out how to write a store procdure which returns a boolean value. I started off writing the following one which returns an int.

USE [Database] GO /****** Object:  StoredProcedure [dbo].[ReturnInt]    Script Date: 09/30/2010 09:31:11 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[ReturnInt]  AS RETURN 3 

I'm unsure however how to write one to return a boolean value.

Can somebody help? Is it a bit value?

like image 402
Mr Cricket Avatar asked Sep 29 '10 20:09

Mr Cricket


People also ask

Can SQL return Boolean?

SQL Server does not support a Boolean type e.g. SELECT WHEN CAST(1 AS BIT) THEN 'YES' END AS result -- results in an error i.e. CAST(1 AS BIT) is not the same logical TRUE.

How do you pass a Boolean value in SQL?

when passing the param as LONG to the sql script is equivalent to asking if the Boolean variable is set to true. It looks like false is 0. and from what I can tell in vs 2017, you cant "leave" a Boolean ssis variable equal to anything but true or false.

Can a proc return a value?

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.


1 Answers

You can't. There is no boolean datatype and the procedure return code can only be an int. You can return a bit as an output parameter though.

CREATE PROCEDURE [dbo].[ReturnBit]  @bit BIT OUTPUT AS     BEGIN     SET @bit = 1     END 

And to call it

DECLARE @B BIT EXEC [dbo].[ReturnBit] @B OUTPUT SELECT @B 
like image 145
Martin Smith Avatar answered Sep 21 '22 14:09

Martin Smith