Pete Freitag Pete Freitag

Temporary Stored Procedures on SQL Server

Published on September 07, 2005
By Pete Freitag
databases

I posted about doing fast bulk inserts with PostgreSQL last week, and with MySQL a while back. Now its time for Microsoft SQL Server.

I'm using a technique similar to what I used for PostgreSQL for SQL server. I'm just creating a temporary stored procedure, and then calling it lots of times. I know you could also create a stored procedure on the server to do this, but here's how you might to it with a temporary one:

SET NOCOUNT ON
GO
CREATE PROC #tempInsertProc
  @id integer
AS
  INSERT INTO foo (bar) VALUES (@id)
GO
EXEC #tempInsertProc 10
GO
EXEC #tempInsertProc 11
GO
EXEC #tempInsertProc 12
GO
DROP PROC #tempInsertProc
GO
SET NOCOUNT OFF
GO

Temporary Stored Procedures

Temporary stored procedures on Microsoft SQL Server are prefixed with a pound sign #. One pound sign means that its temporary within the session, two pound signs ## means its a global temporary procedure, which can be called by any connection to the SQL server during its lifetime.

Check out Microsoft's documentation of CREATE PROCEDURE for more info.

Why temporary procedures?

Your probably wondering why create temporary procedures, when you can just create a permanent stored procedure? In most cases its probably better to use a permanent SP, but if your like me, and don't like putting too much logic in the DB, but need to use a stored procedure, then these are one way to go.



sql sqlserver stored procedures

Temporary Stored Procedures on SQL Server was first published on September 07, 2005.

If you like reading about sql, sqlserver, or stored procedures then you might also like:

Discuss / Follow me on Twitter ↯

Comments

Another thought. What if you create a temp table with no indexes in memory, insert everything in there, and then do an INSERT..SELECT statment to move the records to the real table?
by Barney on 09/07/2005 at 5:26:58 PM UTC
That's a good idea, I'll have to test it out!
by Pete Freitag on 09/07/2005 at 5:36:12 PM UTC
Can you explain a little more why you would actually want to do this? Have you found a performance improvement? I would think that heavy use of this would bloat up your tempdb. So would using temp tables. I used to use a lot of temp tables, but I've since switched to using table variables. They work about the same and you don't have to worry about deadlocking your tempdb.

If you REALLY have a lot of records to import, you might want to try spooling them to text and using bulk insert.
by Mike Rankin on 09/07/2005 at 7:19:44 PM UTC
well maybe you dont have permission to create permanent stored procedures, this is a good way to test it until its ready for production then you can get your dba guy to do it
by abdullah on 01/25/2007 at 11:48:16 AM UTC
When I am creating the sql drive to use my database, in the step "change the default database to" the checkbox near "create temporary stored...." is always checked and hibernating!!!. my question is how can I deactivate the hibernation because I don't want to use a temporary file. thank you in advance.
by Amine on 02/27/2009 at 1:58:00 PM UTC