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)
 Inserting values and select statement together

Author  Topic 

sahu74
Posting Yak Master

100 Posts

Posted - 2002-07-22 : 11:35:45
I need to insert some values like year, collegecode,...etc
I know some of the values and have to use select statements for the others.

How can I insert a row with the values and select statements together.

PKS.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-07-22 : 11:52:58
Here's an example:

CREATE TABLE Things (yearCol int,
collegeCode varchar(3),
FirstName varchar(15),
LastName varchar(20) )
GO
DECLARE @yr int, @code varchar(3)
SELECT @yr=2001, @code='USC' --set values for the variables

INSERT INTO Things (yearCol, collegeCode, FirstName, LastName)
SELECT @yr, @code, FirstName, LastName
FROM Students
WHERE College='USC' AND Year(GraduationDate)=2001


The parameters are simply included in the SELECT list, and they correspond to the yearCol and collegeCode columns indicated in the INSERT list.

Go to Top of Page
   

- Advertisement -