In Sybase SQL, I would like to execute a String containing SQL.
I would expect something like this to work
declare #exec_str char(100)
select #exec_str = "select 1"
execute #exec_str
go
from the documentation of the exec command
execute | exec
is used to execute a stored procedure or an extended stored
procedure (ESP). This keyword is
necessary if there are multiple
statements in the batch.
execute is also used to execute a string containing Transact-SQL.
However my above example gives an error. Am I doing something wrong?
You need bracketing:
execute ( #exec_str )
Related
I am calling HQL from shell script.
I am passing variable to HQL from querying from other table. Variable I am passing as follows:
$$A1=('123','124')
I see variable $$A1 properly in shell script with echo statement and it displayed as ('123','124').
but when I am using this variable in query, its missing single quotes. I mean it is passing as (123,124)
I am passing as $$A1 as follows:
select * from table1 where cd in $$A1
query is taking as select * from table where cd in (123,124)
why single quotes are missing when it is passing to the query.
appreciate any help on this.
Thanks,
Babu
I am trying to write the Stored Procedure for SQL equivalent in Hive. I managed to translate the first two:
DECLARE #ReloadMonths as INT=15
set reloadMonths=15
DECLARE #Anchor_DT as DATE =EOMONTH(Getdate(),-1);
set anchor_dt=select last_day(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd')`)
But I am having troubles translating the following two:
DECLARE #YearMonth as INT=C_II.Common.FN_COM_DATEToYearMonth(#Anchor_DT);
set yearMonth=(anchor_dt,'yyyy-MM')
DECLARE #StartYearMonth as INT =ISNULL(#StartYearMonth_Inp,C_II.Common.FN_COM_DATEToYearMonth(DATEADD(MM,-#ReloadMonths+1,#Anchor_DT)));
set startYearMonth=${hiveconf:${hiveconf:startYearMonth}};
Any ideas or suggestions?
your requirements was not much clear in the question. Also it seems that this function 'C_II.Common.FN_COM_DATEToYearMonth' is specific to your project and it's not a standard sql server function.
Lets breakdown it in step by steps:
If we run below statements in sql server:
DECLARE #Anchor_DT as DATE =EOMONTH(Getdate(),-1);
select #Anchor_DT;
It will give you date as: 2019-06-30
whereas the hive conversion you made for this is incorrect.
select last_day(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'))
It will give you last day of current month as '2019-07-31' so the right and equivalent coversion to sql server would be as:
select DATE_SUB(current_date(),DAY(current_date()));
This will return you date as :'2019-06-30'
The last two statements in your question was not very clear but looks like you are expecting below conversion.
select date_format('${hiveconf:anchor_dt}','yyyy-MM');
It will return as : 2019-06
"DECLARE #StartYearMonth as INT =ISNULL(#StartYearMonth_Inp,C_II.Common.FN_COM_DATEToYearMonth(DATEADD(MM,-#ReloadMonths+1,#Anchor_DT)));"
I have converted above statement in sql server as shown below:
select format((DATEADD(MM,-#ReloadMonths+1,#Anchor_DT)),'yyyy-MM');
This will return date in sql server as : 2018-04
Answer to your question:
create a hive script and save it on your hdfs location.(testdatehive.hql)
select date_format('${hiveconf:anchor_dt}','yyyy-MM');
select date_format(add_months('${hiveconf:anchor_dt}',-${hiveconf:reloadMonths}+1),'yyyy-MM');
Shell script:
#!/bin/bash
#Declare integer variable
declare -i reloadMonths=15
echo $reloadMonths
echo "Executing the hive query - get anchor date and store it in shell variable"
anchor_dt=$(hive -e "select DATE_SUB(current_date(),DAY(current_date()));")
echo $anchor_dt
echo "Pass anchor_date & reloadMonths to hive script"
hive --hiveconf anchor_dt=$anchor_dt --hiveconf reloadMonths=$reloadMonths -f hdfs://hostname/user/vikct001/dev/hadoop/hivescripts/testdatehive.hql
echo "Executing the hive query - ends"
Here is your shell output:
15
Executing the hive query - get anchor date and store it in shell variable
2019-06-30
Pass anchor_date & reloadMonths to hive script
2019-06
2018-04
Let me know if this works.
I'm not sure if I was specific in the question, but I'm having trouble creating a Postgres function that runs a Linux shell command, with one detail: it's a function in a Trigger after insert and I need to use some NEW columns.
While in MySQL, using the plugin "MySQL UDF" it was pretty simple, trigger worked like this:
BEGIN
DECLARE result int(10);
SET result = sys_exec('/usr/bin/php /var/www/html/.../regras.php NEW.uniqueid NEW.linkedid NEW.eventtype');
END
But on PostgreSQL I tried the language PL/sh, wich enables running any shell script, so I wrote the following function:
CREATE FUNCTION tarifador_func2() RETURNS TRIGGER
LANGUAGE plsh
AS $$
#!/bin/sh
/usr/bin/php /var/www/html/...regras.php NEW.uniqueid NEW.linkedid NEW.eventtype
$$;
It does execute the .php file in proper way, the problem is the language does not recognize the NEW variables I'm giving as arguments to the PHP, so in the args[] what I got is "NEW.uniqueid", "NEW.linkedid" and "NEW.eventtype".
So, anyone knows how can I properly use the NEW argument in PL/sh?
Another possible solution might be to manually set the three values I need via the arguments on crating the trigger, but it's not allowed to use NEW in the arguments.
You can access some values in plsh triggers.
UPDATE offers only OLD
INSERT offers only NEW (duh)
DELETE I didn't test
So you get those values using arguments, like $1, $2
You function would look kinda like this:
CREATE FUNCTION tarifador_func2() RETURNS TRIGGER
LANGUAGE plsh
AS $$
#!/bin/sh
/usr/bin/php /var/www/html/...regras.php $3 $6 $1
$$;
Notice that I didn't use $1 $2 $3, that is because plsh extension dumps ALL columns into arguments in order they are declared in your table. So you might do something like INSERT INTO table1 (column3) VALUES (6); and it will be under $3 in plsh, assuming this is third column in table.
As a side note, metadata of trigger is available thru env vars.
As far as I know, you cannot access the NEWand OLD tuple in PL/sh.
I would use PL/Perl or PL/Python for this purpose.
Here is an example in PL/Python:
CREATE OR REPLACE FUNCTION pytrig() RETURNS trigger
LANGUAGE plpythonu AS
$$import os
os.system("/usr/bin/php /home/laurenz/hello.php '" + TD["new"]["val"] + "'")$$;
CREATE TABLE test (id integer PRIMARY KEY, val text);
CREATE TRIGGER pytrig AFTER INSERT ON test FOR EACH ROW
EXECUTE PROCEDURE pytrig();
I am writing a shell script which involves BigQuery commands to query an existing table and save the results to a destination table.
However, since my script will be run periodically, I have a parameter for the date for which the query should run.
For example, my script looks like this:
DATE_FORMATTED=$(date +%Y%m%d)
bq query --destination_table=Desttables.abc_$DATE_FORMATTED "select hits_eventInfo_eventLabel from TABLE_DATE_RANGE([mydata.table_],TIMESTAMP($DATE_FORMATTED),TIMESTAMP($DATE_FORMATTED)) where customDimensions_index = 4"
I get the following error:
Error in query string: Error processing job 'pro-cn:bqjob_r5437894379_1': FROM clause with table wildcards matches no table
How else can I pass the variable $DATE_FORMATTED to the TABLE_DATE_RANGE function from BigQuery in order to help execute my query?
Use double quotes "" + single quote ''. For example, in your case:
TIMESTAMP("'$DATE_FORMATTED'")
OR
select "'$variable'" as dummy from your_table
You are probably missing the single quotes around the $DATE_FORMATTED value inside the TIMESTAMP functions. Without the quotes it's going to be defaulting to the EPOCH time.
Try with:
TIMESTAMP('$DATE_FORMATTED'),TIMESTAMP('$DATE_FORMATTED')
I am creating an SSIS Execute SQL Task that will use variables but it is giving me an error when I try to use it. When I try to run the below, it gives me an error and when I try to build the query, it gives me an error SQL Sytnax Errors encountered and unable to parse query. I am using an OLEDB connection. Am I not able to use variables to specify the tables?
You can't parameterize a table name.
Use the Expressions editor in your Execute SQL Task to Select a SqlStatementSource property.
Try "SELECT * FROM " + #[User::TableName]
After clicking OK twice (to exit the Task editor), you should be able to reopen the editor and find your table name in the SQL statement.
Add a string cast in a case where it might be a simple Object - (DT_WSTR,100)
You are using only single parameter(?) in the query and assigning 3 inputs to that parameters which is not fair put only single input and assign some variable as input as shown in image and change the value of variable respectively.
the parameter name should be incremented by 1 start with 0 because they are the indexes representing the "?" in the query which was written the query window.