BEGIN
DECLARE @sName VARCHAR(3)
DECLARE @valu VARCHAR(MAX)
DECLARE @query VARCHAR(MAX)
DECLARE cursor_customer CURSOR FOR
SELECT Code FROM BCWarehouse
OPEN cursor_customer
FETCH NEXT FROM cursor_customer
INTO @sName;
PRINT 'SELECT (
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseSTO
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = ''STO'')
) AS STO,'
SET @query = 'SELECT (
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseSTO
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = ''STO'')
) AS STO,'
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @valu = '(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouse'+@sName+'
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = '''+@sName+''')
) AS '+@sName+','
PRINT @valu
SET @query = @query + @valu
FETCH NEXT FROM cursor_customer -- Fetch next cursor
INTO @sName -- Next into variable
END
PRINT ' StockQty AS stocktotal
, Code
, DefStkUnitCode
FROM dbo.BCITEM'
SET @query = @query + ' StockQty AS stocktotal
, Code
, DefStkUnitCode
FROM dbo.BCITEM'
CLOSE cursor_customer;
DEALLOCATE cursor_customer;
END
PRINT @query
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseAIR
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'AIR')
) AS AIR
โดยวนใส่ค่าที่ได้มาจากการ Select (ด้านล่างนี้)
SELECT Code FROM BCWarehouse WHERE Code NOT IN ('ACC','STR','STO','POS','FMA','RMK')
คำสั่ง SQL Server ที่ออกมาคือ
SELECT (
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseSTO
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'STO')
) AS STO,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseAIR
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'AIR')
) AS AIR,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseBCD
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'BCD')
) AS BCD,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseBCH
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'BCH')
) AS BCH,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseCCK
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'CCK')
) AS CCK,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseKSK
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'KSK')
) AS KSK,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseLK2
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'LK2')
) AS LK2,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseLKK
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'LKK')
) AS LKK,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseLTC
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'LTC')
) AS LTC,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseLTK
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'LTK')
) AS LTK,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseLTL
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'LTL')
) AS LTL,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseLTM
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'LTM')
) AS LTM,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseMMK
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'MMK')
) AS MMK,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseRFM
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'RFM')
) AS RFM,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseTMK
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'TMK')
) AS TMK,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseTUD
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'TUD')
) AS TUD, StockQty AS stocktotal
, Code
, DefStkUnitCode
FROM dbo.BCITEM
คือถ้าเปรียบกับของ php จะใช้ loop while แบบนี้หน่ะครับ แล้วคำสั่ง echo แต่ทีนี้คำสั่งที่ผมต้องการมันต้องเสร็จภายใน Sql Server ครับ
Code (SQL)
SELECT (
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouseSTO
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = 'STO')
) AS STO
======================= LOOP @CODE===================
,(
SELECT Qty
FROM dbo.BCStkWarehouse as BCStkWarehouse@CODE
WHERE (ItemCode = dbo.BCITEM.Code)
AND (UnitCode = dbo.BCITEM.DefStkUnitCode)
AND (WHCode = @CODE)
) AS @CODE
=======================================================
, Code
, DefStkUnitCode
FROM dbo.BCITEM