Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id C91271810A00 for ; Fri, 2 Mar 2012 07:49:53 -0400 (AST) Received: from swh44.wesh.co.uk ([85.92.78.115]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S3Qzd-00014C-PW for pgsql-sql@postgresql.org; Fri, 02 Mar 2012 11:49:52 +0000 Received: from cpc2-king5-0-0-cust1226.perr.cable.virginmedia.com ([82.36.220.203] helo=zbox.pedal.me.uk) by swh44.wesh.co.uk with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1S3QzM-0002vX-Ja; Fri, 02 Mar 2012 11:49:32 +0000 Received: from [192.168.104.180] (unknown [77.107.188.229]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by zbox.pedal.me.uk (Postfix) with ESMTPSA id CBB3919809B9; Fri, 2 Mar 2012 11:49:35 +0000 (GMT) Message-ID: <4F50B3CB.9030604@pedal.me.uk> Date: Fri, 02 Mar 2012 11:49:31 +0000 From: Philip Couling User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 To: Rehan Saleem CC: "pgsql-sql@postgresql.org" Subject: Re: How To Create Temporary Table inside a function References: <1330687383.77274.YahooMailNeo@web121602.mail.ne1.yahoo.com> In-Reply-To: <1330687383.77274.YahooMailNeo@web121602.mail.ne1.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - swh44.wesh.co.uk X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - pedal.me.uk X-Source: X-Source-Args: X-Source-Dir: X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201203/17 X-Sequence-Number: 36412 Hi Rehan I suggest attempting to drop the table before you create the temp table: DROP TABLE IF EXISTS table1; See: http://www.postgresql.org/docs/current/static/sql-droptable.html Also if you're using an actual TEMP table, PostgreSQL can automatically drop the table or just empty it once the transaction is committed: CREATE TEMP TABLE foo (columns...) ON COMMIT DROP; CREATE TEMP TABLE foo (columns...) ON COMMIT DELETE ROWS; See: http://www.postgresql.org/docs/current/static/sql-createtable.html As for filling the table, you have two options, INSERT ... SELECT ... Which is just the same syntax as you've used previously for MS SQL or create the table with data already in it it: CREATE TEMP TABLE foo ON COMMIT DROP AS SELECT ... See: http://www.postgresql.org/docs/current/static/sql-createtableas.html Hope this helps. On 02/03/2012 11:23, Rehan Saleem wrote: > hi everyone , > how can i create temp table say table1 with three column of types > varchar , int and int, inside the function body and if that same table > already exist it should drop that already existing table(table1) , and > on every run this process should run. and how can i insert values from > postgresql view say vwfirst to this table(table1) > > Here is the MS-SQL code for this process but i want to know how can > this be done in postgresql > > if object_id('tempdb..#tblTFSites1') is not null > drop table tempdb..#tblTFSites1 > > create table #tblTFSites1( > Chr varchar(50) NULL, > Start int NULL, > [End] int NULL > ) > > INSERTION PROCESS > > INSERT INTO #tblTFSites1 (Chr, Start,[End]) > Select Chr_U, Start_U, End_U from vwChrCompareSites where KBId=@ID and > UserDataDetailId=@UserDataDetailId > and bpOverlap >= @bpOverlap and (CentreDistance <= @CentreDistance or > @CentreDistance=1) > >