Experts i have 2 queries in an IF block written below
declare @count int, @sql nvarchar(1000)
declare @poNumber varchar(20)
if(select sum(quantity_size.quantity) from carton inner join (select po_detail_id from po_detail, po_header where po_header.po_header_id = po_detail.po_header_id and po_header.po_number=@poNumber)PO_INFO
on PO_INFO.po_detail_id=carton.po_detail_id inner join quantity_size on carton.carton_id = quantity_size.carton_id
and carton.attribute_status_id not in (1,3,4)) >0
begin
select distinct
po_header.po_number as PONumber,
vendor.name as Vendor,
po_status.description as POStatus,
po_detail.style as Style,
dbo.get_inventory_units(po_detail.po_detail_id) as inventoryunits,
dbo.get_staged_units(po_detail.po_detail_id) as StagedUnits,
dbo.get_shipped_units(po_detail.po_detail_id) as ShippedUnits,
'' as OtherUnits,
po_detail.po_detail_id as Id,
po_detail.cost as Cost,
po_detail.description as Description,
vendor_quality.description as Quality,
gender.description as Gender,
label.description as Label,
category.description as Category from po_detail,
po_header,
po_status,
vendor,
vendor_quality,
label,
category,
gender
where po_detail.po_header_id = po_header.po_header_id
and po_header.po_status_id = po_status.po_status_id
and po_header.vendor_id = vendor.vendor_id
and po_detail.gender_id = gender.gender_id
and po_detail.category_id = category.category_id
and po_detail.vendor_quality_id = vendor_quality.vendor_quality_id
and po_detail.label_id = label.label_id and po_header.po_number = @poNumber
UNION ALL
SECOND QUERY
select distinct
po_header.po_number as PONumber,
vendor.name as Vendor,
po_status.description as POStatus,
po_detail.style as Style,
'' as inventoryunits,
'' as StagedUnits,
'' as ShippedUnits,
dbo.get_other_inventory_units(po_detail.po_detail_id) as OtherUnits,
po_detail.po_detail_id as Id,
'' as Cost,
po_detail.description as Description,
vendor_quality.description as Quality,
gender.description as Gender,
label.description as Label,
category.description as Category from po_detail,
po_header,
po_status,
vendor,
vendor_quality,
label,
category,
gender
where po_detail.po_header_id = po_header.po_header_id
and po_header.po_status_id = po_status.po_status_id
and po_header.vendor_id = vendor.vendor_id
and po_detail.gender_id = gender.gender_id
and po_detail.category_id = category.category_id
and po_detail.vendor_quality_id = vendor_quality.vendor_quality_id
and po_detail.label_id = label.label_id and po_header.po_number = @poNumber
order by vendor_quality.description, label.description, category.description, po_detail.description
end
else
begin
select distinct
po_header.po_number as PONumber,
vendor.name as Vendor,
po_status.description as POStatus,
po_detail.style as Style,
dbo.get_inventory_units(po_detail.po_detail_id) as inventoryunits,
dbo.get_staged_units(po_detail.po_detail_id) as StagedUnits,
dbo.get_shipped_units(po_detail.po_detail_id) as ShippedUnits,
'' as OtherUnits,
po_detail.po_detail_id as Id,
po_detail.cost as Cost,
po_detail.description as Description,
vendor_quality.description as Quality,
gender.description as Gender,
label.description as Label,
category.description as Category from po_detail,
po_header,
po_status,
vendor,
vendor_quality,
label,
category,
gender
where po_detail.po_header_id = po_header.po_header_id
and po_header.po_status_id = po_status.po_status_id
and po_header.vendor_id = vendor.vendor_id
and po_detail.gender_id = gender.gender_id
and po_detail.category_id = category.category_id
and po_detail.vendor_quality_id = vendor_quality.vendor_quality_id
and po_detail.label_id = label.label_id and po_header.po_number = @poNumber
end
The way i want the output is:
If the IF block returns true then the results from 1st query should be joined with the results from 2nd query.
The results from 2nd query have a non-zero and non-null column named "OtherUnits". And for what ever PO_Detail_ID this column is non-zero only that row should be added as an extra row to the results of first query.
Please let me know if i can explain it in a better way. I tried union but it gives me 10 + 10 rows where as i need something like for example if first query return 10 rows and second one returns only 3 then i need 13 rows back in total.
I'd use a temp table and two inserts. You'll need to fill in the types of the columns you're selecting after the DECLARE table statement.
Open in new window