CREATE FUNCTION f_CursorExists(@CursorName sysname) RETURNS TABLE AS RETURN( SELECT * FROM( SELECT Name=@CursorName, Type=CAST('LOCAL' as varchar(8)), IsExists=CAST(CASE CURSOR_STATUS('LOCAL',@CursorName) WHEN -3 THEN 0 ELSE 1 END as bit) UNION ALL SELECT @CursorName,'GLOBAL', CASE CURSOR_STATUS('GLOBAL',@CursorName) WHEN -3 THEN 0 ELSE 1 END UNION ALL SELECT @CursorName,'VARIABLE', CASE CURSOR_STATUS('VARIABLE',@CursorName) WHEN -3 THEN 0 ELSE 1 END )a WHERE IsExists=1) GO