Looking for suggestions RRS feed

  • Question

  • I have two stored procedures (l'll call them P1 & P2).  P1, after a lot of processing, creates a temporary table that is used by P2 after an "exec P1" is done.  I've separated the logic into two stored procedures because, ultimately, other sprocs will need the output of P1.

    I get an error if I use #tempTable as the output table in P1 because it no longer exists after P1 finishes.  ##tempTable works, but I'm concerned about concurrency issues.  Any suggestions on what construct(s) I should be using?

    Thanks in advance!



    Tuesday, September 5, 2006 8:54 PM

All replies

  • Global temporary table (GTT) (##tempTable) might not be the one you need

    here is the downside of using GTT.


    1. you cannot use it to cache Data for a long period of time - let say 1 day

    If all the reference to GTT is drop then the data in it is also lost forever

    2. GTT is visible to all stored procedure using it -  thus concerrency issue

              This wont cause you any problem if the data in it is readonly. meaning

               You process only once for the entire day. if you'll be cjhanging the content

                of GTT from time to time this will cause you headache


    two recommendations:

    1. if the resultset of P1 will have to live for at least 1 day and there will be no processing required  for that span of time,

         I would recommend a physical table instead.

    2. if the result set  of P1 is dynamic and P1 needs to be reused from time to time

        I recommend that P1 be transformed into a table-valued function



    Tuesday, September 5, 2006 11:42 PM
  • I totally agree.  In fact, I would probably suggest (based on the phrase "lot of processing") that you create a couple of permanent tables.  One to hold the results, and the other to keep either:

    1.  Users of the data that you expect to read the data.  As these users read the data, delete their rows, when the last reader finishes, delete them.

    2.  An amount of time before the results are invalidated.  So if you could run it daily, have it be invalidated at midnight, and then users of the data would do their select, and if no data was there, or it was past it's date, it would build the cache, and if the data was there, then fetch it.

    If a lot of processing is not really a "lot" then you might use a table-valued function, but there are limitations, like no side effects, if there is any data being written in the process.

    Wednesday, September 6, 2006 4:01 AM