NathanGriffiths
Programmer
I have created the following stored procedure but when executed it fails on the update statement indicated due to the LastOrderValue and LastPaymentMethod columns not being present in tmp_OrderDates, apparently because the ALTER TABLE statement has not worked - I am correct in this interpretation and can anyone point out where I'm going wrong?
thanks,
Nathan
CREATE PROCEDURE spTransactions_2 AS
UPDATE IndSummary SET orderfreq_25_36 = null,
lastordervalue = null,
lastorderdate = null,
firstorderdate = null,
lastpaymethod = null
IF EXISTS( SELECT * FROM Information_Schema.Tables WHERE table_name = 'tmp_OrderDates' )
BEGIN
DROP TABLE tmp_OrderDates
SELECT
UIN,
Brand,
MAX(OrderDate) AS MaxOrderDate,
MIN(OrderDate) AS MinOrderDate
INTO tmp_OrderDates
FROM Orders
GROUP BY UIN, Brand
ORDER BY UIN, Brand
END
ALTER TABLE tmp_OrderDates
ADD
LastOrderValue SMALLMONEY,
LastPaymentMethod VARCHAR(15)
-- The following statement fails !
UPDATE t
SET t.LastOrderValue =
rderValue, t.LastPaymentMethod = o.PaymentMethod
FROM tmp_OrderDates t
JOIN Orders o ON t.UIN = o.UIN AND t.Brand = o.Brand AND t.MaxOrderDate =
rderDate
CREATE INDEX idx_tmp_OrderDates_UINBrand ON tmp_OrderDates(UIN, Brand)
UPDATE i
SET i.LastOrderDate = t.MaxOrderDate,
i.FirstOrderDate = t.MinOrderDate,
i.LastOrderValue = t.LastOrderValue,
i.LastPayMethod = t.LastPaymentMethod
FROM IndSummary i
JOIN tmp_OrderDates t ON i.UIN = t.UIN AND i.Brand = t.Brand
GO
thanks,
Nathan
CREATE PROCEDURE spTransactions_2 AS
UPDATE IndSummary SET orderfreq_25_36 = null,
lastordervalue = null,
lastorderdate = null,
firstorderdate = null,
lastpaymethod = null
IF EXISTS( SELECT * FROM Information_Schema.Tables WHERE table_name = 'tmp_OrderDates' )
BEGIN
DROP TABLE tmp_OrderDates
SELECT
UIN,
Brand,
MAX(OrderDate) AS MaxOrderDate,
MIN(OrderDate) AS MinOrderDate
INTO tmp_OrderDates
FROM Orders
GROUP BY UIN, Brand
ORDER BY UIN, Brand
END
ALTER TABLE tmp_OrderDates
ADD
LastOrderValue SMALLMONEY,
LastPaymentMethod VARCHAR(15)
-- The following statement fails !
UPDATE t
SET t.LastOrderValue =
FROM tmp_OrderDates t
JOIN Orders o ON t.UIN = o.UIN AND t.Brand = o.Brand AND t.MaxOrderDate =
CREATE INDEX idx_tmp_OrderDates_UINBrand ON tmp_OrderDates(UIN, Brand)
UPDATE i
SET i.LastOrderDate = t.MaxOrderDate,
i.FirstOrderDate = t.MinOrderDate,
i.LastOrderValue = t.LastOrderValue,
i.LastPayMethod = t.LastPaymentMethod
FROM IndSummary i
JOIN tmp_OrderDates t ON i.UIN = t.UIN AND i.Brand = t.Brand
GO