SQLGrammerException : could not execute statement - sql

I am trying to alter a postgreSQL table using the following code.
String query = "ALTER TABLE \"georbis:world_boundaries\" DROP COLUMN testcolumn";
sessionFactory.openSession().createSQLQuery(query).executeUpdate();
The problem is my table name contains colon(:) due to which i'm getting the following error.
ERROR: unterminated quoted identifier at or near ""georbis? DROP COLUMN testcolumn" at character 13
STATEMENT: ALTER TABLE "georbis? DROP COLUMN testcolumn
Other answers of similar questions suggest that i should put double quotes(") around the table name which i tried but still getting the same error.
Also when i run the same query from SQL editor it works perfectly fine.
I also tried to run the same query for a table that does not contain colon(:) and it worked .
Solution : Thanks everyone. Got the answer from HERE
I changed my query like this and it worked.
String query = "ALTER TABLE \"georbis\\:world_boundaries\" DROP COLUMN testcolumn";

The problem is that ":" is a prompt for a named parameter. Executing the query directly in the ORM Session means the ORM will try to parse it and transform it.
Assuming you are using the latest Hibernate Session, you will need to execute the query at a lower level (jdbc connection), as this answer describes:
sessionFactory.openSession().doWork(
new Work() {
public void execute(Connection connection) throws SQLException
{
try(Statement stmt=connection.createStatement()){
stmt.executeUpdate("ALTER TABLE \"georbis:world_boundaries\" DROP COLUMN testcolumn");
}
}
}
);
That being said, are you absolutely sure you want to execute DDL from the ORM? There are other solutions out there. If your project is Maven, you can use SQL Maven plugin to put up then tear down SQL setups for testing.

Related

Delete primary index of a database table programmatically

I need to delete a Primary key indexes on some database tables. Afterwards indexes will be re-created. Meanwhile some calculation will be performed.
I have following SQL Commands:
DROP INDEX "SAPSR3"."KNA1~0";
that are working correctly.
But once called within ABAP program:
EXEC SQL.
DROP INDEX SAPSR3.KNA1~0
ENDEXEC.
The message "SQL error 911 occurred when executing Native SQL" has been raised.
I've also tried with:
CALL FUNCTION 'RSDG_KEY_INDEX_DEL'
EXPORTING
i_tablnm = lv_tblname. " containing "KNA1"
This also doesn't work.
Is there any FM or similar way to perform such activity?
I also tried:
EXEC SQL.
DROP INDEX KNA1~0
ENDEXEC.
What you tried, in the SQL console (DROP INDEX "SAPSR3"."KNA1~0";), and in the ABAP program (DROP INDEX SAPSR3.KNA1~0), are not written identically as you can see!
I have just tried in ABAP below the version that you tried in the SQL console, and it dropped the index as intended!
EXEC SQL.
DROP INDEX "SAPSR3"."KNA1~0"
ENDEXEC.
So, why, in your ABAP attempt, did you remove the double quotes around the index name? (and also for the scheme name, but I guess it has less undesired effect).
In the SQL console, I'm really not sure that DROP INDEX SAPSR3.KNA1~0 works! Because there is the special "tilde" character, you need to add double quotes around the index name, the following is the minimal working syntax: DROP INDEX SAPSR3."KNA1~0".

sql server - An expression of non-boolean type specified in a context where a condition is expected, near 'dbo'

i'm getting the above error when i try and run any alter table type script, even an add column script returns this error eg:
alter table up_retail add StockGroupID int null
running the above will return the above error, i have tried restarting sql, restore ansi settings etc, does anyone have any advice on what might be causing this?
UPDATE:
when i try and drop constraints as per
ALTER TABLE [dbo].[ZSeeAlso] DROP CONSTRAINT [FK_ZSeeAlso_Performers]
i get this:
Incorrect syntax near 'dbo'.
again, any ideas what would cause this?
** UPDATE 2 **
for everyone checking this is the only thing in my query window, here is my ssms:
Your query is okey , try to Use YourDB that contains the table up_retail Not Master like this :
Use YourDB
Alter Table up_retail Add StockGroupID int null
Go
after trying everything i can think and that, that was suggested in here, i basically ended up re-installing sql and that seems to have fixed. it looks like the syntax engine or sql itself became corrupt for some reason
thanks all for the suggestions

CREATE TABLE IF NOT EXISTS statement in SQLite

I have a WPF application where I access a SQLite database via ADO.NET (http://adodotnetsqlite.sourceforge.net/). So far everything works fine, but when I try to execute the following SQL:
sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS notes (id integer primary key, text varchar(100));";
sqlite_cmd.ExecuteNonQuery();
I get the following exception:
An exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll but was not handled in user code.
Additional information: near "NOT": syntax error
When I remove the IF NOT EXISTS part it works fine, but I want to create the table only if it is not already there. Is there anything I'm doing wrong?
This question has some answers which may be helpful. From that question, however, this answer suggests that SQLite 3.3 and above support IF NOT EXISTS.
Based on that question's answers, you could try selecting the COUNT of tables named 'notes' using this (slightly modified) query:
SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='notes';
You can then test the result of that query. If there were 0 results, create the table. Otherwise, don't create the table.

Can an ALTER TABLE accept query results in Access 2010

I am getting a syntax error on the below code trying to execute an ALTER TABLE query in Access 2010. The ulimate goal was to execute this from a VB.net app. Both queries work indepently within Access.
ALTER TABLE [Test_table] DROP CONSTRAINT (SELECT [MSysRelationships].[szRelationship]FROM [MSysRelationships] WHERE MSysRelationships.[szObject]='Test_table');
I guess the issue is whether or not the ALTER statement can accept query results as the input?
I'm quite sure that Access SQL does not support the syntax you tried to use. You'll probably have to run the SELECT query first, pull the constraint names into a recordset (or similar), then loop through the rows and issue the ALTER TABLE statements one by one.

SQL - create database and tables in one script

Sorry if already asked, but I can't find anything on this.
I am moving something over from MySQL to SQL Server I want to have a .sql file create a database and tables within the database. After working out syntax kinks I have gotten the files to work (almost).
If I run
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
it works fine, and if I run
CREATE TABLE dbname.dbo.TABLE1 (
);
...
CREATE TABLE dbname.dbo.TABLEN (
);
it also works fine. But, if I run them in the same file I get this error
Database 'dbname' does not exist
Right now, the CREATE TABLE statements are not within the IF statement, which I would like, but I also cannot seem to find the syntax for that. ( { } does not work?)
So my big question is, how do I ensure a particular command in a .sql file is completed before another in SQL Server?
My second question is, how do I include multiple instructions within an IF clause?
To be clear, I have been running this into sqlcmd.
Put a GO command between queries.
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
GO
CREATE TABLE dbname.dbo.TABLE1 (
);
CREATE TABLE dbname.dbo.TABLEN (
);
As for putting the table statements in the IF, you wouldn't be able to because of the GO command. You could create additional IF statements afterwards, to check for each tables pre-existence.
The syntax for a block if is:
IF condition
BEGIN
....
....
END
Between creating the database and creating the tables you will need a USE statement.
USE dbname
This way the tables will be created in the correct place, without having to specify the DB name on everything.
Also, GO and BEGIN...END like everyone else is saying.
You have to separate the statements with the GO keyword:
sql query
GO
another sql query
GO
and so on
By placing a GO between statements (to create separate batches of statements)