How to execute sql query files via RPostgreSQL - sql

I am accessing my PostgreSQL database (9.3) via R using the RPostgreSQL package.
I have a few very long and big sql queries (several MB big. generated from raster2pgsql).
How can I send / execute sql query files as statement within R?
The normal way
\i query.sql
does not seem to work via dbSendQuery.
I tried to read in the whole sql file as character vector via readLines, however this also fails, because dbSendQuery only supports a single command apparently?

dbSendQuery or dbGetQuery is just for the "SQL" part, not the psql commands such as \i.
In your case the simplest is indeed to use readLines but then wrap dbGetQuery in a sapply call.
con <- dbConnect(...) #Fill this as usual
queries <- readLines("query.sql")
sapply(queries, function(x) dbGetQuery(con,x))
dbDisconnect(con)
Since I use this very often, I have a shortcut for this in my .Rprofile file:
dbGetQueries<-function(con,queries)sapply(queries,function(x)dbGetQuery(con,x))
Of course, you can also go the system way:
system("psql -U username -d database -h 127.0.0.1 -p 5432 -f query.sql") #Remember to use your actual username, database, host and port

Related

Clickhouse Dictionaries with complex MySQL queries for data source

I want to set a lot of dictionaries in my clickhouse server and some of them aren't just plain MySQL queries to get the existing values, for a few I need to do JOINs and WHERES, and the dictionary configuration in Clickhouse only allows me to tell which MySQL table it will read the data from.
Is it possible to set a custom MySQL query for it?
Other thing that would be helpful is to use ALIASES in the attributes names.. that way I wouldn't be force to use the MySQL column name later.
Thank you.
you can try use external shell script which run
mysql -u<user> -p<password> -h <host> -N -B -e "SELECT field AS field_alias... FROM table1 JOIN table2"
and try read this article
https://www.altinity.com/blog/2017/4/12/dictionaries-explained

How to get output from SQLCMD or OSQL?

Looked everywhere... to no avail.
I am trying to do a basic select using SQLCMD from the command line:
sqlcmd -S myServer -d myDB -E
So far so good.
select * from myTable
Nothing, just goes to the next line. Shouldn't it display a table with values ? Or at least "n row(s) returned" ?
I also tried the -o param: it creates an empty file.
When you use the SQLCMD tool in interactive mode statements that you enter are sent to the server when you use the keyword GO.
GO signals both the end of a batch and the execution of any cached
Transact-SQL statements. When specifying a value for count, the cached
statements will be executed count times, as a single batch.
See Use the sqlcmd Utility specifically the section titled Running Transact-SQL Statements Interactively by Using sqlcmd
So in your case:
select * from myTable enter
GOenter

Inserting UNICODE characters in Sql

I am trying to get unicode strings into an SQL*Plus: Release 10.2.0.2.0 database but am having difficulties. If I use SQLPlus and copy and paste the insert statement into the database, any special characters are inserted as ? or something like that. I then try to call a sql file that has been encoded to UTF-8 and the outcome is the same.
Does anyone know how to get unicode data into database?
Can anyone help . How can i set NLS_LANG option within sqlplus
SQL *Plus is not a database it's a command-line based front-end to a SQL or PL/SQL database. The command-line usually only supports ANSI or ASCI encoded characters. So when you try and paste in the command the program (SQL *Plus) just replaces the text it can't figure out how to encode with "?" marks. You probably need to switch to a different client application to use UTF-8
In my case the problem wasn't in sqlplus but in the environment in that the database was run. After I set
NLS_LANG=RUSSIAN_RUSSIA.UTF8
I could insert unicode characters without any distortion.
Some restarts may be needed for this to take effect (db or OS or both). I run an Oracle XE db in docker so
I had to add -e NLS_LANG=RUSSIAN_RUSSIA.UTF8 to the docker run... command as shown below:
docker run -d --rm -p 49161:1521 --name db -e NLS_LANG=RUSSIAN_RUSSIA.UTF8 -e ORACLE_ALLOW_REMOTE=true wnameless/oracle-xe-11g
Set the NLS_LANG environmental variable to make SQL*Plus understand the character representation you're using.
http://www.sqlsnippets.com/en/topic-13434.html
try to using something around this statement
update pp.pp_employee t
set t.first_name=UNISTR('АБДАЛЛА')
where t.emp_id = 5451 ;

How to indicate in postgreSQL command in which database to execute a script? (simmilar to SQL Server "use" command)

I have the following problem, I need to put in a script that is going to run before the new version is rolled the SQL code that enables the pgAgent in PostgreSQL. However, this code should be run on the maintenance database (postgres) and the database where we run the script file is another one.
I remember that in SQL Server there is a command "use " so you could do something like:
use foo
-- some code
use bar
-- more code
is there something similar in PostgreSQL?
You can put in your file something like:
\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Are you piping these commands through the psql command? If so, \c databasename is what you want.
psql documentation
You can't switch databases in Postgres in this way. You actually have to reconnect to the other database.
PostgreSQL doesn't have the USE command. You would most likely use psql with the --dbname option to accomplish this, --dbname takes the database name as a parameter. See this link for details on the other options you can pass in you will also want to check out the --file option as well. http://www.postgresql.org/docs/9.0/interactive/app-psql.html
well after looking on the web for some time I found this which was what I need it
http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html

db2 sql script file

I have an oracle script that I am trying to convert to valid db2 syntax. Within this sql file I have various calls to other sql files passing in a parameter using the '#' syntax.
e.g.
#script1 param1
#script2 param2
Can anyone help me with valid db2 equivalent statements? Is there an equivalent run command in db2? is it possible to pass parameters to a sql script in db2?
thanks,
smauel
The thing you are after is the DB2 Command Line Processor (CLP).
If you want to execute a script, you would execute in the CLP:
db2 -vtf script1
-f tells the CLP to run command input from the given file.
Here's the full list of options.
Unfortunately db2 doesn't support passing parameters to a script. You would have to combine your db2 -vtf commands with other scripting commands (such as sed) to generate the scripts for you, as in this example.
1) place the filename.sql file in SQLLIB/BIN
2) run db2cmd
3) execute this to connect to the required db
db2 connect to *dbname* user *userid* using *password*
4) excute this command
db2 -vtf *filename.sql*
This should execute the sql statements in the file one by one. The sql statements must be ending with a semicolon
There is an easier way for passing in parameters, that works fine for us (it might not work with (complex) multiline sql statements).
Convert your sql-script into a shell script by adding 'db2 ' at the beginning of each line. Than you can use the standard variable replacement syntax from your shell in your scripts.
so instead of
insert ...
update ...
you will have
db2 insert ...
db2 update ...
Place file in one directory.
Open db2cmd.exe as administrator
Navigate to directory where you have place the script
type db2 -vtf `