Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:28:18+00:00 2026-06-06T17:28:18+00:00

I am trying to create a function as per my requirements. But, when am

  • 0

I am trying to create a function as per my requirements.

But, when am creating or drop #tempTable, it is giving an error as:

invalid use of a side-effecting operator ‘drop object’ within a function

My understanding is that we can’t have create, drop or insert operations on #temptable in a function.

Is that correct?

My SQL:

CREATE FUNCTION [dbo].[RT_ResultFunction]
(
    Id VARCHAR(4000)
)
RETURNS @RT_ResultFunction TABLE 
(   
    Id VARCHAR(20)
    , Name varchar(20)
    ,Balance Int
)
AS
BEGIN
    IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL  
       DROP TABLE #tempTable

    SELECT Id, COUNT(Balance) 
    INTO  #tempTable
    'Balance' FROM Table1

    INSERT  @RT_ResultFunction 
        SELECT T1.ID,T1,NAME,T2,Balance 
        FROM    Table2 T1, 
                #tempTable T2
        WHERE T1.ID = T2.ID

    RETURN
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:28:20+00:00Added an answer on June 6, 2026 at 5:28 pm

    That is correct – you cannot have side effecting statements:

    From here: http://msdn.microsoft.com/en-us/library/aa175085(v=sql.80).aspx

    The statements in a BEGIN…END block cannot have any side effects.
    Function side effects are any permanent changes to the state of a
    resource that has a scope outside the function such as a modification
    to a database table. The only changes that can be made by the
    statements in the function are changes to objects local to the
    function, such as local cursors or variables. Modifications to
    database tables, operations on cursors that are not local to the
    function, sending e-mail, attempting a catalog modification, and
    generating a result set that is returned to the user are examples of
    actions that cannot be performed in a function.

    What you would find, even without your DROP statement, is that any attempt to access a temp table will give you the message (such as a SELECT ... INTO #TMP):

    Cannot access temporary tables from within a function

    As @Dems points out, you can use table variables. Because these are variables, they are scoped within the function, and therefore aren’t side effecting.

    Your function might run as:

    ...
    
    BEGIN
        DECLARE @tempTable table (id varchar(20), rows int)
    
        insert @tempTable
        SELECT Id, COUNT(Balance) 
        FROM Table1
    
        INSERT  @RT_ResultFunction 
            SELECT T1.ID,T1,NAME,T2,Balance 
            FROM    Table2 T1, 
                    @tempTable T2
            WHERE T1.ID = T2.ID
    
        RETURN END
    

    Not tested or anything, but you get the gist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a function that sends 100 call per second, and
I'm trying to create a function that would append two vectors together, but I
I am trying to create a function that displays three buttons per row for
I am trying to create a function that updates another value, and initially (after
I'm trying to create a function that searches up a WebControl's parent-child relationship (basically
I am trying to create a function which will give me alphabet position when
I'm trying to create a function to create a new basewith another struct as
I am trying to create a function that will block access to some of
I'm trying to create a function that would dynamically allocate an array, sets the
I am trying to create a function that given a divid, and a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.