User defined type dropped while in use - sql

I have an Oracle database with a table as follows:
create table Table1
(Column1 number(5,0),
Column2 special_type);
Now, due to some data errors, the support team decided the fix would be to drop and recreate the Type.
I now have a table as follows:
Table1
Column1 number(5,0),
Column2 null
The problem is, I cannot drop the table, I get a "Table has errors" message. I cannot alter the table, I get a "Table has errors" message. I have tried to manipulate the DDL in Oracle SQL Developer, guess what I get? A "Table has errors" message.
Can anyone point me in the right direction?

It seems support team have dropped the type forcefully. In normal scenario, you cannot drop a type if it has any dependent table.
See demo:
SQL> CREATE OR REPLACE TYPE NUU AS TABLE OF NUMBER;
/
Type created.
SQL> CREATE TABLE TBLLL(NUM NUU)
NESTED TABLE NUM STORE AS VVVVV ;
/
Table created.
Now when i try to do a simple drop type, i get the below error:
SQL> drop type nuu;
drop type nuu
*
ERROR at line 1:
ORA-02303: cannot drop or replace a type with type or table dependents
So i drop it forcefully:
SQL> drop type nuu force;
Type dropped.
And when i try to make a select i get the error:
SQL> select * from tblll;
select * from tblll
*
ERROR at line 1:
ORA-04063: table "USER.TBLLL" has errors
So in order to Alter the table you first need to create the type back. Once the type is created back, your table definition becomes correct and then you can Alter your table.
Other solution is to drop the table and recreate it which you already mentioned is not working.

You can try the following -
re-create the type
create another table with same table structure
insert the data into the second table
use the second table now.
You may want to not recreate the type since it caused issues earlier. Find an alternative. It might solve your issue.

Related

How to copy SQL column data to new column then drop the original column?

I am following the recommendation in this sql snowflake forum in order to transform an integer data column into a varchar by creating a new column. I want to drop the original integer column when I am done, but doing so always results in the new column no longer working and any future queries erroring out.
For instance, I have test_num is the integer and test_num_to_char is the varchar
alter table test_table
add test_num_to_char varchar as CAST(test_num as varchar)
then
alter table test_table
drop column test_num
select *
from test_table
results in an error message:
SQL execution internal error: Processing aborted due to error 300002:224117369
Is there a different transformation method that removes the dependency on the original integer column so I can drop it?
alter table test_table add test_num_to_char varchar(10);
go
update test_table set test_num_to_char = CAST(recno as varchar);
Try the TO_DECIMAL transformation method.
It's documentation is given here

How to truncate a partitioned external table in hive?

I'm planning to truncate the hive external table which has one partition. So, I have used the following command to truncate the table :
hive> truncate table abc;
But, it is throwing me an error stating : Cannot truncate non-managed table abc.
Can anyone please suggest me out regarding the same ...
Make your table MANAGED first:
ALTER TABLE abc SET TBLPROPERTIES('EXTERNAL'='FALSE');
Then truncate:
truncate table abc;
And finally you can make it external again:
ALTER TABLE abc SET TBLPROPERTIES('EXTERNAL'='TRUE');
By default, TRUNCATE TABLE is supported only on managed tables. Attempting to truncate an external table results in the following error:
Error: org.apache.spark.sql.AnalysisException: Operation not allowed: TRUNCATE TABLE on external tables
Action Required
Change applications. Do not attempt to run TRUNCATE TABLE on an external table.
Alternatively, change applications to alter a table property to set external.table.purge to true to allow truncation of an external table:
ALTER TABLE mytable SET TBLPROPERTIES ('external.table.purge'='true');
There is an even better solution to this, which is basically a one liner.
insert overwrite table table_xyz select * from table_xyz where 1=2;
This code will delete all the files and create a blank file in the external folder location with absolute zero records.
Look at https://issues.apache.org/jira/browse/HIVE-4367 : use
truncate table my_ext_table force;

How can i remove temp table after creating it

Hi How can i remove temp table after creating it
because i get this error There is already an object named '#tmp_statement' in the database.
SELECT * FROM #tmp_statement
DROP TABLE #tmp_statement
You are getting error on creating it, not when dropping it. Try putting this before creating table:
IF object_id('tempdb..#tmp_statement') IS NOT NULL
DROP TABLE #tmp_statement
Or just put it before any other statements you have.

Getting Security error while altering table

I have a table called borrowlist. When I try to drop a column I get an error that says :
Cannot find the object "borrowlist"
Because it does not exist or you do not have permissions."
Query : USE Test
alter table borrowlist DROP column MEMBERFULLNAME

Creating a new table using a synonym in SQL Server

I have a synonym created for this table ParaPITD.dbo.BM_00_PolicyMaster that points to ParaPITDYYYYMM.dbo.BM_00_PolicyMaster
There is no table yet - so it acts as a place holder.
I have a stored procedure that creates a temp table #BM_00_PolicyMaster and now I want to insert or select into the place holder where the synonym points to and create the table in ParaPITDYYYYMM.dbo.BM_00_PolicyMaster
If I run this:
select *
into ParaPITD.dbo.BM_00_PolicyMaster
from #BM_00_PolicyMaster
it creates the table in ParaPITD.dbo.BM_00_PolicyMaster and ignores the synonym
If I run this:
INSERT INTO ParaPITD.dbo.BM_00_PolicyMaster
SELECT *
FROM #BM_00_PolicyMaster
it gives me an invalid object error acting like the table must exist before it will insert.
Appreciate any help - thanks
From the doc (http://technet.microsoft.com/en-us/library/ms187552.aspx):
You cannot reference a synonym in a DDL statement
And any statement that creates a table, including SELECT INTO counts as a DDL statement. (See here: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1085a84c-faac-4d26-8f35-c64d8b901034/insert-into-is-sentence-dml-or-ddl)