Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E7044635AE6 for ; Wed, 10 Mar 2010 11:27:03 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 02187-02-6 for ; Wed, 10 Mar 2010 15:26:42 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay.pcl-ipout01.plus.net (relay.pcl-ipout01.plus.net [212.159.7.99]) by mail.postgresql.org (Postfix) with ESMTP id EF822635979 for ; Wed, 10 Mar 2010 11:25:13 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvsEAF5Il0vUOFPl/2dsb2JhbACaU3O9NYR5BA Received: from plesk-mail01.plus.net ([212.56.83.229]) by relay.pcl-ipout01.plus.net with ESMTP; 10 Mar 2010 15:25:11 +0000 Received: (qmail 28699 invoked from network); 10 Mar 2010 15:25:11 +0000 Received: from host86-142-60-221.range86-142.btcentralplus.com (HELO osiris.mauzo.dyndns.org) (86.142.60.221) by plesk-mail01.plus.net with SMTP; 10 Mar 2010 15:25:10 +0000 Received: (qmail 38088 invoked by uid 1001); 10 Mar 2010 15:25:10 -0000 Date: Wed, 10 Mar 2010 15:25:10 +0000 From: Ben Morrow To: dennis@teltel.com, pgsql-sql@postgresql.org Subject: Re: Help : insert a bytea data into new table Message-ID: <20100310152510.GA37991@osiris.mauzo.dyndns.org> References: <20100309034626.GA12083@osiris.mauzo.dyndns.org> <20100309111410.GA15752@osiris.mauzo.dyndns.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Newsgroups: pgsql.sql Organization: Who, me? User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 201003/54 X-Sequence-Number: 33761 Quoth dennis : > Hi Ben > > here is my function , it's for fix missing chunk problem. > It has same problem ,please take look > > > thank for you help > > -------------table---------------------- > > > db=# \d usersessiontable; > Table "public.usersessiontable" > Column | Type | Modifiers > -----------+------------------------+----------- > serverid | character varying(100) | > sessionid | character varying(50) | > data | bytea | > Indexes: > "usersessiontable_idx" btree (sessionid) > db=# > > db=# \d usersessiontable_test; > Table "public.usersessiontable" > Column | Type | Modifiers > -----------+------------------------+----------- > serverid | character varying(100) | > sessionid | character varying(50) | > data | bytea | > > ------------------function-------------------------------- > > > CREATE OR REPLACE FUNCTION check_missing_chunk_table_usersessiontable() > RETURNS integer AS > $BODY$ > declare > begin > records = 0; > OPEN curs1 FOR EXECUTE 'SELECT * FROM usersessiontable ORDER BY > sessionid'; > loop > FETCH curs1 INTO rowvar; > IF NOT FOUND THEN > EXIT; > END IF; > begin > a_sql = 'insert into > usersessiontable_test(sessionid,serverid,data) > values('''||rowvar.sessionid||''','''||rowvar.serverid||''',E'''||rowvar.data||''')';<<--my You are trying to concatenate ''',E''' (of type text) and rowvar.data (of type bytea). This is where the error is coming from. (This actually works in 8.4, so I presume you're using an earlier version?) In any case, this is not a safe way to interpolate into an SQL string: you need the quote_literal function. a_sql = 'insert into usersessiontable (sessionid, serverid, data) ' || 'values (' || quote_literal(rowvar.sessionid) || ', ' || quote_literal(rowvar.serverid) || ', ' || quote_literal(rowvar.data) || ')'; (Is there a function which will do %-interpolation the way RAISE does? It would be much clearer in cases like this.) Ben