Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Temp Tables

Author  Topic 

vijayshankar2003
Starting Member

4 Posts

Posted - 2012-08-18 : 17:18:20
Hello friends, i just want to know why do we use temp table to insert values first and then later move those values to the main table. While inserting, we do delete or discard the temp tables.

Happiness is the best habit to cultivate. Do not serach for it, its there beside you.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-18 : 17:28:49
its not a necessity but it really depends on your scenario. In some cases you would require some intermediate processing before you get data in required format as per your destination table thats when you make use Temp tables. The choice of temp tables against table variable for doing the processing depends on lots of other factors like size of dataset, scope to which you want to persist the resultset etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-08-19 : 01:40:01
Generally, if no indexes are required and the dataset if ~ < 75000 rows I'll consider Table variables. Otherwise I'll use temp tables if temporary storage and manipulation is required.

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

vijayshankar2003
Starting Member

4 Posts

Posted - 2012-08-19 : 05:02:45

Thanks for ur reply guys.. since am new to the world of sequel programming, can u brief on that intermediate proceesing you mentioned. For example, i just want to out the values on the click of a button. But my guidelines suggest me to first save the values in a temp table and later move the values into main table and out the values from it. Why not instead directly out the value from main table. Is their any protocol or coding guidelines followed for this? Hope am clear to you.



Happiness is the best habit to cultivate. Do not serach for it, its there beside you.
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-08-19 : 06:37:16
Which guidelines do you mean? From a SQL Serve perspective, temporary tables were designed for storage and manipulation of temporal data. They are instantiated in TempDB - and are not permanent tables. SQL Server Books Online , has some excellent usage guidelines

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-19 : 10:19:52
quote:
Originally posted by vijayshankar2003


Thanks for ur reply guys.. since am new to the world of sequel programming, can u brief on that intermediate proceesing you mentioned. For example, i just want to out the values on the click of a button. But my guidelines suggest me to first save the values in a temp table and later move the values into main table and out the values from it. Why not instead directly out the value from main table. Is their any protocol or coding guidelines followed for this? Hope am clear to you.



Happiness is the best habit to cultivate. Do not serach for it, its there beside you.


if its just retrieving the data from table without any intermediate processing then you can use main table itself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-08-20 : 01:35:59
Are you able to show the SQL Code you're running?

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

vijayshankar2003
Starting Member

4 Posts

Posted - 2012-08-26 : 13:58:36
I shall try to explain my scenario clearly.. On the click of a button, the selected values should get saved in a table and later when i try to maintain that same value, it should get updated and the new one should get saved .... So what i do now is, i first insert those values in a temp table and then i move it to main table. While moving it to main table, i delete the temp table with the respective guid created for the record..
hope am clear to you...!

Vijay Shankar
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-26 : 14:31:39
quote:
Originally posted by vijayshankar2003

I shall try to explain my scenario clearly.. On the click of a button, the selected values should get saved in a table and later when i try to maintain that same value, it should get updated and the new one should get saved .... So what i do now is, i first insert those values in a temp table and then i move it to main table. While moving it to main table, i delete the temp table with the respective guid created for the record..
hope am clear to you...!

Vijay Shankar


If there are no intermediate processing and its straight saving of values on click of button, there's no need for usage of temporary tables. You can directly do inserts on main table itself. But if there's some intermediate processing like Aggregation, some row level processing etc then temp table is required to hold intermediate results

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -