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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Populating a temp table

Author  Topic 

frank2
Starting Member

35 Posts

Posted - 2005-09-13 : 16:20:29
I want to populate a temp table. I have a table MyTable containing 10 records. I need to copy out the five records where ID = 'ME' then change 'ME' to 'SENT' Then append those changed records back to MyTable. So MyTable will end up having a total of 15 records.

I am guessing i have to query out the 5 records into a temp table change them and insert them back.

Can I SELECT INTO a temp table? Ir do the entire task in a single SELECT?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-13 : 16:25:04
INSERT INTO MyTable (Column1, Column2, Column3, Column4, Column5)
SELECT Column1, Column2, Column3, 'SENT', Column5
FROM MyTable
WHERE Column4 = 'ME'

Tara
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-09-13 : 16:27:22
[code]
Create Table #MyTempTable (id varchar(100), a varchar(10))
Insert Into #myTempTable Select 'You', 'Blah1'
Insert Into #myTempTable Select 'You', 'Blah2'
Insert Into #myTempTable Select 'Me', 'Blah3'
Insert Into #myTempTable Select 'You', 'Blah4'
Insert Into #myTempTable Select 'Me', 'Blah5'
Insert Into #myTempTable Select 'Me', 'Blah6'
Insert Into #myTempTable Select 'You', 'Blah7'
Insert Into #myTempTable Select 'You', 'Blah8'
Insert Into #myTempTable Select 'Me', 'Blah9'
Insert Into #myTempTable Select 'Me', 'Blah10'

Select * From #myTempTable

Insert Into #myTempTable
Select Id = 'Sent', a
From #myTempTable
Where id = 'ME'

Select * From #myTempTable

Drop Table #myTempTable
[/code]

Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page

frank2
Starting Member

35 Posts

Posted - 2005-09-13 : 16:30:42
Thanks for the answers. You folks hit the nail on the head. :)
Go to Top of Page
   

- Advertisement -