如何在SQL Server 2005中的插入查询中使用选择查询

时间:2022-12-09 23:33:32

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.

我需要在表中插入值。但我的条件是我需要从另一个表中选择Min(date),并且该值应该插入到另一个表中。

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.

指导我如何在插入查询中使用select查询。

1 个解决方案

#1


7  

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

而不是在INSERT语句中使用VALUES(),而是使用SELECT来添加行值:

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.

SELECT语句可以根据需要进行复杂处理,这意味着可以包含WHERE等。

#1


7  

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

而不是在INSERT语句中使用VALUES(),而是使用SELECT来添加行值:

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.

SELECT语句可以根据需要进行复杂处理,这意味着可以包含WHERE等。