Drop a View or Table, unknowing which it is, in sqlite3 - sql

I am in a situation where I want to drop a view or table, but can only know at run-time which it is (same identifier though). This does not work:
DROP VIEW IF EXISTS my_table_or_view;
Because if my_table_or_view is a table, it will throw:
android.database.sqlite.SQLiteException: use DROP TABLE to delete table my_table_or_view
Likewise, I cannot use DROP TABLE, because it tells me to use DROP VIEW if i have a view at hand. I could catch the error, of course, but since this is part of a larger transaction, I would definitely prefer an answer that works using pure SQL (as understood by sqlite3). Any ideas?

You can get information about items in your sqlite database by SELECTing from a pseudo-table called sqlite_master.
In this case, you would do:
SELECT type FROM sqlite_master WHERE name = 'my_table_or_view'
and the resulting information will tell you whether you're dealing with a table or a view.
More info: http://www.sqlite.org/faq.html#q7

Related

Cannot delete from view without exactly one key preserved table

I have to run a script for a company. I just get the same error every time.
The query:
DELETE FROM WMO
WHERE (clientnr = ****** AND number_message = *****)
The error:
ORA-01752: cannot delete from view without exactly one key-preserved
table
What did I wrong?
Thnx!
Database views are in general projection of one or more tables. It is a SELECT statement over one or more tables to be specific. For database engine it is impossible to decide what it should delete and from which table unless the view is constructed from single table.
The best solution is to run DELETE command against tables that are used to construct the view.
Additional information:
ORA-01752: cannot delete from view without exactly one key-preserved table

how to remove Error for schema bindings in redshift

I want to be able to make CTE to make the below SQL work, I am getting the error
ERROR: Cannot replace a normal view with a late binding view for the below SQL, any way I could change it up so that it doesnt bind with schema views?
CREATE OR REPLACE
VIEW "dev"."XXBRK_DAILY_FX_RATES" ("F_C", "CURRENCY", "C_D", "C_R") AS
SELECT DISTINCT GL.GL_R.F_C, GL.GL_R.CURRENCY,
GL.GL_R.DATE, GL.GL_R.C_R
FROM GL.GL_R
with no schema binding
WHERE GL.GL_R.C_T='Corporate'
UNION ALL
SELECT DISTINCT GL.GL_R.F_C, GL.GL_R.F_C CURRENCY, GL.GL_R.DATE, 1
FROM GL.GL_R;
So you seem to have a statement issue. The last 4 lines are after the ';' and not part of the statement being run. I'm guessing that these are extraneous and posted by mistake.
Views on Redshift come in several types - normal and late binding are 2. The view "dev"."XXBRK_DAILY_FX_RATES" seems to already exist in your cluster so your command is trying to replace it, not create it. The error message is correct, you cannot replace a view with a view of a different type. You need to drop the view, then recreate it as late binding.
Now be careful as other objects dependent on this view will be impacted when you drop it (especially if you CASCADE the drop). When you drop and recreate the view it is a new object in the database but replacing a view just make a new definition for the same object. Understand the impacts of drop to your database before you execute it.

Retrieve Script used in "Create Table As" Statement

We have a table in our Oracle Database that was created from an actual script.
Ex:
Create Table AS (Select * from table).
I was hoping to recover the original script the table was created from as the data is quite old in the table, but needs this created table needs to be refreshed. This table is created with data from another live table in our database, so if there is a way to refresh this without the original query - I'm open ears. Any solutions are welcomed!
Thanks!
I suppose you could also do a column by column comparison of this table against all others to see which one (if any) matches it. Of course, this would only be a guess.
It would require that object to actually be a materialized view instead of a table. Otherwise you are probably left off with exploring logs. Beyond that I doubt there is any way to recover the original select statement used to create that table.

update table error as data manipulation operation not legal on this view

I wrote simple update query as below
update table_name set name = 'new name
that gave to me error
SQL Error: ORA-01732: data manipulation operation not legal on this view 01732. 00000 - "data manipulation operation not legal on this view"
after that i check
select * FROM USER_OBJECTS WHERE OBJECT_NAME='table_name'
that list TABLE and MATERIALIZED VIEW same as the 'table_name'
I know materialized view is not possible to update but table should update
please let me know how can i update table using simple above update query
You cannot update the materialized view or the physical table it uses for storage. You can only update the table(s) the view is built against. Presumably you know those; if not you can get the view definition from the data dictionary, or with dbms_metadata.get_ddl.
Updating the view or it's backing table doesn't make sense anyway. Even if you were allowed to, any changes you made would be lost the next time the view is refreshed.
If you don't want the materialized view to ever be refreshed again then you could drop that with the preserve table option, which would leave behind just the physical table - and you then update that. But if you change your mind you'd have to recreate the view and specify the existing table.

There is already an object named 'tbltable1' in the database

I am trying to insert data from one table to another with same structure,
select * into tbltable1 from tbltable1_Link
I am getting the following error message:
There is already an object named 'tbltable1' in the database.
The SELECT INTO statement creates a new table of the name you provide and populates it with the results of the SELECT statement.
I think you should be using INSERT INTO since the table already exists. If your purpose is in fact to populate a temporary table, then you should provide a table name that does not already exist in the database.
See MSDN for more information on this.
If you are confident that tbltable1 is not required, you can drop the table first.
You may also want to consider using temporary tables...
Select * into ##MyTemporaryTable FROM tblTable1_Link
You can then use the temporary table in this session. (Ending the session should drop the temporary table automatically, if I remember correctly. It's been a while since I've worked with SQL Server).