You must Sign In to post a response.
  • Category: ASP.NET

    How to exit from the sql function


    Are you looking for a way to exit from sql function ? then read this thread to know how to exit from function



    Hi All,

    I am creating a function like below ..... but it is not working. Could you please help me how can i exit from the the funtions if the condition is false.

    CREATE FUNCTION fn_isFileIsValidToAttachBox
    (
    @ItemID INT
    )
    RETURNS BIT
    AS

    BEGIN

    SELECT @CHILDITEMDISPVALIDATION = CHILDITEMDISPVALIDATION FROM SYSACCT

    IF(@CHILDITEMDISPVALIDATION = 1)
    SET @isFileIsValid = 0
    RETURN -1
    ELSE IF(ISNULL(@CHILD_DISP_ACTION_DATE,0) = 0)
    SET @isFileIsValid = 1
    RETURN -1
    ELSE IF (ISNULL(@CHILD_DISP_ACTION_DATE,0) > @PARENT_DISP_ACTION_DATE)
    SET @isFileIsValid = 0
    RETURN -1
    ELSE IF ((@PARENT_DISP_ACTION_DATE - @CHILD_DISP_ACTION_DATE) <= @AllowedDaysBefore)
    SET @isFileIsValid = 1
    RETURN -1
    ELSE IF ( (@PARENT_DISP_ACTION_DATE - @CHILD_DISP_ACTION_DATE) > @AllowedDaysBefore)
    SET @isFileIsValid = 0
    RETURN -1

    RETURN @isFileIsValid
    END
  • #728237
    You can set the RETURN in order to exit. retrun any value which could give a meningful information.
    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #728240
    Hello Krishna Prasad,

    I din't worked with Functions anymore. But while searching on MSDN Library, I found a nice article for you.

    Please go through it, might be helpful.


    http://technet.microsoft.com/en-us/library/ms186755.aspx


    All the Best.

    -------------
    Glad to be,
    John Bhatt
    Editor - DNS Forums
    https://www.pyarb.com

  • #728243
    Hi,

    To exit from any function, you can simply use the 'return' keyword.

    Ex:
    IF 1 = 1
    BEGIN
    -- Some statements
    RETURN
    END

    If you want to exit from any loop, you can use 'break' keyword

    Ex:
    WHILE 1 = 1
    BEGIN
    -- Do work here
    -- If you need to stop execution then use a BREAK

    BREAK: Make sure to have this break at the end to prevent infinite loop

    Thanks & Regards,
    Mahesh Durgam

  • #728253
    There is no exit like statement in SQL function but you can write RETURN to get return from the function
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #728367
    You need to use RETURN keyword under loop for exit from SQL function.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.