How to execute SQL command without a transaction block in Golang? - sql

Problem
Error running schema migration, err: cmd failed, cmd:
ALTER TABLE "schema"."table" ALTER COLUMN kill_id TYPE VARCHAR(MAX)
err: pq: ALTER TABLE ALTER COLUMN cannot run inside a transaction block
AWS Redshift does not support running the above ALTER COMMAND in an transaction block.
Does database/sql supports running sql commands without a transaction block?
How to run raw sql commands without a transaction block in Golang?

Related

HSQLDB SqlTool is throwing user lacks privilege or object not found

I'm executing .sql scripts using SqlTool. It keep on saying user lacks privilege or object not found. The same script is working perfectly from Swing UI.
My Script (hello.sql)
\.
SET DATABASE SQL SYNTAX ORA TRUE;
ALTER CATALOG PUBLIC RENAME TO SOMENAME;
COMMIT;
CREATE SCHEMA SOMESCHEMA;
COMMIT;
CREATE PROCEDURE SOMENAME.SOMESCHEMA.SP_FAILED_COUNT(IN i_ssn VARCHAR(100), IN i_page_id NUMBER(10), IN i_ip_address VARCHAR(100), IN i_session_guid VARCHAR(100), OUT o_toomanyfails VARCHAR(2000))
READS SQL DATA
BEGIN ATOMIC
SET o_toomanyfails = 'N';
END
COMMIT;
.
:;
Exception
> java -jar sqltool-2.4.1.jar --autocommit --rcfile C:\\my-files\\hsqldb\\2.4.1\\dbmanager.rc web C:\\my-files\\hsqldb\\2.4.1\\hello.sql
Executing command from edit buffer:
"SET DATABASE SQL SYNTAX ORA TRUE;
ALTER CATALOG PUBLIC RENAME TO SOMENAME;
COMMIT;
CREATE SCHEMA SOMESCHEMA;
COMMIT;
CREATE PROCEDURE SOMENAME.SOMESCHEMA.SP_FAILED_COUNT(IN i_ssn VARCHAR(100), IN i_page_id NUMBER(10), IN i_ip_address VARCHAR(100), IN i_session_guid VARCHAR(100), OUT o_toomanyfails VARCHAR(2000))
READS SQL DATA
BEGIN ATOMIC
SET o_toomanyfails = 'N';
END
COMMIT;"
SEVERE SQL Error at 'C:\my-files\hsqldb\2.4.1\hello.sql' line 14:
"SET DATABASE SQL SYNTAX ORA TRUE;
ALTER CATALOG PUBLIC RENAME TO SOMENAME;
COMMIT;
CREATE SCHEMA SOMESCHEMA;
COMMIT;
CREATE PROCEDURE SOMENAME.SOMESCHEMA.SP_FAILED_COUNT(IN i_ssn VARCHAR(100), IN i_page_id NUMBER(10), IN i_ip_address VARCHAR(100), IN i_session_guid VARCHAR(100), OUT o_toomanyfails VARCHAR(2000))
READS SQL DATA
BEGIN ATOMIC
SET o_toomanyfails = 'N';
END
COMMIT;"
user lacks privilege or object not found: SOMENAME
org.hsqldb.cmdline.SqlTool$SqlToolException
The same script is working from HSQLDB Swing UI.
I tried adding commit but still it is not working. It is working fine if I remove catalogname.schemaname.(SOMENAME.SOMESCHEMA.) from script
Also, i didn't understand one thing.
If I execute the following command in Swing UI it working perfectly for first time but if i execute for the second time i get the following exception
user lacks privilege or object not found: PUBLIC / Error Code: -5501 / State: 42501
ALTER CATALOG PUBLIC RENAME TO SOMENAME;
It confirms that the catalog is renamed. But where if I run the same script using sqltool again and again it never throws the same exception. How to make it working from sqltool (i.e. after importing it from .sql script)
There's no way that "The same script is working from HSQLDB Swing UI."
I loaded the exact script into HSQLDB Swing UI v. 2.4.1 and it fails as I expected with "unknown token: / Error Code: -5582 / State: 42582" because you have SqlTool-specific grammar in your script.
If I remove the Sql-Tool specific grammar then Swing UI reports "user lacks privilege or object not found: SOMENAME / Error Code: -5501 / State: 42501".
Fred T. can explain exactly why, but those commands apparently have to be in two transactions and I can make it work from Swing UI by just executing everything before the "CREATE SCHEMA" in one execution; then the rest in another execution. I suspect that you actually edited your script (not running "The same script") and then executed it with an older version of HyperSQL Swing UI where it did not execute all of the commands in a single transaction.
SqlTool doesn't have that limitation and allows you to control exactly what commands go over in which transaction. Just allow all commands (the COMMITs are unnecessary) in their own transactions (which is default behavior). It then works.
TIPS:
You don't need the COMMITs because these are DDL statements.
You can
execute the . transaction by terminating it with ".;" instead of
separate "." and ":." commands.
You don't need the \. at all because
SqlTool automatically knows that you need a new grouped transaction
when it sees "CREATE PROCEDURE" beginning a command. If it makes
the code more readable for you, you can insert the ".\" immediately
before the CREATE PROCEDURE.
In summary this works from SqlTool:
SET DATABASE SQL SYNTAX ORA TRUE;
ALTER CATALOG PUBLIC RENAME TO SOMENAME;
CREATE SCHEMA SOMESCHEMA;
CREATE PROCEDURE SOMENAME.SOMESCHEMA.SP_FAILED_COUNT(IN i_ssn VARCHAR(100), IN i_page_id NUMBER(10), IN i_ip_address VARCHAR(100), IN i_session_guid VARCHAR(100), OUT o_toomanyfails VARCHAR(2000))
READS SQL DATA
BEGIN ATOMIC
SET o_toomanyfails = 'N';
END
.;

What is the difference between "psql -c" and "psql -f" when executing multiple queries?

I'm trying to execute two sql commands (create a new schema and table), in a way that would enable a rollback of both commands if the execution fails. The database I'm connecting to is AWS Redshift.
create schema if not exists test_schema;
create table test_schema.test_table as select 1;
Initially I tried to execute these commands programatically with python, using both psycopg2 and pyodbc, and got the following error:
ERROR: schema "test_schema" does not exist
I realised that it fails because the first command isn't being comitted, so to fix that , I tried setting the autocommit mode on, and wrapping the statements with "begin/end" block, which didn't help.
When I used psql CLI and ran the following, everything worked as intended (there was no "schema does not exist" error, and after the rollback, both schema and table were gone):
dev=# begin;
BEGIN
dev=# create schema test_schema;
CREATE SCHEMA
dev=# create table test_schema.test_table as select 1;
SELECT
dev=# rollback;
ROLLBACK
I tried to get the same results by running the following in the command line:
psql -c "begin; create schema test_schema; create table test_schema.test_table as select 1;"
This results in the same error:
ERROR: schema "test_schema" does not exist
However, when I put the above code in a file and ran the same command, this time using -f, it worked:
psql -f create_schema_and_table.sql
My questions are:
What is the difference between executing queries with "psql -c" and "psql -f"?
How can the same result be achieved programatically, with python?
Thanks a lot!
I don't know what you are doing wrong, your "psql -c" command works perfectly fine:
ads#diamond:~$ psql -c "begin; create schema test_schema; create table test_schema.test_table as select 1;" postgres
SELECT 1
psql will send the entire string to the server, and execute it in one single transaction. Your problem is that you start a transaction using "begin", but never commit it. Therefore at the end of the psql run, all your changes are rolled back. The next psql command will not find the schema, nor the table. But as long as everything stays in a single psql call, subsequent queries in the same command can see newly created objects.
Your query string should instead look like:
begin; create schema test_schema; create table test_schema.test_table as select 1; commit;
Or, more easy:
create schema test_schema; create table test_schema.test_table as select 1;
Both will work.

Execute SQL Server stored procedure through SSIS

I have this stored procedure:
CREATE PROCEDURE [dbo].[sp_Carrier_Scan_Compliance]
(#RETAILERID INT OUTPUT,
#SYSTEM_ID VARCHAR(10) OUTPUT)
AS
BEGIN
SET #RETAILERID = 2
SET #SYSTEM_ID = 'DMASOS'
...
END
I have created a SSIS package using a Execute SQL Task in the control flow.
These are my Execute SQL Task editor settings:
This are my Variable settings:
These are my Parameter Mapping settings:
When I run the SSIS package, I get an error:
Error: 0xC002F210 at Execute SQL Stored Procedure (to copy data from 'BI-Datatrunk' source table) Task, Execute SQL Task: Executing the query "exec = [sp_Carrier_Scan_Compliance] ? OUTPUT, ? O..." failed with the following error: "Incorrect syntax near '='.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Task failed: Execute SQL Stored Procedure (to copy data from 'BI-Datatrunk' source table) Task
Warning: 0x80019002 at Carrier_Scan_Compliance_SP: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
I am not sure what I am missing.
Please help me.
Thanks
The key part of the last error is
The EXECUTE permission was denied on the object
'sp_Carrier_Scan_Compliance', database 'DATAK', schema 'dbo'."
You need to assign EXECUTE permissions to the SQL user executing the Proc
USE DATAK
GO
GRANT EXECUTE ON sp_Carrier_Scan_Compliance TO <sql user>
GO

Creating table in Firebird script causes "unsuccessful metadata update" with deadlock

I have the following script that I run using "isql -i scriptfile.sql":
CONNECT C:\Databasefile.fdb USER user PASSWORD password;
SET TERM !! ;
EXECUTE BLOCK AS BEGIN
IF (EXISTS(SELECT 1 FROM rdb$relations WHERE rdb$relation_name = 'MYTABLE')) THEN
EXECUTE STATEMENT 'DROP TABLE MYTABLE;';
END!!
SET TERM ; !!
CREATE TABLE MYTABLE
(
MYCOLUMN VARCHAR(14) NOT NULL
);
The very first time I run this (when the table does not already exist) the table is created as expected.
If I run the script again I get the following error:
Statement failed, SQLCODE = -607
unsuccessful metadata update
-STORE RDB$RELATIONS failed
-deadlock
After line 8 in file d:\myscript.sql
When the script exits, MYTABLE has been deleted and can no longer be found in the database.
If I run the script a third time the table is once again created and no errors are thrown.
Why can't the script both delete and then recreate a table?
DDL from PSQL is not allowed, using EXECUTE STATEMENT it is not directly forbidden, and usually possible, but still not wise exactly because of these kinds of problems. I am not exactly sure about the reasons, but part of it have to do with how DDL changes are applied in Firebird; the use of execute statement adds additional locks iirc which conflict with a subsequent DDL for the same table name.
Instead of dropping and creating this way, you should use the DDL statement RECREATE TABLE instead.
Note that the word deadlock in this error is actually a bit of a misnomer (there is no real deadlock).

How to Truncate Multiple Table in SSIS ( using ADO.NET Destination and Oracle Database)

I got a little trouble in SSIS. I have multiple table and i want adding Truncate statement so that table can't create double data.
This is the image of package that I made :
each Data Flow, i used Flat File Source and ADO NET Destination.
And then, in Execute SQL Task i want to apply Truncate Table
After that, i have error message :
"[Execute SQL Task] Error: Executing the query "truncate table Table1
truncate table Tabl..." failed with the following error: "ERROR
[HY000] [Oracle][ODBC][Ora]ORA-00911: invalid character". Possible
failure reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established
correctly."
What i must suppose to do?
P.S
Sorry if my english is not good
Since the destination is an Oracle Database you should use this syntax:
begin
execute immediate 'truncate table t1';
execute immediate 'truncate table t2';
end;
Does this syntax work:
truncate table table1;
truncate table table2;
Note the semi colons.