Dropping partitions in Hive - hive

Hi All the table is partitioned on column 1 and column 2 both being INT types,I am using the following command to drop the partition,column1 is equal to null or HIVE_DEFAULT_PARTITION
ALTER TABLE Table_Name DROP IF EXISTS PARTITION(column1=__HIVE_DEFAULT_PARTITION__,column2=101);
but i am getting the following error :missing \' at ',' near ''
when i do:
show partitions Table_Name (this is how the partition to be dropped looks)
Output:
column1=__HIVE_DEFAULT_PARTITION__/column2=101

Thanks for your answers guys appreciate it ..actually figured out the problem
ALTER TABLE Table_Name DROP IF EXISTS PARTITION(column1<1,column2=101);
as column1 had a null value entry which i wanted to remove which was HIVE_DEFAULT_PARTITION or (null) so using conditions <1 worked for me

Have you tried putting HIVE_DEFAULT_PARTITION in quotes
ALTER TABLE Table_Name DROP IF EXISTS
PARTITION(column1='__HIVE_DEFAULT_PARTITION__',column2=101);

You can directly drop the partition on column2.
ALTER TABLE Table_Name DROP IF EXISTS PARTITION(column2=101);

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 #temp vs drop myTable if it's not null

So far i was using
IF OBJECT_ID(''tempdb..#tempTable'') IS NOT NULL -- dropping the temp table
DROP TABLE #tempTable
Is there a way in which I could use the same statement for a table which is not a temp one?
Tried like this and it didn't work:
IF OBJECT_ID(''myOwnDb.dbo.myTable'') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
Strings in MS SQL server should be enclosed in single quotes.
So neither OBJECT_ID(''myOwnDb.dbo.myTable'') nor OBJECT_ID("myOwnDb.dbo.myTable") will work.
But OBJECT_ID('myOwnDb.dbo.myTable') will work perfectly.
In addition to what other users have suggested wrt Object_ID which is fine, you can explore below method to detect if table exist or not using INFORMATION_SCHEMA
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = N'Your Table Name')
BEGIN
Drop table <tablename>
END
The reason it did not work is because you have the extra quotes instead of single quotes.
i.e. You should be doing this:
IF OBJECT_ID('myOwnDb.dbo.myTable') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
However, note that when you actually drop the table. You aren't even referencing the database. So you can just do:
IF OBJECT_ID('dbo.myTable') IS NOT NULL -- dropping the table
DROP TABLE dbo.myTable
Unless you are calling this command from another database. Then you will need to include the database name in the DROP TABLE command as well.

Hive Alter table change Column Name

I am trying to rename a columnName in Hive. Is there a way to rename column name in Hive .
tableA (column1 ,_c1,_c2)
to
tableA(column1,column2,column3)
??
Change Column Name/Type/Position/Comment:
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
Example:
CREATE TABLE test_change (a int, b int, c int);
// will change column a's name to a1
ALTER TABLE test_change CHANGE a a1 INT;
Command works only if "use" -command has been first used to define the database where working in. Table column renaming syntax using DATABASE.TABLE throws error and does not work. Version: HIVE 0.12.
EXAMPLE:
hive> ALTER TABLE databasename.tablename CHANGE old_column_name new_column_name;
MismatchedTokenException(49!=90)
at org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken(BaseRecognizer.java:617)
at org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115)
at org.apache.hadoop.hive.ql.parse.HiveParser.alterStatementSuffixExchangePartition(HiveParser.java:11492)
...
hive> use databasename;
hive> ALTER TABLE tablename CHANGE old_column_name new_column_name;
OK
alter table table_name change old_col_name new_col_name new_col_type;
Here is the example
hive> alter table test change userVisit userVisit2 STRING;
OK
Time taken: 0.26 seconds
hive> describe test;
OK
uservisit2 string
category string
uuid string
Time taken: 0.213 seconds, Fetched: 3 row(s)
In the comments #libjack mentioned a point which is really important. I would like to illustrate more into it. First, we can check what are the columns of our table by describe <table_name>; command.
there is a double-column called _c1 and such columns are created by the hive itself when we moving data from one table to another. To address these columns we need to write it inside backticks
`_c1`
Finally, the ALTER command will be,
ALTER TABLE <table_namr> CHANGE `<system_genarated_column_name>` <new_column_name> <data_type>;

Best way to create a temp table with same columns and type as a permanent table

I need to create a temp table with same columns and type as a permanent table. What is the best way to do it? (The Permanent table has over 100 columns)
i.e.
Usually I create table like this.
DECLARE #TT TABLE(
member_id INT,
reason varchar(1),
record_status varchar(1) ,
record_type varchar(1)
)
But is there any way to do it without mentioning the column names and type, but mention the name of another table with the required columns?
select top 0 *
into #mytemptable
from myrealtable
I realize this question is extremely old, but for anyone looking for a solution specific to PostgreSQL, it's:
CREATE TEMP TABLE tmp_table AS SELECT * FROM original_table LIMIT 0;
Note, the temp table will be put into a schema like pg_temp_3.
This will create a temporary table that will have all of the columns (without indexes) and without the data, however depending on your needs, you may want to then delete the primary key:
ALTER TABLE pg_temp_3.tmp_table DROP COLUMN primary_key;
If the original table doesn't have any data in it to begin with, you can leave off the "LIMIT 0".
This is a MySQL-specific answer, not sure where else it works --
You can create an empty table having the same column definitions with:
CREATE TEMPORARY TABLE temp_foo LIKE foo;
And you can create a populated copy of an existing table with:
CREATE TEMPORARY TABLE temp_foo SELECT * FROM foo;
And the following works in postgres; unfortunately the different RDBMS's don't seem very consistent here:
CREATE TEMPORARY TABLE temp_foo AS SELECT * FROM foo;
Sortest one...
select top 0 * into #temptable from mytable
Note : This creates an empty copy of temp, But it doesn't create a primary key
select * into #temptable from tablename where 1<>1
Clone Temporary Table Structure to New Physical Table in SQL Server
we will see how to Clone Temporary Table Structure to New Physical Table in SQL Server.This is applicable for both Azure SQL db and on-premises.
Demo SQL Script
IF OBJECT_ID('TempDB..#TempTable') IS NOT NULL
DROP TABLE #TempTable;
SELECT 1 AS ID,'Arul' AS Names
INTO
#TempTable;
SELECT * FROM #TempTable;
METHOD 1
SELECT * INTO TempTable1 FROM #TempTable WHERE 1=0;
EXEC SP_HELP TempTable1;
METHOD 2
SELECT TOP 0 * INTO TempTable1 FROM #TempTable;
EXEC SP_HELP TempTable1;

How to get SQL Server to ignore checks?

I have a SQL script that populates a temp column and then drops the column at the end of the script. The first time it runs, it works fine because the column exists, then it gets dropped. The script breaks the 2nd time because the column no longer exists, even though the IF statement ensures that it won't run again. How do I get around SQL checking for this field?
IF EXISTS (SELECT name FROM syscolumns
WHERE name = 'COLUMN_THAT_NO_LONGER_EXISTS')
BEGIN
INSERT INTO TABLE1
(
COLUMN_THAT_NO_LONGER_EXISTS,
COLUMN_B,
COLUMN_C
)
SELECT 1,2,3 FROM TABLE2
ALTER TABLE TABLE1 DROP COLUMN COLUMN_THAT_NO_LONGER_EXISTS
END
I had a similar problem once and got round it by building all the queries as strings and executing them using the Exec() call. That way the queries (selects, inserts or whatever) don't get parsed till they are executed.
It wasn't pretty or elegant though.
e.g
exec('INSERT INTO TABLE1(COLUMN_THAT_NO_LONGER_EXISTS,COLUMN_B,COLUMN_C) SELECT 1,2,3 FROM TABLE2')
Are you checking the column isnt on another table ? If not you probably to check the table too see if statement below.
If you are already doing that is it running a in a single transaction and not picking up the that dropped column has gone ?
IF Not EXISTS (SELECT name FROM sys.columns
WHERE name = 'COLUMN_THAT_NO_LONGER_EXISTS' and Object_Name(object_id) = 'Table1')
Created a quick script program for this; can you confirm this matches what you are trying to do because in SQL 2007 at least this isnt returning an error. If i create the table and run through with teh alter table to add colc it works; if i then run the if / insert that works even after dropping the table.
create table tblTests
(
TestID int identity (1,1),
TestColA int null,
TestColB int null
)
go -- Ran this on its own
insert into tblTests (TestColA, TestColB)
Select 1,2
go 10
-- Insert some initial data
alter table tblTests
add TestColC Int
go -- alter the table to add new column
-- Run this with column and then after it has removed it
IF EXISTS (SELECT name FROM sys.columns a
WHERE name = 'TestColC' AND
OBJECT_NAME(object_id) = 'tblTests')
Begin
insert into tblTests (TestColA, TestColB, testcolc)
select 1,2,3
alter table tblTests
drop column TestColC
End