Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZMpmp-00055u-3Q for pgsql-sql@arkaria.postgresql.org; Wed, 05 Aug 2015 03:54:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZMpmo-0002hA-EN for pgsql-sql@arkaria.postgresql.org; Wed, 05 Aug 2015 03:54:38 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZMpml-0002fC-0W for pgsql-sql@postgresql.org; Wed, 05 Aug 2015 03:54:35 +0000 Received: from mail-ob0-x22f.google.com ([2607:f8b0:4003:c01::22f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZMpmi-00037B-7b for pgsql-sql@postgresql.org; Wed, 05 Aug 2015 03:54:33 +0000 Received: by obnw1 with SMTP id w1so22618578obn.3 for ; Tue, 04 Aug 2015 20:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=to:from:subject:message-id:date:user-agent:mime-version :content-type:content-transfer-encoding; bh=FK6AIP5XFcLLWwpNuIs2syGXa1dssrMKwxXXt9dC1IM=; b=d8wpSzil77dEr/TB1sqIENrGM++0w3+m0Fntedj5D8OpTK2+BoU30GhaRGqqeM5x4b QOiPLNz1aWR9QE3Z3g7dL2q3+HMXMhZ9hceqXEZH1jyk1yEghKrm4FSfvwTjJ6tq4pIf MsGxC4FWmFxZzef+xpbivmIo9Byq0ipsWE0kVykHrFO75uquHEdvZ8Z88E7aFGyqcrEu nPZZsXaXdfSTr/DLnHPkV/DtLT3g1wizhrFRvWD2dQDNo4HpRJGyQin5wPBKnYfgFRvD t6B/sH7bYyKo7Pi+y+2kVTKDM+Dycu4cQniieVf5El+C3KHyZE0n08Rc+RQpvJK4geuk BenA== X-Received: by 10.182.240.135 with SMTP id wa7mr6493445obc.63.1438746870834; Tue, 04 Aug 2015 20:54:30 -0700 (PDT) Received: from [10.0.1.180] ([173.218.155.21]) by smtp.googlemail.com with ESMTPSA id e207sm982218oig.15.2015.08.04.20.54.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Aug 2015 20:54:30 -0700 (PDT) To: pgsql-sql@postgresql.org From: Jason Aleski Subject: Stored Procedure to return resultset from multiple delete statements. Message-ID: <55C188F5.7020904@gmail.com> Date: Tue, 4 Aug 2015 22:54:29 -0500 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I have a function that will purge an item out of our inventory system. This script works and displays the appropriate information in the messages/notices pane. I would like it to return the notices in a resultset format because only developers have access to the messages/notices pane. I would like to display the results as a resultset in my application. The problem is that I'm issuing multiple delete commands which cannot be joined. I tried creating a temp table at the top of the procedure, then inserted data (rows affected) into the table; but I could not get that method to work. Can anyone point me in a direction on what to look at to get the "table affected" and the "number of rows affected by the delete" into some sort of result set? Below is my current procedure that is working. There are actually 3 more tables that need to be purged, but I removed those for now. CREATE OR REPLACE FUNCTION purgeInventoryItemByCode (IN t text) RETURNS void AS $BODY$ DECLARE RCprice_history int; RCinventory_transaction_log int; RCitem_code int; BEGIN --Purging price history of item. DELETE FROM price_history WHERE item_id IN (SELECT row_id FROM item WHERE item_code = $1); IF found THEN GET DIAGNOSTICS RCprice_history = ROW_COUNT; RAISE NOTICE 'DELETE % row(s) FROM price_history', RCprice_history; END IF; --Purging item from inventory transaction log. DELETE FROM inventory_transaction_log WHERE item_id IN (SELECT row_id FROM item WHERE item_code = $1); IF found THEN GET DIAGNOSTICS RCinventory_transaction_log = ROW_COUNT; RAISE NOTICE 'DELETE % row(s) FROM inventory_transaction_log', RCinventory_transaction_log; END IF; --Purging item from Master Items table DELETE FROM items WHERE item_code = $1; IF found THEN GET DIAGNOSTICS RCitem_code = ROW_COUNT; RAISE NOTICE 'DELETE % row(s) FROM items', RCitem_code; END IF; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100 -- Jason Aleski / IT Specialist -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql