I have the following problem, and SQL isn't my strongest skill yet. I have the following procedure in SQL Server 2014 that works for returning the amount of money in each individual Quote for a Quoting System. Each Quote has Groups, which in turn have Parts in them.:
USE [My_DB_Name]
GO
DECLARE @QuoteNumberID int;
SELECT @QuoteNumberID = QuoteNumberId FROM [Quote].[Quote]
WHILE @QuoteNumberID IS NOT NULL
BEGIN
INSERT INTO [Quote].[ZQuoteBackupGL117] (QuoteAmount)(
SELECT ISNULL(SUM(ExtendedPrice) ,0) AS QuoteTotal
FROM(
SELECT (Quantity * ((UnitPrice - ISNULL(DollarDiscount, 0)) -
ROUND((((ISNULL(PercentDiscount,0)/100 + ISNULL(CustomerPercentDiscount,0)/100))) * UnitPrice, 2))) AS ExtendedPrice
FROM [Quote].[PartGroupPart] p
INNER JOIN [Quote].[QuotePartGroup] g ON p.PartGroupID = g.PartGroupID
INNER JOIN [Quote].[ZQuoteBackupGL117] q ON g.QuoteID = q.QuoteID
WHERE QuoteNumberId = @QuoteNumberID AND g.IsRecommended = 0 AND g.ExcludeFromTotal = 0 AND (q.GrandTotalValue IS NULL OR q.GrandTotalValue = 0)
)tmp)
SELECT @QuoteNumberID = MAX(QuoteNumberId) FROM [Quote].[Quote] WHERE @QuoteNumberId > QuoteNumberID
END
What I'm trying to do is get the value of QuoteTotal and put it in a new field in the [Quote].[ZQuoteBackupGL117] backup table named QuoteAmount(money, allow nulls) in each entry in the table. I will also run it on my production table later. Normally, I would avoid using a loop for this, but this query is only meant to run once to populate QuoteAmount for retroactive Quotes, of which there are thousands. I have looked into using INSERT INTO, but I'm not sure how I am supposed to structure it using this loop. Any help you guys can give me would be appreciated.