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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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