SQL INSERT INTO与SELECT&串

时间:2017-05-04 03:31:31

标签: c# sql

如何组合下面的sql?

2009-11-01 01:30:00

Exp:ContactName = Andy,contact_ID是' 0001'。

所以第二个sql看起来应该是

NOW()

如何将两个sql组合成一个sql?

5 个答案:

答案 0 :(得分:1)

你可以像这样使用Insert into ... select ....from ...

INSERT INTO PurchaseInfo (ContactName)
SELECT 'ContactName ' + ContactName 
FROM Contact WHERE Contact_ID = '0001'

答案 1 :(得分:1)

我假设您需要在列中使用名称的静态值,我只是在@Triv的答案中稍作修改

INSERT INTO PurchaseInfo (ContactName)
SELECT 'ContactName ' + ContactName AS Expr1
FROM Contact WHERE Contact_ID = '0001'

答案 2 :(得分:0)

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

答案 3 :(得分:0)

INSERT INTO PurchaseInfo (ContactName) 
VALUES (CONCAT('Contact Name',SELECT ContactName FROM Contact WHERE Contact_ID = '0001'));

只是更新@TriV答案,我也是NEWBIE给我一个尝试:)

答案 4 :(得分:0)

如果您使用的是SQLServer,请执行此操作:

DECLARE @strContactName varchar(50)
select @strContactName = ContactName  from Contact WHERE Contact_ID = '0001';
INSERT INTO PurchaseInfo (ContactName) VALUES (@strContactName);