T-SQL RETURN not working

I have a problem with My T-SQL to see if there is an EXISTS element in the table, but I have an error message

Msg 178, Level 15, State 1, Line 2 The RETURN return statement cannot be used in this context. Msg 178, Level 15, State 1, Line 4 The RETURN statement with return value cannot be used in this context.

IF EXISTS(SELECT COUNT(timesheetID) FROM [TaskManagementSystem_DB].[dbo].[Timesheet_entry] WHERE userID ='12' AND CONVERT(date, startTimeStamp)=CONVERT(date, getdate())) RETURN 1 else RETURN 0 

All code:

 ALTER PROCEDURE [dbo].[CheckTimesheetIsEXISTS_forUser] @UserID uniqueidentifier AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; IF EXISTS(SELECT COUNT(timesheetID) FROM [TaskManagementSystem_DB].[dbo].[Timesheet_entry] WHERE userID ='3fd971f7-e6e8-40fe-a90d-a7c9df8bf7b5' AND CONVERT(date, startTimeStamp)=CONVERT(date, getdate())) RETURN 1 else RETURN 0 END 
+4
source share
3 answers

Try the following:

 ALTER PROCEDURE [dbo].[CheckTimesheetIsEXISTS_forUser] @UserID uniqueidentifier AS BEGIN SELECT CASE WHEN COUNT(timesheetID) > 0 THEN 1 ELSE 0 END AS isExists FROM [TaskManagementSystem_DB].[dbo].[Timesheet_entry] WHERE userID = @UserID AND CONVERT(date, startTimeStamp)= CONVERT(date, getdate()) END 
+2
source

Return works inside stored procedures / user functions.

A simple tsql is just a package of commands. I think you want to write SELECT instead of RETURN.

+6
source

You can only call return in stored procedures and functions. I guess this is not the case either.

+3
source

Source: https://habr.com/ru/post/1432958/


All Articles