Jump to content

Ms Sql Server Technology Discussions


9Pardhu

Recommended Posts

[quote name='ghazni' timestamp='1371584486' post='1303867361']

thats good part.......... data analysis chesi niku ela kavalo ala rayadamey code..... btw pm check chesuko
[/quote]

Naku kudaa PM pettu adi

Link to comment
Share on other sites

  • 2 weeks later...

[b] Temporary Tables[/b]
[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
The simple answer is yes you can. Let look at a simple CREATE TABLE statement:[/size][/font][/color]
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
You'll notice I prefixed the table with a pound sign (#). This tells SQL Server that this table is a local temporary table. This table is only visible to this session of SQL Server. When I close this session, the table will be automatically dropped. You can treat this table just like any other table with a few exceptions. The only real major one is that you can't have foreign key constraints on a temporary table. The others are covered in [url="http://msdn2.microsoft.com/en-us/library/ms130214.aspx"]Books Online[/url].[/size][/font][/color][color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
Temporary tables are created in [b]tempdb[/b]. If you run this query:[/size][/font][/color]
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )

select name
from tempdb..sysobjects
where name like '#yak%'

drop table #yaks[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
You'll get something like this:[/size][/font][/color]
name
------------------------------------------------------------------------------------
#Yaks_________________________ . . . ___________________________________00000000001D

(1 row(s) affected)[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
except that I took about fifty underscores out to make it readable. SQL Server stores the object with a some type of unique number appended on the end of the name. It does all this for you automatically. You just have to refer to #Yaks in your code.[/size][/font][/color][color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
If two different users both create a #Yaks table each will have their own copy of it. The exact same code will run properly on both connections. Any temporary table created inside a stored procedure is automatically dropped when the stored procedure finishes executing. If stored procedure A creates a temporary table and calls stored procedure B, then B will be able to use the temporary table that A created. It's generally considered good coding practice to explicitly drop every temporary table you create. If you are running scripts through SQL Server Management Studio or Query Analyzer the temporary tables are kept until you explicitly drop them or until you close the session.[/size][/font][/color][color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
Now let's get back to your question. The best way to use a temporary table is to create it and then fill it with data. This goes something like this:[/size][/font][/color]
CREATE TABLE #TibetanYaks(
YakID int,
YakName char(30) )

INSERT INTO #TibetanYaks (YakID, YakName)
SELECT YakID, YakName
FROM dbo.Yaks
WHERE YakType = 'Tibetan'

-- Do some stuff with the table

drop table #TibetanYaks[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
Obviously, this DBA knows their yaks as they're selecting the famed Tibetan yaks, the Cadillac of yaks. Temporary tables are usually pretty quick. Since you are creating and deleting them on the fly, they are usually only cached in memory.[/size][/font][/color]
[b] Table Variables[/b]
[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
If you are using SQL Server 2000 or higher, you can take advantage of the new TABLE variable type. These are similar to temporary tables except with more flexibility and they always stay in memory. The code above using a table variable might look like this:[/size][/font][/color]
DECLARE @TibetanYaks TABLE (
YakID int,
YakName char(30) )

INSERT INTO @TibetanYaks (YakID, YakName)
SELECT YakID, YakName
FROM dbo.Yaks
WHERE YakType = 'Tibetan'

-- Do some stuff with the table [color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
Table variables don't need to be dropped when you are done with them.[/size][/font][/color]
[b] Which to Use[/b]
[list]
[*]If you have less than 100 rows generally use a table variable. Otherwise use a temporary table. This is because SQL Server won't create statistics on table variables.
[*]If you need to create indexes on it then you must use a temporary table.
[*]When using temporary tables always create them and create any indexes and then use them. This will help reduce recompilations. The impact of this is reduced starting in SQL Server 2005 but it's still a good idea.
[/list]
[b] Answering the Question[/b]
[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
And all this brings us back to your question. The final answer to your question might look something like this:[/size][/font][/color]
DECLARE @TibetanYaks TABLE (
YakID int,
YakName char(30) )

INSERT INTO @TibetanYaks (YakID, YakName)
SELECT YakID, YakName
FROM dbo.Yaks
WHERE YakType = 'Tibetan'

UPDATE @TibetanYaks
SET YakName = UPPER(YakName)

SELECT *
FROM @TibetanYaks
[b] Global Temporary Tables[/b]
[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]
You can also create global temporary tables. These are named with two pound signs. For example, [b]##YakHerders[/b] is a global temporary table. Global temporary tables are visible to all SQL Server connections. When you create one of these, all the users can see it. These are rarely used in SQL Server.[/size][/font][/color]
[color=#000000][font=Verdana, Helvetica, Arial, sans-serif][size=3]- See more at: http://www.sqlteam.com/article/temporary-tables#sthash.D1BJLuqK.dpuf[/size][/font][/color]

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 months later...
  • 5 months later...
  • 3 months later...
  • 2 weeks later...

thats good part.......... data analysis chesi niku ela kavalo ala rayadamey code..... btw pm check chesuko

naaku forward chesuko 

Link to comment
Share on other sites

×
×
  • Create New...