5

I need to insert values into a table. But my condition is that I need to select Min(date) from another table and this value should be inserted into another table.

My query

Insert into tempTable values
('Value1','Value2','Value3',(select min(val_dt) from anotherTable),'Y',getdate())

If I use this query I am facing error.

Guide me how to use select query inside the insert query.

1 Answer 1

7

Instead of using VALUES() in the INSERT statement, use a SELECT to add the row values:

INSERT INTO tempTable
SELECT 'Value1', 'Value2', 'Value3', MIN(val_dt), 'Y', GETDATE()
FROM anotherTable

And the SELECT statement can be as convoluted as you want, meaning WHEREs and the like can be included.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.