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)
 help with syntax insert statement

Author  Topic 

mike123
Master Smack Fu Yak Hacker

1462 Posts

Posted - 2006-03-02 : 18:14:50
Hi,

I'm attempting to insert into a table based on a select statement.

The query would be something like this

INSERT INTO tblMail (userID,message) VALUES (1,'message)

However the list of userID's I want to insert is about 1000 records and is derived from the query below

SELECT userID FROM tblinterests WHERE interestID = 989


Can someone refresh me on the syntax? I'm having problems finding / understanding this one


thanks very much once again!

mike123

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-02 : 18:52:30
insert into <tableName> (<columnList>)
select <sameColumnList> from <tableName>



Be One with the Optimizer
TG
Go to Top of Page

pravin14u
Posting Yak Master

246 Posts

Posted - 2006-03-03 : 00:07:53
A cursor can also be used in this case i believe...

declare c cursor for
select userID from tblinterests where interestID=989
open c
fetch next from c into @id

while @@fetch_status=0

begin
insert into table3 values(@id,message);
fetch next from c into @id
end


PLZ CORRECT ME IF I AM WRONG... Thank you
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-03-03 : 00:30:10
Hi,
Use what TG has suggested as it would be an optimum solution as far as performance is concerned..
Go to Top of Page

pravin14u
Posting Yak Master

246 Posts

Posted - 2006-03-03 : 00:43:25
Ok thanks... But wat if the value of the second parameter(message) changes for each insert?
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2006-03-03 : 00:46:08
in this instance, i believe batch processing is more applicable

deal one problem at a time

--------------------
keeping it simple...
Go to Top of Page
   

- Advertisement -