sql problem_ sp
table definition
BillID
Date_
SupplierCode
BillType
TypeCode
Total
Currency
ROE
payment_or_bill
AmountType
AmountFC
PoNo
AmountFC
MultiplicityID
BillID_Detailed
Reference
sample data in the table
61809779-be45-41a2-be9f-6d296dd740bc | 9/8/2008 12:00:00 AM | FR1 | Cheque | CUSTOM | 66.00 | MUR | 96.00 | 0 | VAT | 996221.00 | CA101 | 996221.00 | 5774c555-7785-45a6-828e-03c8973f68a1 | 7156f061-e587-43bf-b5d2-355543e5cee7 | 754 |
ec44e842-6ec4-4620-8f3f-6671e6a3a500 | 9/9/2009 12:00:00 AM | FR1 | Cheque | CUSTOM | 51111.00 | MUR | 44.00 | 0 | DUTY | 454.00 | CA101 | 454.00 | 89cab236-5ea4-4d71-8c88-058b3b46bd9e | 3f0741af-3a5f-4561-98eb-e6f0b544a976 | 4444 |
ec44e842-6ec4-4620-8f3f-6671e6a3a500 | 9/9/2009 12:00:00 AM | FR1 | Cheque | CUSTOM | 51111.00 | MUR | 44.00 | 0 | AMOUNT | 84.00 | CA101 | 84.00 | e3b9e71d-097d-47c8-a180-5f0a8d3b264e | 52021fb8-e98b-497d-b360-15030955c692 | 4444 |
61809779-be45-41a2-be9f-6d296dd740bc | 9/8/2008 12:00:00 AM | FR1 | Cheque | CUSTOM | 66.00 | MUR | 96.00 | 0 | DUTY | 58.00 | CA101 | 58.00 | 93e74e27-7943-4bdf-9fca-66084c2ded4e | cbfe7c24-dba8-4dd6-92b4-d00e07507ef3 | 754 |
ec44e842-6ec4-4620-8f3f-6671e6a3a500 | 9/9/2009 12:00:00 AM | FR1 | Cheque | CUSTOM | 51111.00 | MUR | 44.00 | 0 | test | 454.00 | CA101 | 454.00 | 57aa2fa5-6304-4219-826e-6e15c2f824ee | 0cc4f1d7-fc08-4b0c-838f-f51abd30956f | 4444 |
| 61809779-be45-41a2-be9f-6d296dd740bc | 9/8/2008 12:00:00 AM | FR1 | Cheque | CUSTOM | 66.00 | MUR | 96.00 | 0 | AMOUNT | 21.00 | CA101 | 21.00 | c91e4faa-99e0-4d3d-a990-98a4ff063192 | e6a374f0-66d7-4cf7-a223-bfd62fdb27aa | 754 |
ec44e842-6ec4-4620-8f3f-6671e6a3a500 | 9/9/2009 12:00:00 AM | FR1 | Cheque | CUSTOM | 51111.00 | MUR | 44.00 | 0 | tes1 | 9652.00 | CA101 | 9652.00 | 8dff0708-4fe5-4340-9596-aaedc40a3815 | 793a9acb-276e-4e99-b5a1-150ac32bda69 | 4444 |
ec44e842-6ec4-4620-8f3f-6671e6a3a500 | 9/9/2009 12:00:00 AM | FR1 | Cheque | CUSTOM | 51111.00 | MUR | 44.00 | 0 | VAT | 844.00 | CA101 | 844.00 | 5a4b442e-25b1-46a3-a747-e79050108283 | f38fbd02-c8b0-4fec-bb69-0fdd853a23ff | 4444 |
stored procedure
set
ANSI_NULLSONset
QUOTED_IDENTIFIERONgo
ALTER
PROCEDURE [dbo].[StP_Suppliers_on_PO]@Pono
asnvarchar(25),@payment_or_bill
nvarchar(2)AS
BEGIN
SET
NOCOUNTON;declare
@stmtvarchar(4000)DECLARE
@fieldnamevarchar(10)DECLARE
FieldsCURSORFORSELECT TypesFROM
dbo.Bill_Typesset
@stmt='Select distinct pono, supplier.suppliercode, supplier.Suppliername 'OPEN
FieldsFETCH NextFROM
FieldsINTO @fieldnameWHILE
@@Fetch_Status= 0BEGIN
SET
@stmt= @stmt+',(select Total frombill_ where BillType = '''
+ @fieldname+''''SET
@stmt= @stmt+' and bill_.BillType = brE.BillType 'SET
@stmt= @stmt+' and bill_.typecode = brE.typecode 'SET
@stmt= @stmt+' and bill_.reference = brE.reference 'set
@stmt= @stmt+' ) As '+ @fieldnameFETCH
NextFROM
FieldsINTO
@fieldnameENDCLOSE
FieldsDEALLOCATE
FieldsSET
@stmt= @stmt+' From Bills_Expanded as brE,supplier'SET
@stmt= @stmt+' where pono = '''+ @pono+''''SET
@stmt= @stmt+' and payment_or_bill = '+ @payment_or_bill+''SET
@stmt= @stmt+' and supplier.suppliercode = brE.suppliercode'SET
@stmt= @stmt+' order by suppliercode'exec
(@stmt)select
(@stmt)END
output
Select distinct pono, supplier.suppliercode, supplier.Suppliername ,(select Total from bill_ where BillType = 'Cheque' and bill_.BillType = brE.BillType and bill_.typecode = brE.typecode and bill_.reference = brE.reference ) As Cheque,(select Total from bill_ where BillType = 'Estimated' and bill_.BillType = brE.BillType and bill_.typecode = brE.typecode and bill_.reference = brE.reference ) As Estimated,(select Total from bill_ where BillType = 'Final' and bill_.BillType = brE.BillType and bill_.typecode = brE.typecode and bill_.reference = brE.reference ) As Final,(select Total from bill_ where BillType = 'Proforma' and bill_.BillType = brE.BillType and bill_.typecode = brE.typecode and bill_.reference = brE.reference ) As Proforma From Bills_Expanded as brE,supplier where pono = 'ca101' and payment_or_bill = 0 and supplier.suppliercode = brE.suppliercode order by suppliercode
pono suppliercode suppliername cheque estimated final proforma
CA101 FR1 Roger NULL NULL NULL 55.00
CA101 FR1 Roger 66.00 NULL NULL NULL
CA101 FR1 Roger 51111.00 NULL NULL NULL
CA101 FR2 Khune & Nagel 448.00 NULL NULL NULL
CA101 SP2 C2323 NULL NULL 44.00 NULL
CA101 SP4 Test NULL NULL NULL 4554.00
i need to get each amount type that is created by cursor in a column
note that the first three rows is a dilema.. there are 2 cheques so i need to be able to send all on one row, that is
ca101 fr1 roger 55.00 66.00 51111.00 null null
im not sure how to do this.

