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)
 creating an insert statement that uses an input va

Author  Topic 

pras2007
Posting Yak Master

216 Posts

Posted - 2009-04-28 : 21:22:19
I want to create an insert statement that uses an input value from a previous record set.

Example: --1st query statement

Select MAX(Date_Entered)
FROM Sample

Whatever the Max value for the Date_Entered field, it should be in my insert value

Example: --2nd query statement

INSERT INTO Table
(column1, column2)
VALUES
(value1, [Date_Entered]);

How can I store the value of "date_entered" field from the previous record set and use that value in my insert statement?

Please advice. Thanks.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-04-28 : 21:27:54
[code]
-- declare a variable
declare @Date_Entered datetime

Select @Date_Entered = MAX(Date_Entered)
FROM Sample

INSERT INTO Table (column1, column2)
VALUES (value1, @Date_Entered)


or Combined in single statement, example
INSERT INTO Table (column1, column2)
SELECT <some constant value or variable here>, MAX(Date_Entered)
FROM Sample
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

pras2007
Posting Yak Master

216 Posts

Posted - 2009-04-28 : 21:41:50
Thanks for the quick response khtan!
Go to Top of Page
   

- Advertisement -