473,788 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy select query result to a file

jlm699
314 Contributor
I'm at the end of my wits here and can't possibly see what I'm doing wrong.

I tried doing a "COPY (query) TO '/var/tmp/filename.csv' WITH CSV", which gave me an error. So I tried it w/o the WITH CSV... same error. I tried the query by itself which worked. As soon as I add the parenthesis and the COPY TO stuff I get the following error:

ERROR: syntax error at or near "("
SQL state: 42601
Character: 6

Here's a simple test I devised to make sure it wasn't my actual tables.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE copytest (t1 char, t2 integer, t3 text) WITH OIDS;
  2. INSERT INTO copytest VALUES('a', 5, 'testing');
  3. INSERT INTO copytest VALUES('b', 4, 'this');
  4. INSERT INTO copytest VALUES('c', 3, 'should');
  5. INSERT INTO copytest VALUES('d', 2, 'work');
  6. INSERT INTO copytest VALUES('e', 1, 'right?');
  7. SELECT * FROM copytest;
  8.  
That works just fine... now as soon as I try to do the copy operation using the same exact query it fails. I'm sure this is something very simple that I'm messing up but I can't see where I'm going wrong.
Expand|Select|Wrap|Line Numbers
  1. -- This doesn't work
  2. COPY (SELECT * FROM copytest) TO '/var/tmp/copytest.csv' WITH CSV;
  3. -- This doesn't work either
  4. COPY (SELECT * FROM copytest) TO '/var/tmp/copytest.csv';
  5. -- Atleast this works, but isn't quite what I'm going for
  6. COPY copytest TO '/var/tmp/copytest.csv' WITH CSV;
  7.  
The two that fail above fail with that same syntax error. As far as I can see I'm doing it the same as in the PostgreSQL guide! Can anybody help me?

Regards,

James
Jan 9 '08 #1
2 12693
jlm699
314 Contributor
Whoops! Looks like that ability is new to 8.2 (I'm still running 8.0.13)

Let me update and see what happens
Jan 9 '08 #2
jlm699
314 Contributor
Ok, well being lazy I've omitted updating to 8.2 and just did a little workaround:

Expand|Select|Wrap|Line Numbers
  1. CREATE TEMPORARY TABLE foo_bar AS some_query;
  2. COPY foo_bar TO 'foo_bar.csv' WITH CSV;
  3.  
Works just as well I guess!
Jan 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2749
by: Phil Powell | last post by:
Here is the scope of what I need to do; want: enrollment_year allowed (even if null) all of ica criteria:
8
1462
by: Knighterrant | last post by:
I want to copy elements from one namespace to anothor, how to create the xslt? for example, the source data is: <s:mail xmlns:s="urn:source-namespace"> <s:subject>xxxx</s:subject> <s:from>xxxx</s:from> <s:to>xxxx</s:to> <s:content> <!]>
1
3748
by: Rhanda Zak | last post by:
Hi all, my question is maybe so trivial, that I even could not find it in Google's NG seach, so I annoy you now: I simply want to copy the result of a SELECT query to another table which has the same structure. All I could do until now is showing the result of the query in another form with the query as data source:
7
11641
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
2
1623
by: jerrygarciuh | last post by:
Hi all, The following script is giving me weird problems. I have in this directory an index.php and hurricane.php. If the script gets $i = 'on' it is supposed to back up the current index into a file called normal.php and then copy hurricane.php into index.php. This should create a backup of the index and then put the hurrican alert in place.
3
2682
by: eholz1 | last post by:
Hello PHP programmers. I had a brilliant idea on one of my pages that selects some data from my mysql database. I first set the page up to display some info and an image, just one item, with a row of data, etc. then I thought it would be nice to do a select, and perhaps an update (the title of the image) on the same page.
6
5457
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in it--then hold the next value through the next set of nulls, and so on. See example below: I wanna copy down batch "IMR138" in record ID 1, all the way to ID 10. Then copy down batch "7138" all the way to ID 20 and so on....
10
17952
by: pbd22 | last post by:
Hi. Like the title says - how do i do this? I was given the following example: INSERT INTO TABLE2 SELECT * FROM TABLE1 WHERE COL1 = 'A' The above statement threw the following error:
21
29821
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays prodcut anme have used ajax to avoid refreshing of page this works fine 2.now i have created one row with checkbox|select box|text|text|text|text| where in the select box values are fetched from table here also i have used ajax for getting the m_name...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.