adding subquery in insert into

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
Code:
[COLOR=#242729][FONT=Arial]"insert into temp_cost(RecordID,ClassID, Classtype, ClassName, TypeOrder, CreateDate)[/FONT][/COLOR][COLOR=#242729][FONT=Arial]  values('" & strRecordID &[/FONT][/COLOR]"','"& strClassID &"','"[COLOR=#242729][FONT=Arial]& strClassType & "', '" & strClassName & "', '" & strTypeorder & "', '" & strCreateDate & "')"[/FONT][/COLOR]

This code works, but I am trying to add a subquery to auto populate ClassID.

Code:
[COLOR=#242729][FONT=Arial]"insert into temp_cost(RecordID,ClassID, Classtype, ClassName, TypeOrder, CreateDate) select ('" & strRecordID & "', select classid from employee where classcode=&strClassType& , '" & strClassType & "', '" & strClassName & "', '" & strTypeorder & "', '" & strCreateDate & "')"[/FONT][/COLOR]

This is what I've tried and it's not looking up classid from the employee table. How can I make this work?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This is completely untested, but try
Code:
 "insert into temp_cost(RecordID,ClassID, Classtype, ClassName, TypeOrder, CreateDate) select ('" & strRecordID & "', (select classid from employee where classcode='strClassType') , '" & strClassType & "', '" & strClassName & "', '" & strTypeorder & "', '" & strCreateDate & "')"
 
Upvote 0
Thank you for the reply, But I am getting an error message "Incorrect syntax near','."
Is there any ways to make this work?

Thank you for your support!
 
Upvote 0
Yeah silly me. Again untested:
Code:
"insert into temp_cost(RecordID,ClassID, Classtype, ClassName, TypeOrder, CreateDate) values('" & strRecordID & "', (select classid from employee where classcode='strClassType') , '" & strClassType & "', '" & strClassName & "', '" & strTypeorder & "', '" & strCreateDate & "')"
The online syntax checker I use says the syntax is valid but I think it may need some further qualification of the inner select statement.
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top