Stored Procedure for insert

69 views Asked by At

I am taking a datatable as parameter in my stored procedure and I am trying to insert a few of its column in one of the database tables but I don't know the correct syntax.

This is what I have tried

ALTER PROCEDURE [dbo].[spInsertInvoice]
    @tblInvoice [TypeExcel] READONLY
AS
BEGIN
    SET NOCOUNT ON;

    INSERT into Invoice(InvoiceNo, InvoiceDate, CustomerName,[Subject], Reference)
    VALUES (SELECT Template, Invoice_No, InvoiceDate, Cust_Name,[Subject], Reference FROM @tblInvoice)
END

Please guide me how can I correctly insert values in my table.

1

There are 1 answers

3
Richard On BEST ANSWER

You need the form of insert that takes a select rather than a values:

insert into theTable (col1, col2, ...)
select col1, col2, ...
from ...
where...

All the features of a select are available (column aliases, grouping, joins, sub queries, ...).