1

how to combine below sql?

cNamesql = "SELECT ContactName FROM Contact WHERE Contact_ID = '0001'";
sql = "INSERT INTO PurchaseInfo (ContactName) VALUES ('Contact Name "+ cNamesql + "');

Exp: ContactName = Andy, contact_ID is '0001'.

So the second sql should looks like

INSERT INTO PurchaseInfo (ContactName) VALUES ('Contact Name Andy');

How to combine two sql into one sql?

5 Answers 5

1

You could use Insert into ... select ....from ... like this

INSERT INTO PurchaseInfo (ContactName)
SELECT 'ContactName ' + ContactName 
FROM Contact WHERE Contact_ID = '0001'
Sign up to request clarification or add additional context in comments.

1 Comment

I want the values as "ContactName Andy" instead "Andy"
1

I assume you need to have a static value for name in columns, i just made a slight modification in @Triv 's answer

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

Comments

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

Comments

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

JUST UPDATING @TriV ANSWER, i AM ALSO NEWBIE GIVE A TRY :)

1 Comment

not working, error shows Concat is no recognize as a built in function name.
0

Do this if you are using SQLServer:

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

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.