INSERT INTO Company_Item_Company_List (
Company_id, Company_item_id,
client_id, last_modified_timestamp, last_modified_user_id )
SELECT dcsl.distribution_center_id, sisl.Company_item_id,
@current_client_id:int, getdate(), @current_user_id:int
FROM Company_Item_Company_List AS sisl WITH (NOLOCK)
JOIN Distribution_Center_Company_List AS dcsl WITH (NOLOCK)
ON dcsl.Company_id = sisl.Company_id
JOIN Company AS s WITH (NOLOCK)
ON s.Company_id = sisl.Company_id
AND s.distribution_center_flag = 'n'
AND s.cdm_owner_id = s.client_id
JOIN Company_Item_Base AS sib WITH (NOLOCK)
ON sib.Company_item_id = sisl.Company_item_id
WHERE sisl.Company_item_id = @Company_item_id:int
AND s.Company_id = @Company_id:int
AND sib.exception_status_code = 'n'
AND NOT EXISTS (SELECT 1
FROM Company_Item_Company_List AS sisl2 WITH (NOLOCK)
WHERE sisl2.Company_item_id = sisl.Company_item_id
AND sisl2.Company_id = dcsl.distribution_center_id)
AND EXISTS ( SELECT 1
FROM Company_packaged_item spi
WHERE spi.Company_id = @Company_id:int
AND spi.Company_item_id = sisl.Company_item_id
AND spi.cdm_owner_id = @current_client_id:int )
|