CREATE TRIGGER [DBO].[Stock_close] ON [DBO].[ITEM_INWARD] after
UPDATE,
INSERT AS IF EXISTS
(
SELECT Count(*)
FROM stock_close_rpt xx,
inserted yy
WHERE Cast(xx.stock_date AS DATE) = Cast(Getdate() AS DATE)
AND xx.item_code=yy.item_code
)
BEGIN
UPDATE stock_close_rpt
SET item_code=a.item_code ,
opening_stock=
(
SELECT closing_stock =
CASE
WHEN
(
SELECT closing_stock
FROM stock_close_rpt
WHERE Cast(stock_date AS DATE) =
(
SELECT Max(Cast(stock_date AS DATE))
FROM stock_close_rpt c,
inserted d
WHERE c.item_code=d.item_code)) IS NOT NULL THEN closing_stock
ELSE '0'
END
FROM stock_close_rpt),
inward_stock=
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'I'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'I'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)),
outward_stock=
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'D'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'D'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)),
blocked_stock=
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'B'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'B'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)) ,
closing_stock=(
(
SELECT closing_stock =
CASE
WHEN
(
SELECT closing_stock
FROM stock_close_rpt
WHERE Cast(stock_date AS DATE) =
(
SELECT Max(Cast(stock_date AS DATE))
FROM stock_close_rpt c,
inserted d
WHERE c.item_code=d.item_code)) IS NOT NULL THEN closing_stock
ELSE '0'
END
FROM stock_close_rpt)+(
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'I'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'I'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)) -
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'D'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'D'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)))),
colour_id= a.colour_id,
stock_date=a.dt_added,
cp_id=a.cp_id
FROM dbo.item_inward a,
inserted b
WHERE a.item_code=b.item_code
AND Cast(a.dt_added AS DATE)=Cast(b.dt_added AS DATE)
group BY a.dt_added,
a.item_code,
a.colour_id,
a.cp_id ;
END
ELSE
BEGIN
INSERT INTO stock_close_rpt
SELECT a.item_code ,
(
SELECT closing_stock =
CASE
WHEN
(
SELECT closing_stock
FROM stock_close_rpt
WHERE Cast(stock_date AS DATE) =
(
SELECT Max(Cast(stock_date AS DATE))
FROM stock_close_rpt c,
inserted d
WHERE c.item_code=d.item_code)) IS NOT NULL THEN closing_stock
ELSE '0'
END
FROM stock_close_rpt) AS opening_stock,
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'I'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'I'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)) AS inward_stock,
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'D'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'D'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)) AS outward_stock,
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'B'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'B'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)) AS blocked_stock,
(
(
SELECT closing_stock =
CASE
WHEN
(
SELECT closing_stock
FROM stock_close_rpt
WHERE Cast(stock_date AS DATE) =
(
SELECT Max(Cast(stock_date AS DATE))
FROM stock_close_rpt c,
inserted d
WHERE c.item_code=d.item_code)) IS NOT NULL THEN closing_stock
ELSE '0'
END
FROM stock_close_rpt)+(
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'I'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'I'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)) -
(
SELECT status =
CASE
WHEN
(
SELECT Count(ab.status)
FROM item_inward ab,
inserted cd
WHERE ab.status = 'D'
AND Cast(ab.dt_added AS DATE) = Cast(Getdate() AS DATE)
AND ab.item_code=cd.item_code
AND ab.colour_id=cd.colour_id
GROUP BY ab.item_code,
ab.colour_id) IS NOT NULL THEN Count(status)
ELSE '0'
END
FROM item_inward
WHERE status = 'D'
AND Cast(dt_added AS DATE) = Cast(Getdate() AS DATE)))) AS closing_stock,
a.colour_id,
a.cp_id,
a.dt_added
FROM dbo.item_inward a,
inserted b
WHERE a.item_code=b.item_code
AND Cast(a.dt_added AS DATE)=Cast(b.dt_added AS DATE)
GROUP BY a.dt_added,
a.item_code,
a.colour_id,
a.cp_id;
END
這很好。祝你好運。你有問題嗎? – 2014-10-30 14:56:02
'任何幫助讚賞'不是一個問題。 – 2014-10-30 14:56:33
解釋你爲什麼得到樣本結果,並編輯原始表格以便讀取它。有比標題更多的字段 – JotaBe 2014-10-30 14:57:55