i just want to know if it is possible to declare variables on the DBeaver´s sql editor and use them on a query
You have to enable variable processing in the "SQL Processing" settings of DBeaver -> Window -> Preferences -> Database -> Editors -> SQL Editor -> SQL Processing. There is a block on Parameters with settings you can change. See the Dynamic Parameter binding section on the wiki.
You should then be able to do:
#set date = '2019-10-09'
SELECT ${date}::DATE, ${date}::TIMESTAMP WITHOUT TIME ZONE
which produces:
| date | timestamp |
|------------|---------------------|
| 2019-10-09 | 2019-10-09 00:00:00 |
Yes you can, using :.
An example:
SELECT * FROM "SYSIBM".SYSDUMMY1
WHERE IBMREQD = :YOUR_VARIABLE
Based on the incredibly helpful post from #nicoschl, here are a couple of minor improvements:
-- using declarations
#set datex_start = cast('2120-01-01' as date) as date_start;
-- datex_start is the var name
-- casting the value in the declaration saves us the work later
-- the var can be given a default fieldname (e.g. "date_start")
-- run as a standalone command since the subsequent SELECT statement doesn't return values when it's all run together
select
${datex_start}
;
This will return a value "2120-01-01" with a fieldname of "date_start".
In the DBeaver SQL editor you can type the following:
-- define variable
#set my_var='hello SQL world'
-- call variable
select :my_var
You can also use ${my_var} to reference the variable; $my_var however did not work for me. I am using DBeaver v. 21.1.
You have to enable at Dbeaver settings:
Top Window > Preferences > and then see print below (updated 2022/08).
Related
I want to store current_day - 1 in a variable in Hive. I know there are already previous threads on this topic but the solutions provided there first recommends defining the variable outside hive in a shell environment and then using that variable inside Hive.
Storing result of query in hive variable
I first got the current_Date - 1 using
select date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1);
Then i tried two approaches:
1. set date1 = ( select date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1);
and
2. set hivevar:date1 = ( select date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1);
Both the approaches are throwing an error:
"ParseException line 1:82 cannot recognize input near 'select' 'date_sub' '(' in expression specification"
When I printed (1) in place of yesterday's date the select query is saved in the variable. The (2) approach throws "{hivevar:dt_chk} is undefined
".
I am new to Hive, would appreciate any help. Thanks.
Hive doesn't support a straightforward way to store query result to variables.You have to use the shell option along with hiveconf.
date1 = $(hive -e "set hive.cli.print.header=false; select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);")
hive -hiveconf "date1"="$date1" -f hive_script.hql
Then in your script you can reference the newly created varaible date1
select '${hiveconf:date1}'
After lots of research, this is probably the best way to achieve setting a variable as an output of an SQL:
INSERT OVERWRITE LOCAL DIRECTORY '<home path>/config/date1'
select CONCAT('set hivevar:date1=',date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'),1)) from <some table> limit 1;
source <home path>/config/date1/000000_0;
You will then be able to use ${date1} in your subsequent SQLs.
Here we had to use <some table> limit 1 as hive got a bug in insert overwrite if we don't specify a table name.
I'd like to create a table name in Hive using variable substitution.
E.g.
SET market = "AUS";
create table ${hiveconf:market_cd}_active as ... ;
But it fails. Any idea how it can be achieved?
You should use backtrics (``) for name for that, like:
SET market=AUS;
CREATE TABLE `${hiveconf:market}_active` AS SELECT 1;
DESCRIBE `${hiveconf:market}_active`;
Example run script.sql from beeline:
$ beeline -u jdbc:hive2://localhost:10000/ -n hadoop -f script.sql
Connecting to jdbc:hive2://localhost:10000/
...
0: jdbc:hive2://localhost:10000/> SET market=AUS;
No rows affected (0.057 seconds)
0: jdbc:hive2://localhost:10000/> CREATE TABLE `${hiveconf:market}_active` AS SELECT 1;
...
INFO : Dag name: CREATE TABLE `AUS_active` AS SELECT 1(Stage-1)
...
INFO : OK
No rows affected (12.402 seconds)
0: jdbc:hive2://localhost:10000/> DESCRIBE `${hiveconf:market}_active`;
...
INFO : Executing command(queryId=hive_20190801194250_1a57e6ec-25e7-474d-b31d-24026f171089): DESCRIBE `AUS_active`
...
INFO : OK
+-----------+------------+----------+
| col_name | data_type | comment |
+-----------+------------+----------+
| _c0 | int | |
+-----------+------------+----------+
1 row selected (0.132 seconds)
0: jdbc:hive2://localhost:10000/> Closing: 0: jdbc:hive2://localhost:10000/
Markovitz's criticisms are correct, but do not produce a correct solution. In summary, you can use variable substitution for things like string comparisons, but NOT for things like naming variables and tables. If you know much about language compilers and parsers, you get a sense of why this would be true. You could construct such behavior in a language like Java, but SQL is just too crude.
Running that code produces an error, "cannot recognize input near '$' '{' 'hiveconf' in table name".(I am running Hortonworks, Hive 1.2.1000.2.5.3.0-37).
I spent a couple hours Googling and experimenting with different combinations of punctuation, different tools ranging from command line, Ambari, and DB Visualizer, etc., and I never found any way to construct a table name or a field name with a variable value. I think you're stuck with using variables in places where you need a string literal, like comparisons, but you cannot use them in place of reserved words or existing data structures, if that makes sense. By example:
--works
drop table if exists user_rgksp0.foo;
-- Does NOT work:
set MY_FILE_NAME=user_rgksp0.foo;
--drop table if exists ${hiveconf:MY_FILE_NAME};
-- Works
set REPORT_YEAR=2018;
select count(1) as stationary_event_count, day, zip_code, route_id from aaetl_dms_pub.dms_stationary_events_pub
where part_year = '${hiveconf:REPORT_YEAR}'
-- Does NOT Work:
set MY_VAR_NAME='zip_code'
select count(1) as stationary_event_count, day, '${hiveconf:MY_VAR_NAME}', route_id from aaetl_dms_pub.dms_stationary_events_pub
where part_year = 2018
The qualifies should be removed
You're using the wrong variable name
SET market=AUS; create table ${hiveconf:market}_active as select 1;
The parameterization example in the "SQL Parameters" IPython notebook in the datalab github repo (under datalab/tutorials/BigQuery/) shows how to change the value being tested for in a WHERE clause. Is it possible to use a parameter to change the name of a field being SELECT'd on?
eg:
SELECT COUNT(DISTINCT $a) AS n
FROM [...]
After I received the answer below, here is what I have done (with a dummy table name and field name, obviously):
%%sql --module test01
DEFINE QUERY get_counts
SELECT $a AS a, COUNT(*) AS n
FROM [project_id.dataset_id.table_id]
GROUP BY a
ORDER BY n DESC
table = bq.Table('project_id.dataset_id.table_id')
field = table.schema['field_name']
bq.Query(test01.get_counts,a=field).sql
bq.Query(test01.get_counts,a=field).results()
You can use a field from a Schema object (eg. given a table, get a specific field via table.schema[fieldname]).
Or implement a custom object with a _repr_sql_ method. See: https://github.com/GoogleCloudPlatform/datalab/blob/master/sources/lib/api/gcp/bigquery/_schema.py#L49
I'm trying to query the Oracle v$session table using Groovy (imported groovy.sql.SQL) like this:
sql.eachRow("""
Select
'Y' as runInd
from v$session
where upper(module) = ?
having count(*) > 1
""", [programName]) {row -> etc...}
But Groovy keeps telling me: "Groovy:Apparent variable 'session' was found in a static scope but doesn't refer to a local variable, static field or class."
It apparently doesn't like the table called v$session. I've tried many things, I'm not sure why I can't find out how to do this. Any help is appreciated. Thanks!
Tom
Instead of """ which marks it as a multi-line templated groovy string, try ''' which shouldn't try to template things following a $:
sql.eachRow( '''Select
| 'Y' as runInd
| from v$session
| where upper(module) = ?
| having count(*) > 1'''.stripMargin(), [programName]) { row ->
etc...
}
I am looking for few global variable in mysql which are equivalent of sql server.
I want the whole list
eg.
Sql Server Equivalent
##error ---
##Identity ---
etc.
Basically right now I want to know what are the equivalent variable of ##error and ##identity.
But it will be helpfull if u could provide some other variable also
The last auto_increment value (i.e. the last identity) generated for the current connection can be found using the LAST_INSERT_ID() function.
About errors, not sure ; there doesn't seem to be any system variable that corresponds to the last error message.
There is a show errors statement :
mysql> select a from b;
ERROR 1046 (3D000): No database selected
mysql> show errors;
+-------+------+----------------------+
| Level | Code | Message |
+-------+------+----------------------+
| Error | 1046 | No database selected |
+-------+------+----------------------+
1 row in set (0,00 sec)
But not sure how you can use this result...
After searching a bit more, I found out this thread : Getting the last error message, which says (quoting) :
I think there should be something like
##last_error_id and
##last_error_message, but I can`t
find anything in the current manual.
And the first answer states (quoting) :
This isn't possible currently, from
what we understand error handling will
be improved in version 5.2 and
hopefully something like this may be
possible.
If you are interested in the number of errors (or if there was an error), using ##error_count seems to work.
> select a from bogus_table;
(1054, "Unknown column 'a' in 'field list'")
> select ##error_count;
+---------------+
| ##error_count |
+---------------+
| 1 |
+---------------+
Regarding Errors, there is a facility using which you can get the error number and other things. There is a utility named GET DIAGNOSTICS using which you can query database diagnostics data. Refer the example below -
GET DIAGNOSTICS #NUM = NUMBER, #rowsAffected = ROW_COUNT;
-- here #NUM and #rowsAffected are user variables. Any other variables can work here as well.
In above statement you can get number of error conditions and number of rows affected. For error conditions we refer another variant of this utility which consumes number of error count that we got from above statement.
GET DIAGNOSTICS CONDITION #NUM
#errNo = mysql_errno, -- Error Number
#msg = message_text, -- Error Message Text
#sqlState = sqlstate_returned -- Affected SQL State under which error occured
;
A word of caution though - use these statement as soon as you expect your sql to fail. Since these capture internals database diagnostics any other function can quickly overlap your session data.
For more on this, refer link here