Hive truncate table only when it's not empty - hive

I have a hive job that is scheduled to be executed. At the beginning of the job, I want to truncate a table by doing:
TRUNCATE TABLE SOMETABLE
The problem is that the table may be empty. In that case, I don't want to perform the truncate operation which will raise an exception. I know in MySQL you can do something like:
IF EXISTS(SELECT * FROM SOMETABLE)
BEGIN
TRUNCATE SOMETABLE
END
Is there a way I can achieve something similar in hive? Thanks a lot for your help!

If the table is empty also hive won't raise any exception.
You can also make use of Temporary tables in hive so that these tables only accessible through the session that established and very useful to manage intermediate data.
Once the session got closed then Hive deletes all temporary tables.
Please refer this and this links regarding hive temporary tables.

Related

Oracle is dropping my table before select statement finishes

At work we have a large Oracle SQL query designed to output two select statements based off of analysis and table combining in prior scripts. At the end of these select statements we truncate the temp tables that were created. My issue is that the tables are getting truncated before the select statement has time to run, resulting in 0 output for both queries and empty tables that now need the whole process to be run over again to populate the tables correctly. This is something I'm trying to help automate but I'm stuck on how to get Oracle to wait for the select statement to finish processing before triggering the truncate. Very simply it looks like:
Select * from temp;
Truncate Table temp;
commit;
TRUNCATE is a DDL sentence in Oracle which means it can modify the structure of tables and databases, instead of using TRUNCATE why don't you try to change it for a simple DELETE which is a simple DML sentence in Oracle that just change the data.
What you describe can only ne the case if the 2 select statements (one followed by a truncate) are running in separate sessions. Run in the same session and the problem would be solved although you may lose out on a performance benefit of runnng them in parallel.

oracle creating table from another table created partially ; unable to extend temp space

We are trying to create a table from another table with method -
create table tab1 as select * from tab2;
But the process failed with error
ORA-01652: unable to extend temp segment by 8192 in tablespace
However the table tab1 is created with partial data only. There is a count mismatch in tab1 and tab2. Any of these two tables being not populated/ updated by any transaction. This happened with a couple of tables.
What my knowledge says about it, a create table should create a table at all or not at all. There is no possibility of table being created partially.
Any insight is suggested from experts.
Putting the cause of the error aside (addressed by #Leo in his answer):
I have not found anything specific on transactions for CREATE TABLE ... AS SELECT. Any CREATE TABLE statement is a DDL operation, which in turn are generally non-transactional operations.
This is just a speculation, but I'd say that the table creation did succeed. The instruction you gave is basically a two in one, where the first one is the actual table creation, which does work (and as it is not transactional, it can't be affected by the second one) and the second is a variant of a bulk insert from select (with implicit commits for batches), which breaks at some point.
This is probably not answering your question, but as the operation is apparently two-phase anyway, if you need more transactional approach, you would benefit from splitting the operation into two separate ones:
first:
CREATE TABLE tab1 AS SELECT * FROM tab2 WHERE 1 = 2;
second:
INSERT INTO tab1 SELECT * FROM tab2;
This way if the second part fails, you will not end up with a partial insert. You will still have the table in place though.
Execute the following to determine the filename for the existing tablespace as sysadmin
SELECT * FROM DBA_DATA_FILES;
Then extend the size of the datafile as follows (replace the filename with the one from the previous query):
ALTER DATABASE DATAFILE 'C:\ORACLEXE\ORADATA\XE\SYSTEM.DBF' RESIZE 4096M;
You can first try below command or ask DBA to give the privilege:
grant unlimited tablespace to <schema_name>;

Truncate a table in GBQ

I am trying to truncate an existing table in GBQ but the below command fails when I run it. Is there any specific command or syntax to do that. I looked into GBQ documentation but no luck.
TRUNCATE TABLE [dw_test.test];
While BigQuery didn't used to support anything other than SELECTs, it now does as long as you uncheck "Use Legacy SQL" in the query options. There is no truncation, but you can delete:
DELETE from my_table WHERE 1=1
Note that BigQuery requires the use of WHERE in the DELETE, so if you want to delete everything you need to use a statement that will always be true.
Good news, TRUNCATE TABLE is supported by now: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#truncate_table_statement
TRUNCATE TABLE [[project_name.]dataset_name.]table_name
However, please note that this will not work / is not supported, if a partition filter is required through your table definition.
CREATE OR REPLACE TABLE <dataset>.<table>
AS SELECT * FROM <dataset>.<table> LIMIT 0;
For partitionned tables, assuming you have a day partition on field "created_on", then execute the following :
CREATE OR REPLACE TABLE <dataset>.<table> PARTITION BY created_on
AS SELECT * FROM <dataset>.<table> WHERE created_on = CURRENT_DATE() LIMIT 0;
EDIT (Nov 2020): BigQuery now supports other verbs, check other answers for newer solutions.
BigQuery doesn't support TRUNCATE as part of a query string. The only DDL/DML verb that BQ supports is SELECT.
One option is to run a job with WRITE_TRUNCATE write disposition (link is for the query job parameter, but it's supported on all job types with a destination table). This will truncate all data already in the table and replace it with the results of the job.
If you don't want to replace the contents with other data or start a job, your best option is probably to delete and recreate the table with the same schema.

SQL Server Unable to Open Table or Rename

Hey when I try open table I receive the message
Timeout Expired
Then when I try and rename the table I get
Rename Failed Lock Request Time out Expired
Basically I just want to delete the content of this table but in every step there is something stopping me.
Any Ideas ?
If you just want to empty it, use DELETE FROM TABLENAME or TRUNCATE TABLENAME in SSMS or SQLCMD.
Sounds like some running query/transaction is locking the table. Try using sp_who to see what activity is occuring:
USE master
EXEC sp_who 'active'
Or you can use SQL Server Profiler to see what queries are running against your DB.
SqlACID tel you the best and easiest way to solve your problem.
truncate will work faster and effective, you will not have large archive log files, but after you can't restore your data. it will delete permanently and the flash_got command will not help you.
if truncate does not works too after that you must recreate your table by this command
drop table your_table cascade constraints;
-- Create table
create table your_table (col1 number, col2 varchar2(5)).
if it does not works too after this create another table like it:
create table temp_table2 as
select * from temp_table 1 where 1=2

How to efficiently remove all rows from a table in DB2

I have a table that has something like half a million rows and I'd like to remove all rows.
If I do simple delete from tbl, the transaction log fills up. I don't care about transactions this case, I do not want to rollback in any case. I could delete rows in many transactions, but are there any better ways to this?
How to efficiently remove all rows from a table in DB2? Can I disable the transactions for this command somehow or is there special commands to do this (like truncate in MySQL)?
After I have deleted the rows, I will repopulate the database with similar amount of new data.
It seems that following command works in newer versions of DB2.
TRUNCATE TABLE someschema.sometable IMMEDIATE
To truncate a table in DB2, simply write:
alter table schema.table_name activate not logged initially with empty table
From what I was able to read, this will delete the table content without doing any kind of logging which will go much easier on your server's I/O.