Removing query decorations from psql output in PostgreSQL - database

I have an SQL file which extracts data from a PostgreSQL database in a particular format. However, I'm getting query "decoration", specifically a "SELECT N" output when creating my temporary tables.
This is an example SQL file.
create temporary table a as
select 1 as the_code, 'Test'::text as the_name;
create temporary table b as
select 2 as the_code, 'Foo'::text as the_name;
select * from a union all
select * from b;
And this is the example output, produced with psql --tuples-only --no-align --file query.sql:
SELECT 1
SELECT 1
1|Test
2|Foo
Note that this question is separate from How to hide result set decoration in Psql output in that it is in relation to different query "decoration". That is, my question is how to remove the "SELECT N", while the existing question is how to remove the column headers and "(n rows)" footer.
EDIT
As a workaround, I could use common table expressions (CTEs) from which the select is run, but it'd be nice if there was a solution to the above so I don't have to rework my exiting SQL.

Related

Is it possible to return query results as an Excel/CSV file formatted as a BLOB or base64 encoded string?

Limitations on some software I'm using require me to think outside the box, so can you have a query that automatically returns a "file" with the results in it? I imagine it would be as BLOB or a base 64 encoded string or something similar.
Using Microsoft SQL Server
You can use two methods to achieve this
1. Use SQLcmd
SQLCMD -S SERVERNAME -E -Q "SELECT Col1,Col2,Col3 FROM MyDatabase.dbo.MyTable"
-s "," -o "D:\MyData.csv"
Run this above command in a cmd and achieve the expected result.
2. Use OpenRawset
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
SELECT Field1, Field2, Field3 FROM DatabaseName
You need to have the Microsoft.ACE.OLEDB.12.0 provider available. The Jet 4.0 provider will work, too, but it's ancient, so I used this one instead.
limitations:
The .CSV file will have to exist already. If you're using headers (HDR=YES), make sure the first line of the .CSV file is a delimited list of all the fields.
If you can create objects in the target database or another database on the same server, you could use the stored procedures and functions from my answer to this question: SSMS: Automatically save multiple result sets from same SQL script into separate tabs in Excel?
You would just need to exclude the #OutputFileName parameter to force output as a single column, single row varbinary(max).
select * into #systables from sys.tables;
select * into #syscolumns from sys.columns;
select * into #systypes from sys.types;
exec dbo.GetExcelSpreadsheetData
#Worksheets = 'sys.tables/#systables/autofilter|sys.columns/#syscolumns/autofilter|sys.types/#systypes'
drop table #systables;
drop table #syscolumns;
drop table #systypes;
Output (truncated):
ExcelSpreadsheetData
-------------------------------------
0x504B0304140000000800DB78295479400A1

How to arrange or format the table results in Oracle SQL Plus

I started learning Oracle and I've been gone through different types of examples. But from the beginning I have an issue that how can we arrange the retrieve table in well formatted view. I am using SQL Plus.
I attached a image of the way I get the output. I want the output table in a proper table view ( Organized columns and rows ).
SELECT DISTINCT c.name,
i.company.company AS stock_name,
i.company.current_price AS cur_price,
i.company.last_dividend AS last_devident,
i.company.earning_pershare AS eps
FROM client_table_c c, TABLE (c.investments) i
You can do the following to have the output in a single line and in a more formatted way
SET LINESIZE 32000;
SET PAGESIZE 40000;
For more formatting options, see Oracle Documentation
In SQL*Plus, there's the COLUMN command (can be abbreviated to COL) which allows you to format columns. For example:
SQL> create table test
2 (id number,
3 name varchar2(50));
Table created.
SQL> insert into test values (1, 'Littlefoot');
1 row created.
SQL> select * From test;
ID NAME
---------- ---------------------------------------
1 Littlefoot
SQL> col name format a15
SQL> select * From test;
ID NAME
---------- ---------------
1 Littlefoot
SQL>
See? NAME column is now shortened.
For much more info, have a look at Formatting SQL Plus reports (feel free to read the whole SQL Plus document).
P.S. I'm glad you use SQL Plus. It requires you to actually know what you're doing. GUI tools allow you to click here & click there, not really knowing what's behind that click and what result you can expect. However, once you get used to use Oracle, switch to - for example - SQL Developer (a free GUI tool by Oracle) which displays results of your queries in a more appropriate manner, without the need to manually adjust the output in order to make it readable).

SSIS SQL TASK MAX(DATE) to Variable in DATA FLOW

OK this seems like it should be insanely easy, but I cannot figure it out. Every where I look online says to create temp tables and VB scripts and I cannot believe I have to do that. My goal is to insert all the records in a table with a date later than the max date in that destination table.
UPDATE The 2 tables are in two different non linked SQL databases
So:
Select #[User::Dated] = MAX(Dateof) from Table2
Insert into Table2
Select *
From Table1
Where DateOf > #[User::Dated]
I am trying to do this in SSIS. I declared a variable, the SQL execution step looks like it is assigning the single row output to it. But when I got go into the data flow it give me no parameters to choose, when I force the known parameter which is in the project scope it says no parameter exists
Create two OLE DB data sources each pointing at you two databases.
Create a variable called max_date and make its data type String.
Place an Execute SQL Task on the Control Flow, change its connection type to OLE DB and for the connection select the name of the data source that contains Table2. Set the ResultSet to Single Row. Add the following for the SQLStatement:
SELECT CAST(MAX(Dateof) AS VARCHAR) AS max_date FROM Table2
Go to the Result Set pane, click Add and enter the following:
Result Name: max_date
Variable Name: User::max_date
You can now use the max_date variable in an expression to create a SQL statement, for example you could use it in another Execute SQL Task which would use the second Data Connection like so:
"INSERT INTO Table2
SELECT *
FROM Table1
WHERE DateOf > '" + #[User::max_date] + "'"
Or in an OLE DB Source in a data flow like so:
"SELECT *
FROM Table1
WHERE DateOf > '" + #[User::max_date] + "'"
You can do this in a single SQL Task if you want:
Insert into Table2
Select *
From Table1
Where DateOf > (Select MAX(Dateof) from Table2)
If you want to use multiple Execute SQL Task items in the control flow, or want to make use of the parameter in a data flow instead, you have to change the General > Result Set option for your MAX() query to Single Row, then move from General to Result Set and Add a new variable for your result set to occupy.
To use that variable in your INSERT INTO.... query via Execute SQL Task, you'll construct your query with a ? for each parameter and map them in the parameter mapping section. If a variable is used multiple times in a query it's easiest to use a stored procedure, so you can simply pass the relevant parameters in SSIS.

how to import data from other database in SQL Server 2005

I have 2 databases in SQL Server 2005.
I want a functionality that i have same table structure in 2 database for example i have a same table named as testData in 2 database named as dbTest1 and dbTest2.
Now i want a single query through which i can add all the records from table testData of database dbTest2 into table testData of database dbTest1.
I tried to use following query
insert into dbTest1.testData values select * from dbTest2.testData
but this query is not running and giving error.
I also tried
insert into dbTest1.testData(col1,col2,col3) values select * from dbTest2.testData
but this also gives error that "Invalid object name dbTest2.testData"
Could any one help in this
Thanks
Replace dbTest2.testData with dbTest2..testData - you have to specify 3 things (or optionally leave the middle blank for dbo).
i.e.
insert into dbTest1..testData
select * from dbTest2..testData
If the table doesn't already exist in dbTest1, you can do this:
select *
into dbTest1..testData
from dbTest2..testData
You need to specify all column names in query.
insert into dbTest1.dbo.testData(col1,col2,col3) select * from dbTest2.dbo.testData

How to run multiple queries in one instance in SQL Server 2005

I want to run a SELECT statement and I want execute a DELETE statement for the same row and the read result be respond by the SQL Server.
WITH cte AS (
SELECT *
FROM <mytable>
WHERE key = <mykey>)
DELETE cte
OUTPUT deleted.*;
There are many ways to skin this cat. I often preffer the one posted because is very readable. It clearly separates the SELECT into its own query expression, allowing for easily created complex queries. It deletes exactly the query result. It outputs the deleted rows.
The following is also perfectly valid and easier for simple WHERE clauses:
DELETE <mytable>
OUTPUT deleted.*
WHERE key = <mykey>;

Resources