November 29, 2006 at 9:47 am
Hi everyone
Can anyone offer a quick and easy way to move the result of a select to a temp table ?
This is my select.
select * from XX
where tff_id ='108'
and tap_from_dt >'2006-07-01 00:00:00:000'
Basically want the result from this select moved to a temp table ?
cheers
November 29, 2006 at 9:50 am
Insert into #tempTable (col1, col2, id)
Select col1, col2, id FROM XX
where tff_id ='108'
and tap_from_dt >'2006-07-01 00:00:00:000'
November 29, 2006 at 9:52 am
DECLARE #YourTempTable (colA varchar(20), colB varchar(20), colC varchar(20))
INSERT INTO #YourTempTable
select colA, colB, colC from XX
where tff_id ='108'
and tap_from_dt >'2006-07-01 00:00:00:000'
November 29, 2006 at 9:54 am
DECLARE #YourTempTable .
Never seen that code before .
November 29, 2006 at 10:01 am
Sorry, that's the advanced, SQL Server 2008 syntax.
November 29, 2006 at 10:05 am
Great to know.
Here's the 2000 / 2005 table variable way
DECLARE @YourTempTable table (colA varchar(20), colB varchar(20), colC varchar(20))
INSERT INTO @YourTempTable
select colA, colB, colC from XX
where tff_id ='108'
and tap_from_dt >'2006-07-01 00:00:00:000'
November 29, 2006 at 12:27 pm
If you want all columns of your query into a temp table
select colA, colB, colC
INTO #Temp
FROM XX
WHERE tff_id = '108' and tap_from_dt > '07/01/2006'
November 29, 2006 at 12:47 pm
Yes, but doesn't SELECT...INTO lock the database for the duration of the select? That's why I always create the table first, then insert into it.
November 29, 2006 at 12:59 pm
>>doesn't SELECT...INTO lock the database
Database lock ?
Creation of a table requires locks on the system tables that contain the table/column definitions. Pages in tempdb's sysobjects and syscolumns may be locked during a SELECT INTO operation. The impact of this on any given system depends on many variables.
November 29, 2006 at 1:05 pm
Yup... that's why I hate taking the risk... just create the temp table or temp variable and us the insert select statements we provided... no possible headaches this way .
November 29, 2006 at 1:11 pm
I don't think there is any difference in using INSERT INTO #Temp ( ) SELECT
and SELECT ... INTO #Temp FROM
SELECT ....INTO created the table by itself instead of the user create the table.
November 29, 2006 at 1:17 pm
tempdb.sbo.sysobjects and tempdb.dbo.syscolumns will be locked for the whole time of the insert and no other temp objects will be able to be created. If a high transaction environement, that can quickly escalate to big problems.
November 30, 2006 at 5:19 pm
Another problem with using Select Into is that the column definitions are based on the result set and not the underlying structure. So for example, if you have a varchar(500) column with only 2 characters in each row, the new row will be created with a much smaller lengh. The real problem is that if all values are null, it will try to create the field with a size of 0 causing the query to fail.
A workaround for this is to do the Select Into using criteria that returns 0 rows. This will cause it to look at the underlying table structures to build the new table.
select colA, colB, colC
INTO #Temp
FROM XX
WHERE 1 = 2
INSERT INTO #Temp (colA, colB, colC )
select colA, colB, colC
FROM XX
WHERE tff_id = '108' and tap_from_dt > '07/01/2006'
Another advantage to doing it this way (Where 1 = 2) is that the creation of the new table is virtually instaneous as SQL Server does not wait for the recordset to be returned before completing the table creation.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply