Need some quick help! How to convert function to SP

  • Hi,

    How can I convert the below function to a Stored procedure? Please let me know. Appreciate it.   The function is causing the slowness and so I wanted to try out the SP scenario. Could you please let me know how can we convert this fn to SP.

     

  • Considering that the above is a multi-line table value function, the syntax for creating the 2 is very similar. Just change FUNCTION to PROCEDURE, remove the RETURNS clause and you're basically there. The only other thing is don't INSERT the data into a table variable, and instead just SELECT the dataset.

    Though I would also remove the NOLOCK hints, unless you really need them (which I doubt); if so why?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Great, I am almost there, quick question,

     

    The only other thing is don't INSERT the data into a table variable, and instead just SELECT the dataset. - SHould I just comment out the INSERT part and keep the select part for each INSERT in the existing function?

    Also, I need to change the table variable to the temp table in the NEW SP. So, is changing @ --># should do the trick?

     

    • This reply was modified 3 years, 4 months ago by  sizal0234.
    • This reply was modified 3 years, 4 months ago by  sizal0234.
  • Note to OP: Please click the Insert/edit code sample button when adding code to your posts, it's really not difficult to use.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • sizal0234 wrote:

    The only other thing is don't INSERT the data into a table variable, and instead just SELECT the dataset. - SHould I just comment out the INSERT part and keep the select part for each INSERT in the existing function?

    I don't follow what you're asking here, if I am honest. The table variable, @TodaysAWNegBal won't exist due to the RETURNS clause being removed, so you just want to return the INSERT clause in the final statement. Though you could comment it out, it likely wouldn't make a lot of sense to anyone else reading it later.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • This is what I am trying to do

    Step 1  - Convert function to SP

    Step 2 - Change table variable to Temp Tbl

    and then I would test it out. This is just one scenario I wanted to try to see how things work out. Sorry about any confusion.

     

  • Why change the variable to a temporary table if you're just going to SELECT from it afterwards? Just skip the middle man.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • If you want a single return set like you'd get with the function you would need to union the two queries together.

  • Temp tables: instead of

    declare @table table (id int)

    use

    create table #table (id int)

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • For performance, first try converting it into an inline TVF; those get compiled directly into the code whereas a multi-line TVF does not.

    SET ANSI_NULLS OFF
    SET QUOTED_IDENTIFIER OFF
    GO
    CREATE FUNCTION [dbo].[fnGet_Example]

    ( @CDate DATE )

    RETURNS TABLE
    AS
    RETURN (
    WITH cte_date AS (
    SELECT DATEADD(DAY, -1, @CDate) AS SDate
    ),
    TodaysAWNegBal AS (
    SELECT
    Record_A_ID, Trans_Amount
    FROM
    dbo.tbHuge WITH (NOLOCK)
    CROSS JOIN cte_date
    WHERE
    Trans_TransType_ID = 30
    AND Trans_DateTime = SDate
    AND Trans_Amount < 0
    ),
    Tbl_LastPositiveAvailableBalance AS (
    SELECT BT1.Record_A_ID, MAX(BT2.Trans_DateTime) AS Trans_DateTime
    FROM TodaysAWNegBal BT1
    CROSS JOIN cte_date
    LEFT OUTER JOIN dbo.tbHuge BT2 WITH (NOLOCK)
    ON BT1.Record_A_ID = BT2.Record_A_ID
    WHERE
    BT2.Trans_TransType_ID = 30
    AND BT2.Trans_DateTime < SDate
    AND BT2.Trans_Amount >= 0
    GROUP BY BT1.Record_A_ID
    )
    SELECT
    NAB.Record_A_ID,
    NAB.Trans_Amount,
    PAB.PositiveDate
    FROM
    TodaysAWNegBal NAB
    JOIN Tbl_LastPositiveAvailableBalance PAB
    ON NAB.Record_A_ID = PAB.Record_A_ID
    )
    /*end of function*/
    GO

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • Instead of converting to a stored procedure - I would recommend changing this multi-statement table valued function to an inline-table valued function.

    It also looks like there could be some options to improve the performance, but that is going to require more information about what this function does and how to determine the data to be returned.

    Jeffrey Williams
    Problems are opportunities brilliantly disguised as insurmountable obstacles.

    How to post questions to get better answers faster
    Managing Transaction Logs

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply