Skip to main content
added 248 characters in body
Source Link

Are you trying to insert all of the invoice items for all students with an invoice date of '2019-09-05'? If you use a single query to get all the students you are interested in creating invoice items for, then in the end you would have a total of two insert statements. The one below, then a similar one for the 'Tuition_Service_Fee'. Also, using table joins instead of sub queries is preferred in my exampleopinion - more readable and can be better performing. But definitely, use variables instead of hardcoded values will allow the code to be reused whether you just call the script or create a stored procedure.

declare @invoiceDtm datetime

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = '2019-09-05'@invoiceDtm
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')

Are you trying to insert all of the invoice items for all students with an invoice date of '2019-09-05'? If you use a single query to get all the students you are interested in creating invoice items for, then in the end you would have a total of two insert statements. The one below, then a similar one for the 'Tuition_Service_Fee'. Also, using table joins instead of sub queries in my example.

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = '2019-09-05'
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')

Are you trying to insert all of the invoice items for all students with an invoice date of '2019-09-05'? If you use a single query to get all the students you are interested in creating invoice items for, then in the end you would have a total of two insert statements. The one below, then a similar one for the 'Tuition_Service_Fee'. Also, using table joins instead of sub queries is preferred in my opinion - more readable and can be better performing. But definitely, use variables instead of hardcoded values will allow the code to be reused whether you just call the script or create a stored procedure.

declare @invoiceDtm datetime

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = @invoiceDtm
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')
added 369 characters in body
Source Link

Is this whatAre you aftertrying to insert all of the invoice items for all students with an invoice date of '2019-09-05'? If you use a single query to get all the students you are interested in creating invoice items for, then in the end you would have a total of two insert statements. The one below, then a similar one for the 'Tuition_Service_Fee'. Also, using table joins instead of sub queries in my example.

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = '2019-09-05'
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')

Is this what you after?

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = '2019-09-05'
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')

Are you trying to insert all of the invoice items for all students with an invoice date of '2019-09-05'? If you use a single query to get all the students you are interested in creating invoice items for, then in the end you would have a total of two insert statements. The one below, then a similar one for the 'Tuition_Service_Fee'. Also, using table joins instead of sub queries in my example.

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = '2019-09-05'
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')
Source Link

Is this what you after?

INSERT INTO [dbo].[InvoiceItem] (invoice_item_name, amount, invoice_id)
select "Tuition", Grade.tuition_amount, invoice.invoice_id
from Student
join invoice on student.student_id = invoice.student_id
join grade on student.grade_id = grade.grade_id
where student.invoice_date = '2019-09-05'
--if you really want to specifiy the first names
and student.first_name in ('Murdoch', 'Hartwell')