Error running hive script with parameters in parenthesis - hive

I have the code below saved in a file called tableCreate.hql. I'm running it from the command line with
hive -f tableCreate.hql
I'm trying to use parameters but I keep getting error messages if there are parenthesis around the parameter like the example below. I'm wondering do I need to add quotes. I'm new to using parameters in hive scripts.
original code:
set hive.exec.compress.output=false;
set hive.mapred.mode=nonstrict;
set hivevar:MIN_DT=20170401;
set hivevar:MAX_DT=20170301;
DROP TABLE IF EXISTS table1;
CREATE TABLE table1 AS
SELECT *
FROM table t
WHERE dt<=${MAX_DT} AND dt>=(${MIN_DT})
error: unexpected symbol near ${MIN_DT}
modified code:
set hive.exec.compress.output=false;
set hive.mapred.mode=nonstrict;
set hivevar:MIN_DT=20170401;
set hivevar:MAX_DT=20170301;
DROP TABLE IF EXISTS table1;
CREATE TABLE table1 AS
SELECT *
FROM table t
WHERE dt<=${MAX_DT} AND dt>=('${MIN_DT}')

You should refer to the namespace for variable substitution which is hivevar in this case.
WHERE dt<=${hivevar:MAX_DT} AND dt>=${hivevar:MIN_DT}

Related

Check if column exists then alter column from the table?

I want to write sql script that should check if column exists in the table, and then remove the column if previous statement is true. The database I use is Sybase ASE, and this is the code that I tried to use:
IF EXISTS (SELECT 1 FROM syscolumns WHERE id = object_id('users') AND name = 'maiden_name')
BEGIN
ALTER TABLE security DROP maiden_name
END
The code above executed successfully first time I run it. The second time I goth the error:
Invalid column name 'maiden_name'
If column does not exist the ALTER TABLE block of code shouldn't run. Is there a way to achieve this is Sybase? Thank you.
You can use dynamic SQL:
IF EXISTS (SELECT 1 FROM syscolumns WHERE id = object_id('users') AND name = 'maiden_name')
BEGIN
EXEC('ALTER TABLE security DROP maiden_name')
END;
The problem is that the parser is trying to parse the ALTER during the compilation phase, and it gets an error if the column does not exist.

Drop table only if it exists, or ignore drop error

I have a table MYLOG and would like to try drop it before creating it using the SQL script below.
If the table does not exist yet, the error below is throw.
How could I bypass this error if the table does not exist?
The schema gets set in an earlier script, which is not available in the SQL script:
set current schema MYSCHEMA
SQL script:
DROP TABLE MYLOG;
CREATE TABLE MYLOG (
TIME_STARTED TIMESTAMP NOT NULL,
USER_EMAIL VARCHAR(254) NOT NULL,
CONSTRAINT PK_TIME_STARTED_USER_EMAIL PRIMARY KEY (TIME_STARTED, USER_EMAIL)) ORGANIZE BY ROW;
COMMIT;
Error:
DROP TABLE MYLOG
SQLError: rc = 0 (SQL_SUCCESS)
SQLGetDiagRec: SQLState : S0002
fNativeError : -204
szErrorMsg : [IBM][CLI Driver][DB2/6000] SQL0204N "MYSCHEMA.MYLOG" is an undefined name. SQLSTATE=42704
This is a FAQ
There's more than one way to do it.
You can use compound-SQL in your script with a continue-handler for the SQLSTATE corresponding to the error you get if the table is not found, but this requires that you also use an alternative statement delimiter like shown below
--#SET TERMINATOR #
set current schema myschema#
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
BEGIN end;
EXECUTE IMMEDIATE 'DROP TABLE MYLOG';
END #
CREATE TABLE MYLOG(... )#
You can also change the abort-on-first-error logic (if you use +s when running your script via the command line). You can udate the Db2 CLP options on the fly inside your script via update command options using s off (to continue on error) or update command options using s on to abort on error.
by using this query
select tabname from syscat.tables where
tabschema='myschema' and tabname='MYLOG'
check that table in your schema
if exist then
drop table myschema.MYLOG
then create

Sybase - How To Output Variable Contents To File

I'm writing a procedure in Sybase using Interactive SQL. The proc contains several SELECT statements that store the results in variables, eg
DROP VARIABLE IF EXISTS #totalRows;
CREATE VARIABLE #totalRows LONG VARCHAR;
SELECT COUNT(*) INTO #totalRows FROM <MyTable>;
I'd like to be able to output the results of this query to a CSV file but I get an error when trying to run the following statement
DROP VARIABLE IF EXISTS #totalRows;
CREATE VARIABLE #totalRows LONG VARCHAR;
SELECT COUNT(*) INTO #totalRows FROM <MyTable>;
OUTPUT TO 'C:\\temp\\TEST.CSV' FORMAT ASCII DELIMITED BY ';' QUOTE '' WITH COLUMN NAMES;
The error reads
Could not execute statement.
Syntax error near 'OUTPUT' on line 4.
SQLCODE=-131, ODBC 3 State="42000".
Line 1, column 1
If I remove the OUTPUT TO section of the query it runs fine. Is it possible in Sybase to write the contents of a variable to an external file in this manner?
Seems like, 'OUTPUT' clause is not supported by Sybase.
As a workaround, you may run this query using some text-based tool (like sqlite) and redirect (>) the output into file, if you happen to use linux box at your client end.
Or, you may add ODBC data source (which will require sybase ODBC-driver) corresponding to your DB in Windows and use MS Excel embedded tool Microsoft Query (Data -> From other sources -> From Microsoft Query) in order to export your query result directly into excel datasheet, which you may save as CSV.
OUTPUT TO is a dbisql command, i.e. a directive for the dbisql client utility. It is not a SQL statement. If you try to execute this with anything other than dbisql, you'll get an error.
BTW -- I believe the OUTPUT clause must follow the semicolon that terminates the SELECT stmt, i.e. not have a line break in between.
Need add select variable before output statement
DROP VARIABLE IF EXISTS #totalRows;
CREATE VARIABLE #totalRows LONG VARCHAR;
SELECT COUNT(*) INTO #totalRows FROM <MyTable>;
SELECT #totalRows; --select variable before output
OUTPUT TO 'C:\\temp\\TEST.CSV' FORMAT ASCII DELIMITED BY ';' QUOTE '' WITH COLUMN NAMES;

Deleting the Global Temp table and then creating the Global temp table with same name is giving ambiguity error

Below is the SQL statement for Deleting the global temp table and creating it again.
if object_ID('tempdb..##TempTable123') is not null
begin drop table tempdb..##TempTable123 end
GO
Select * into ##TempTable123 from ##TempTable1
After executing the Select statement, I am getting the following Error
"The reference to temp table name '##TempTable123' is ambiguous and cannot be resolved. Use either '##TempTable123' or '##TempTable123'."
Try to add tempdb.. to the select:
Select * into tempdb..##TempTable123 from tempdb..##TempTable1

Getting Error with ##TempTable

I want to use ##TempTable to get value from oracle db in my sql Server query, but always get error, the error says
##TempTable is Invalid object name '##TempTable '.
here's the code where I use ##TempTable
SET #sQuery = 'SELECT * INTO ##TempTable
FROM OPENQUERY(ITCP,''SELECT * FROM DB.WINFO WHERE SCH_DATE = '''''+
CONVERT(VARCHAR(10),#DDATE,121) +''''''' ) A' EXEC(#sQuery)
I confused, Should I Declare Query to create the ##TempTable like create Table like usual or by that query the temp table should automatically created? because I always getting error
I think the issue is your temp table is being created in one scope, and your dynamic query is being executed in another.
A few things to try:
Create the ##temptable first, then populate it in your dynamic query.
Try prefixing the table name with tempdb.. (so tempdb..##temptable).
Use a persistent table (not a temp table).
Hope this helps.