Results 1 to 2 of 2

Thread: to save a resultset in store procedure

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2000
    Location
    brooklyn
    Posts
    48
    I have several places where I used the subquery like this
    in my store procedure

    SELECT *
    FROM MASTER_NEW
    WHERE ProductCode NOT IN
    (SELECT ProductCode FROM MAPSDATA)



    I am going to use the subquery

    FROM MASTER_NEW
    WHERE ProductCode NOT IN
    (SELECT ProductCode FROM MAPSDATA)



    in serveral other places in the stored procedure, like

    select count(*)
    FROM MASTER_NEW
    WHERE ProductCode NOT IN
    (SELECT ProductCode FROM MAPSDATA)


    Is there any ways that I can save the set once and use over and over again to save
    some time?

    Thanks a lot. Any tips will greatly appreciated

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    You can use CREATE TABLE to create a temp table at the beginning of your Stored Procedure - a temp table always starts with a # sign;

    CREATE TABLE #TEMPTABLE

    (see the t_SQL Help for more details on creating temp tables)
    You can then fill this table with the data in your subquery using

    INSERT INTO #TEMPTABLE SELECT... (again look in the help for the full syntax)

    Then you can use this #TEMPTABLE in any of your queries in that stored procedure. You should DROP the table at the end of the procedure (although it should disappear on it's own if you logoff SQL)

    Hope this helps.

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width