• As suggetsed by Koen it may be because of loops you are creating.

    Why don't you read this excellent article of Tally table from Jeff to avoid loop.

    The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url]

    --rhythmk
    ------------------------------------------------------------------
    To post your question use below link

    https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
    🙂