Run stored procedure when temp table is created/dropped

  • Hi guys and girls,

    I need to update 1 or 2 records in my database each time a temp table with the name like '##ABC_%' is created/dropped.

    (Note: Temp tables are created from my application.)

    I can do this in my application (VB.NET), but I didn't wrote it and the guy before me didn't understand multi-tier/OO.

    So I want to move the update part to the SQL Server (if possible).

    Is there a way in SQL2005 to detect the creation/deleting of a temp table and then run a SP?

    All ideas/thoughts are welcome.

    Grtz

    Jo

  • I checked to be sure and DDL triggers FOR CREATE_TABLE do not fire when a global temp table is created. I do not know of way to accomplish what you're after.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • You could do this with SQL Server Integration Services (SSIS) running on a tight schedule (every few minutes?) or with SSIS and the FileWatcher task (available from http://www.sqlis.com), which will detect changes to folders on the server.

    Edit: Sorry, I may have mis-read your post. You're looking at temp tables, not files dropped to a folder. My solution probably won't solve your problem then.

    Rob Schripsema
    Propack, Inc.

  • Thanks for the replies.

    Does any one else have an idea how to solve this?

    grtz

    Jo

  • I did explore this further yesterday and wondered about your apps architecture. You could create these global temp files (##ABC) and look through the tempdb.sys.tables table to locate them, but they will only exist for as long as there exists a connection that references them. So, if your VB app creates them, writes to them and then disappears (dropping the connection), the file(s) will be dropped also.

    But, if the VB app is continously running and keeping the SQL connection open, you could create a 'polling' job that would execute every N minutes/seconds that would query tempdb.sys.tables for entries for temp file names that match your pattern. So, your update process wouldn't be 'triggered' by an event, but would be 'discovered' by a process that goes out and checks (polls) for data.

    Then you'd probably have to use some dynamic SQL to actually query those temp tables to get the data out of them for your update process.

    Not sure I'd agree that this is the most robust architecture, but I suppose it depends on your system and your situation.

    Rob Schripsema
    Propack, Inc.

  • i am wondering if you could put an extended event for CREATE_TABLE on the tempdb?

    I'll grab one of my sample extended events and try it, but it's just an idea for now;

    i'd think that the extended event would be destroyed on start/stop of the server, so you'd need something that keeps adding it back, if an extended event is even allowed.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (1/22/2013)


    i am wondering if you could put an extended event for CREATE_TABLE on the tempdb?

    I thought of that too, but it's SQL 2005.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Hm. Maybe using SQL Profiler? Trap all "CREATE TABLE #" commands? ...just a thought...

  • Hi,

    It seams to be not that easy/possible to catch this at server-side.

    I'm going to dig into the code and seach for those temp tables (and centralize it :-)).

    Thanks for the help on this topic.

    grtz

    Jo

Viewing 9 posts - 1 through 8 (of 8 total)

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