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 2005 Forums
 Transact-SQL (2005)
 INSERT INTO with SELECT statement

Author  Topic 

token
Posting Yak Master

133 Posts

Posted - 2009-01-29 : 16:00:13
Hello!

I have a very simply question. I want to insert two values into a table, but one of the values comes from a SELECT FROM statement. The other value is just random. I cannot understand how to make this work!

Here is my code so far:
INSERT INTO Auctions (ProductID, Random)
(SELECT ProductID FROM Products), #RandomNumber#

This just won't work! I am doing this insert from a webpage where "#RandomNumber#" is a variable that is generated by the ColdFusion/ASP script.

Any ideas on whats wrong with my syntax?

tonymorell10
Yak Posting Veteran

90 Posts

Posted - 2009-01-29 : 16:05:42
Try

INSERT INTO Auctions (ProductID, Random)
(SELECT ProductID, #RandomNumber# FROM Products)
Go to Top of Page

token
Posting Yak Master

133 Posts

Posted - 2009-01-29 : 18:17:04
Perfecto! Works a treat.

Thank you ever so much for your kind help.
Go to Top of Page
   

- Advertisement -