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

谁说我不能喝 提交于 2019-12-23 08:51:57

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/17028961/how-do-i-create-a-sql-function-to-return-a-bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!