Create Stored Procedure which inserts values from multipule queries into table

I'm not as skilled with SQL as i would like to be, I need to insert the values from multipule queries into a new table.  I've done similar inserts with a single query but i'm just not getting this.  

I've included the code i have so far.

-- Drop this procedure if it already exist
IF object_id('SP_Create_AdHoc_Totals_Table') IS NOT NULL
      BEGIN
            DROP PROCEDURE SP_Create_AdHoc_Totals_Table
      END
GO

CREATE PROCEDURE SP_Create_AdHoc_Report_Table @GrantID INT = NULL
AS
      
-- Drop this table if it already exist
IF object_id('tbl_AdHoc_Totals') IS NOT NULL
      BEGIN
            DROP TABLE tbl_AdHoc_Totals
      END
GO

CREATE TABLE tbl_AdHoc_Totals
(
GrantID int,
OriginalTotal decimal NULL,
AdjustmentTotal decimal NULL,
ReimbursedTotal decimal NULL
)

INSERT tbl_AdHoc_Totals
      @GrantID,
      
      (SELECT SUM(lineItems.Total) AS OriginalTotal
      FROM LineItems
      WHERE GrantID = @GrantID),

      (SELECT SUM(Adjustment) AS AdjustmentTotal
      FROM lineitems LEFT JOIN lopapprove_adjustment_by_lineitemid ON lineitems.lineitemid = lopapprove_adjustment_by_lineitemid.lineitemid
      WHERE LineItems.GrantID = @GrantID),

      (SELECT SUM(Reimbursed) AS ReimbursedTotal
      FROM lineitems LEFT JOIN reimbursed_by_lineitemid ON lineitems.lineitemid = reimbursed_by_lineitemid.lineitemid
      WHERE LineItems.GrantID = @GrantID)

SELECT *
FROM tbl_AdHoc_Totals

GO
LVL 5
R_HosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NazermohideeenCommented:
Modify the Insert statement as

INSERT tbl_AdHoc_Totals
Select
@GrantID,

(SELECT SUM(lineItems.Total) AS OriginalTotal
     FROM LineItems
     WHERE GrantID = @GrantID),

(SELECT SUM(Adjustment) AS AdjustmentTotal
     FROM lineitems LEFT JOIN lopapprove_adjustment_by_lineitemid
     ON lineitems.lineitemid = opapprove_adjustment_by_lineitemid.lineitemid
     WHERE LineItems.GrantID = @GrantID),

(SELECT SUM(Reimbursed) AS ReimbursedTotal
     FROM lineitems LEFT JOIN reimbursed_by_lineitemid
     ON lineitems.lineitemid = reimbursed_by_lineitemid.lineitemid
     WHERE LineItems.GrantID = @GrantID)


NM
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aneesh RetnakaranDatabase AdministratorCommented:
IF object_id('SP_Create_AdHoc_Totals_Table') IS NOT NULL
     BEGIN
          DROP PROCEDURE SP_Create_AdHoc_Totals_Table
     END
GO

CREATE PROCEDURE SP_Create_AdHoc_Report_Table @GrantID INT = NULL
AS
     

DECLARE  @tbl_AdHoc_Totals  TABLE(          --- replaced Table with table variables
GrantID int,
OriginalTotal decimal NULL,
AdjustmentTotal decimal NULL,
ReimbursedTotal decimal NULL
)

INSERT INTO @tbl_AdHoc_Totals               -- insert into -Into was missing earlier
SELECT    @GrantID,

    (SELECT SUM(lineItems.Total) FROM LineItems
    WHERE GrantID = @GrantID),

    (SELECT SUM(Adjustment)
    FROM lineitems LEFT JOIN lopapprove_adjustment_by_lineitemid ON lineitems.lineitemid = lopapprove_adjustment_by_lineitemid.lineitemid
    WHERE LineItems.GrantID = @GrantID),

    (SELECT SUM(Reimbursed) AS ReimbursedTotal
    FROM lineitems LEFT JOIN reimbursed_by_lineitemid ON lineitems.lineitemid = reimbursed_by_lineitemid.lineitemid
    WHERE LineItems.GrantID = @GrantID)

SELECT *
FROM @tbl_AdHoc_Totals

GO


--If only these much code is present theb you dont need the insert statements





CREATE PROCEDURE SP_Create_AdHoc_Report_Table @GrantID INT = NULL
AS

SELECT    @GrantID AS GrantID,

    (SELECT SUM(lineItems.Total) FROM LineItems
    WHERE GrantID = @GrantID)AS OriginalTotal,

    (SELECT SUM(Adjustment)
    FROM lineitems LEFT JOIN lopapprove_adjustment_by_lineitemid ON lineitems.lineitemid = lopapprove_adjustment_by_lineitemid.lineitemid
    WHERE LineItems.GrantID = @GrantID)AS AdjustmentTotal,

    (SELECT SUM(Reimbursed) AS ReimbursedTotal
    FROM lineitems LEFT JOIN reimbursed_by_lineitemid ON lineitems.lineitemid = reimbursed_by_lineitemid.lineitemid
    WHERE LineItems.GrantID = @GrantID)ReimbursedTotal

GO
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.