Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a SQL Function to return a BIT?

I am using this script below to create a function but I get an error in the messages log:

CREATE FUNCTION [dbo].[MyFunction] () RETURNS BIT AS RETURN CAST(1 AS BIT) 

Msg 102, Level 15, State 31, Procedure MyFunction, Line 1 Incorrect syntax near 'RETURN'.

It works when I change this to return a table:

CREATE FUNCTION [dbo].[MyFunction] () RETURNS TABLE AS RETURN (SELECT 1 [1])

so I am not sure what is wrong. Why does this work for a table but not a bit?

like image 332
Ian R. O'Brien Avatar asked Jun 10 '13 16:06

Ian R. O'Brien


1 Answers

Change your syntax to include a begin and end like so:

CREATE FUNCTION [dbo].[MyFunction]()
RETURNS bit 
AS 
begin 
RETURN CAST(1 AS bit) 
end
like image 177
Avitus Avatar answered Nov 01 '22 13:11

Avitus