Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XYiKz-0003JW-2B for pgsql-sql@arkaria.postgresql.org; Mon, 29 Sep 2014 21:18:29 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XYiKy-00078Z-HR for pgsql-sql@arkaria.postgresql.org; Mon, 29 Sep 2014 21:18:28 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XYiKx-00076F-Ep for pgsql-sql@postgresql.org; Mon, 29 Sep 2014 21:18:27 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XYiKu-0007rc-Cg for pgsql-sql@postgresql.org; Mon, 29 Sep 2014 21:18:25 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1XYiKt-0005MD-Hb for pgsql-sql@postgresql.org; Mon, 29 Sep 2014 14:18:23 -0700 Date: Mon, 29 Sep 2014 14:18:23 -0700 (PDT) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1412025503538-5821013.post@n5.nabble.com> In-Reply-To: <1412025405544-5821012.post@n5.nabble.com> References: <1412018553142-5820980.post@n5.nabble.com> <1412019286556-5820984.post@n5.nabble.com> <1412024156842-5821009.post@n5.nabble.com> <1412025405544-5821012.post@n5.nabble.com> Subject: Re: Why data returned inside parentheses in for loop MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 0.8 (/) 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 David G Johnston wrote > > wujee wrote >> Thanks David for your reply. If the result is being a "record" type, how >> do we getting a list of data as text and input to other query, for >> example I have the following code, how would I go by doing it? >> >> declare >> v_list text; >> begin >> for i in (select emp_id from employees where emp_id in (select >> emp_id from salaries where salary > 3000) loop >> v_list :=''''||i||''','||v_list; >> delete from salaries where salary > 3000; >> delete from employees where emp_id in (v_list); >> end loop; >> end; > Using my example on how to print just the value of salary you should be > able to figure this out. > > That said, your example code is, to put it bluntly, stupid. > > Even if you were to build v_list incrementally like this having the delete > statements inside the loop means you will keep executing them. At minimum > you'd simply build the v_list and execute the delete commands after the > loop has ended. > > However, there is no reason to add a loop here in the first place. The > salaries delete can simply be executed and the employees delete can use > the loop query directly in its where clause. > > I'd also write the for query as: "SELECT DISTINCT emp_id FROM salaries > ..." - though depending on whether salaries-employee is 1-to-1 or > 1-to-many the DISTINCT would be redundant. If it is 1-to-many then > DISTINCT would be needed but I would have to assume you are missing the > part of the where clause that allows you to distinguish between different > salaries for the same employee. > > David J. You may also want to lookup FOREIGN KEY and ON DELETE CASCADE David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Why-data-returned-inside-parentheses-in-for-loop-tp5820980p5821013.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql