Solved

Temporary table visible only to the connection that created it

Posted on 2003-12-08
7
1,736 Views
Last Modified: 2008-02-26
I HEARD the noise but don't know where it came from.....

Is it possible to create a table in MSSQL that will be visible only to the connection that created it? What I want is a place to store information and manipulate it with the SQL language but I need to protect it so that I can have several copies running at the same time and each copy visible only to its client.

I can achieve it using permanent tables but this possibility is exactly what I need....

So if you can assure me that I can't do it, it will still be a valid answer....
0
Comment
Question by:rmaranhao
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:szacks
ID: 9897904
use a # sign in front of the table and it will only be available to the connection that created it.

example: create table #table1(F1 int, F2 int)

then you would use normal syntax with it. select * from #table1 you can even add indexes and stuff.
0
 
LVL 2

Expert Comment

by:poliguin
ID: 9897912
use a table variable

syntax:

DECLARE @MyTable Table(AnID INT, AValue VARCHAR(255))

INSERT INTO @MyTable SELECT ........


SELECT AnID, AVALUE FROM @MyTable
0
 
LVL 2

Expert Comment

by:poliguin
ID: 9897930
a note,

using CREATE #MyTable(....) will create a table that is accessible by the database.  if someone connects to the while someone is still in the stored procedure, you will get an error that states a table of name #MyTable already exists.  using a table variable allows you to not worry about that table already existing, for it's lifespan is unique to the execution of the stored procedure and many people can fire the same sp with a table variable at the same time or within the same execution time.  you can't do so with CREATE TABLE #MyTable(...).  a table variable works just like a regular table too.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 4

Accepted Solution

by:
szacks earned 125 total points
ID: 9898106
poliguin,

You are incorrect. If you open Query Analyzer for example and type create table #table1  and then open a second query window in QA and type create table #table1 it will create 2 tables that have no relationship to one another.
This is true in stored procedures as well, it's just very easy to test it in QA.
The way that # tables are handled by the database is they are created in the tempdb with somesort of unique identifier attached to the table name which is automatically handled by the engine wqhen you use the table name.
0
 
LVL 5

Author Comment

by:rmaranhao
ID: 9898387
szacks, you are perfecly correct.

Just one more doubt: I the connection is lost, how do I drop this table?
0
 
LVL 2

Expert Comment

by:poliguin
ID: 9898484
i stand corrected on that, i genearlly always run the same instance of qa, so never really noticed that before.


notes about usage of table variables (As taken from books online)
Use table variables instead of temporary tables, whenever possible. table variables provide the following benefits:

A table variable behaves like a local variable. It has a well-defined scope, which is the function, stored procedure, or batch in which it is declared.
Within its scope, a table variable may be used like a regular table. It may be applied anywhere a table or table expression is used in SELECT, INSERT, UPDATE, and DELETE statements. However, table may not be used in the following statements:

INSERT INTO table_variable EXEC stored_procedure

SELECT select_list INTO table_variable statements.

table variables are cleaned up automatically at the end of the function, stored procedure, or batch in which they are defined.

table variables used in stored procedures result in fewer recompilations of the stored procedures than when temporary tables are used.


Transactions involving table variables last only for the duration of an update on the table variable. Thus, table variables require less locking and logging resources.
Assignment operation between table variables is not supported. In addition, because table variables have limited scope and are not part of the persistent database, they are not impacted by transaction rollbacks.
0
 
LVL 4

Expert Comment

by:szacks
ID: 9898609
Thank you for posting that comment on the usage of table variables. I have never used them before, but it seems like they are worth looking into.

One of the beauties of temporary tables is that the database deals with the clean up for you. You do not have to drop a temporary table, though it is considered good form to do so.
The tempdb is specifically made to handle things like this.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question