I've got an old schema that needs to be removed. Problem is it's got the special character \ in the name (believe me I'm no fan of special characters in schema names). I have tried commands like the following
DROP SCHEMA databasename."COMPANY\user1"
where COMPANY\user1 is the name of the schema.
However, I end up getting errors like the following
SQL Error: Incorrect syntax near '.'.`
I've dropped all of the tables inside of the schema, so I don't think there should be any objects remaining. I had success dropping tables with the following command
DROP TABLE databasename."COMPANY\user1".persontable;
Any idea why my attempt to drop the schema is failing? I'm sure it's something obvious I'm missing in the syntax.
Have you tried using square braces?
drop schema databasename.[COMPANY\user1]
Actually, this doesn't work, because drop schema doesn't accept the database (as sort of implied by the syntax in the documentation). Just go into the database and do:
drop schema [COMPANY\user1]
This works for me with names that have unusual characters.
Related
I am trying to drop a table names "union" but I keep getting an error.
I am not sure who and how created that table, but nothing works on it, including describe or select.
Using "hdfs dfs -ls" outside of hive, I can see that table exists and there is data in it, but cannot drop the table.
I am assuming there may be a problem because the table is called "union" and the error I get is
"cannot recognize input near 'union'".
How can I drop the table?
to escape in hive you can use bakctick:
DROP TABLE IF EXISTS `union`;
I've created a DB2 sql script that populates a static table and then does a rename to swap out the live table with the newly updated one. Its a fairly large SQL script so I'm only including the areas that Im having a an error on.
I'm getting the error: "[IBM][CLI Driver][DB2/NT64] SQL0104N An unexpected token "RENAME" was found following "D_HOLIDAY_LOG_OLD; ". Expected tokens may include: "TRUNCATE". LINE NUMBER=382. SQLSTATE=42601".
I suspect, its a syntax issue with the RENAME commands. If I need to add the whole query, I can. Thanks in advance
CREATE OR REPLACE PROCEDURE NSD_HOLIDAY_LOG_SPROC()
LANGUAGE SQL
SPECIFIC SP_NSD_HOLIDAY_LOG_SPROC
DYNAMIC RESULT SETS 1
BEGIN
COMMIT;
TRUNCATE TABLE TMWIN.NSD_HOLIDAY_LOG immediate;
DROP TABLE NSD_HOLIDAY_LOG_OLD;
RENAME TABLE TMWIN.NSD_HOLIDAY_LOG_LIVE TO NSD_HOLIDAY_LOG_OLD;
RENAME TABLE TMWIN.NSD_HOLIDAY_LOG TO NSD_HOLIDAY_LOG_LIVE;
RENAME TABLE TMWIN.NSD_HOLIDAY_LOG_OLD TO NSD_HOLIDAY_LOG;
END#
This is frequently asked.
As you are using static SQL in an SQL PL stored procedure, you must follow the documented rules for blocks of Compound SQL (Compiled) statements.
On of those rules is that static SQL has a restricted set of statements that can appear in such a block of code.
For example, with current versions of Db2-LUW, you cannot use any of the following statically (including rename table) :
ALTER , CONNECT,CREATE, DESCRIBE, DISCONNECT, DROP, FLUSH EVENT MONITOR, FREE LOCATOR, GRANT, REFRESH TABLE, RELEASE (connection only), RENAME TABLE, RENAME TABLESPACE, REVOKE, SET CONNECTION, SET INTEGRITY, SET PASSTHRU, SET SERVER OPTION ,TRANSFER OWNERSHIP
Other Db2 platforms (Z/OS, i-series) might have different restrictions but the same principle.
To achieve what you need you can use dynamic SQL instead of Static-SQL (as long as you understand the implications).
In other words, instead of writing:
RENAME TABLE TMWIN.NSD_HOLIDAY_LOG_LIVE TO NSD_HOLIDAY_LOG_OLD;
you could instead use:
execute immediate('RENAME TABLE TMWIN.NSD_HOLIDAY_LOG_LIVE TO NSD_HOLIDAY_LOG_OLD' );
or equivalent.
You can also use two statements, one to PREPARE and the other to EXECUTE , whichever suits the design. Refer to the documentation for execute immediate.
The same is true for other statements that your version of Db2 disallows in static compound-SQL (compiled) blocks (for example, DROP, or CREATE etc.).
I am using the following to create a database:
use master
IF DB_ID(N'delete') IS NULL
CREATE DATABASE delete
but get an error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'delete'
because delete is a reserved word. But is there no way of creating a database named "delete"?
As you are seeing, delete is a reserved word in most SQL databases.
You could quote the identifier. In T-SQL, which you seem to be using, you would do this with square brackets:
CREATE DATABASE [delete];
But I would not recommend that. From thereon, you will need to quote the database name each and every time you use it - if you fail to do that, you might encounter non-intuitive error messages. As I see it, there are enough words in the English language that we can avoid the very small subset of SQL reserved words (I would extend that to keywords too).
How about this, for example:
CREATE DATABASE db_delete;
Delete is a reserved word, try [delete] instead of delete or choose a better database name e.g. itemstodelete?
Is there a column comment syntax that allows me to specify a column comment directly where I declare the column in the create table statement (i.e. inline)? The 11g spec does not mention anything, on another page something is mentioned but I could not get it to work. There is a way to specify comments after creating the table, but I think it is annoying that the comment is separated from the field definition. I am looking for something like this (which does not work):
create table whatever (
field number(15,0) primary key comment 'primary key generated from sequence pkseq',
...
)
I'm afraid the "annoying" COMMENT ON syntax is the only way of doing this. SQL Server, PostgreSQL and DB2 use the same syntax (even though, as far as I know, there is no ANSI standard syntax for adding comments to database objects).
MySQL supports the way you would like it to work. I agree it would be a nicer mechanism, but in my experience so few people use comments at all that I doubt Oracle will ever change it.
I'm afraid it can only be done after table creation, using the comment on column ... is '' syntax.
A workaround to this annoying syntax is also to view and edit the tables in Oracles SQLExplorer. It contains a wizard that allows you to edit the comments right next to the columns. It even allows easy creation of alter table scripts.
My procedure when editing tables is to enter the changes in the wizard without actually executing them, then go to its DDL tab and retrieve the SQL from there (as update, not full create script) and press cancel on the wizard. Then I put the created SQL into the SQL script I am writing. Only when I am finished with the script I execute everything; I do never make any changes with the wizard itself.
Test on sqlplus (or similar), but the syntax is as follows:
-- assuming you have privileges
COMMENT ON COLUMN SCHEMA1.TABLE1.COL1
IS 'My comment'
-- then you can double check like this
SELECT * FROM all_col_comments WHERE
(OWNER, TABLE_NAME, COLUMN_NAME)
IN (('SCHEMA1','TABLE1','COL1'));
Note that the comment will now show in SQLDeveloper (or Toad or whatever env you have) until you reopen said table's properties.
Similar syntax can be used to annotate tables, indexes and materialized views. [source: https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_4009.htm]
I understand similar syntax exists for MySQL and others, but it is not proper ANSI. It's very useful, though.
I have been using a script run from a batch file to create and edit a table. As far as I cold tell it was working and I worked on a different issue.
However, at some point something has gone wrong.
In PLSQL Developer I tried to select from the table and I got the error:
ORA-01775: looping chain of synonyms
If I try to drop the table I get the error:
ORA-00942: table or view does not exist
I have never, intentionally, created and synonyms so I checked with this:
SELECT owner,synonym_name,table_owner,table_name from dba_synonyms where synonym_name='broken_table';
There are no results returned. The other similar questions on here do not seem to have the same situation or the actual solution, can anyone give any advice on how to proceed here?
(as you can guess sql is not my strong suit).
I got the ORA-01775 error from trying to access a table via a synonym where the table did not exist. The table had been dropped and had not yet been recreated. The synonym was still there. There was no looping synonym.