jueves, 3 de febrero de 2011

SQL Server: Temporary tables

In stored procedures you can create temporary tables, save data into it and use it like some sort of temporary repository, or anything you can think of. This is specially useful in complicated stored procedures. Remember, not every single client you have will prefer having the data logic in a data access layer in your application, some will prefer implementing that logic in the database itself. Heck, some even will want their business logic there.

This type of tool can come in very handy. You can just create a table like:

CREATE TABLE #Tmp (
ID int,
Name varchar(30) )

Note the # at the begining of it's name. This table will last during the whole sql server session, and will get deleted inmediately after that. However, if you can to delete it yourself with a DROP TABLE instruction, you can just do that. No error will be thrown.

PD: I promise one day I'll format the code on the blog a little better. It has a very rustic formatting right now.

No hay comentarios:

Publicar un comentario