Oracle SQL Developer - Spooling error - sql

I am learning SQL at the moment, and am using Oracle SQL Developer.
Right now I am having an issue with the spool command.
I insert the file path and name the file but I keep getting an "unknown command" error.
spool C:\Users\user\Desktop\AAA_Task1\output.txt
set echo on
--drop table TESTTABLE;
create table TESTTABLE (TestCol int);
set echo off
spool off
I alternate between the create and drop statements for each test.
I run the and the table is created or dropped correctly, but I get a line one error "Unknown Command" and no text file is created.
I have attached a picture of a test command and the error.
If someone could point me in the right direction I would be grateful.

There is difference between run script and run statement, see here.
Use following commands and run script.
spool 'C:\Users\user\Desktop\AAA_Task1\output.txt'
set echo on
--drop table TESTTABLE;
create table TESTTABLE (TestCol int);
set echo off
spool off

Making the file path in quotes did not work for me in the Oracle database.
spool data.txt
set echo on
Query -- the query
set echo off
spool off
this will save the query and output to the data.txt file in the
"C:\Users\NELSON JOSEPH\AppData\Roaming\SQL Developer\data.txt" in this location likewise username varies for others.

Related

SQLPLUS : set sqlformat insert : SP2-0158

I want to export all my datas as insert statement.
In Sql Developer, the command "set sqlformat insert" work very well... but when I want to use it on sqlplus, I have this error message : SP2-0158 unknown SET option "sqlformat"
This command work when it's use on a script but i've made a script :
spool /data/scripts/result/test.sql
set sqlformat insert;
select * from mytable;
spool off
I've try the same code on SQLdev and it works...
How can I use it correcly on sqlplus ?
sqlformat insert is NOT an sqlplus acceptable format, try it with sqlcl. It should work (and allow you to use the same setting as you can do in sqldev)

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.

SQL Server trigger is stuck on executing

I am trying to run a batch file in a SQL Server trigger to get information from a query and put it in a text file. I am trying to do this because I want to do stuff with the information in that same batch file later on. The problem is that when the trigger is called, it stays stuck on executing.
I have tested the query and batch file call in Microsoft SQL Server Management Studio and they both work but when I call the batch file in the trigger is stays stuck at executing.
Here is my code. First the batch file, then trigger, query the batch file is calling, and my query to test the trigger
#echo off
echo start
sqlcmd -S AZ7GH2\SQLEXPRESS -h -1 -i C:\Users\user1\Documents\test3.sql -o C:\Users\user1\Documents\test.txt
echo end
exit
SQL Server trigger
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[ffupdate]
ON [dbo].[feat]
AFTER UPDATE
AS
BEGIN
IF UPDATE (act)
BEGIN
EXEC xp_CMDShell 'C:\Users\user1\Documents\ffscript.bat'
END
END
test3.sql (query being called by batch file)
:setvar SQLCMDERRORLEVEL 1
SET NOCOUNT ON
USE [dev]
DECLARE #ver INT
SET #ver = CHANGE_TRACKING_CURRENT_VERSION() - 1
CREATE TABLE #ctb(fuid INT)
INSERT INTO #ctb
SELECT featid
FROM CHANGETABLE(CHANGES feat, #ver) AS tb
SELECT fl.flg
FROM fl, #ctb
WHERE fl.fid = #ctb.fuid
GO
:setvar SQLCMDERRORLEVEL 0
SET NOCOUNT OFF
Query to test trigger
USE [dev]
UPDATE feat
SET act = 0
WHERE featid = 1;
I don't know what is wrong. I have looked for an answer and can't find one. Like I said, everything works fine by itself but when put together it stays stuck at executing. Any help would be greatly appreciated.
You are locking yourself !
You have a trigger on the table FEAT , and when you update one row ( session A ) , you create a new session ( session B via the batch ) that try to read some capture information on the table FEAT .
For future reference... you might want to consider adding "COMMIT" at the end of your input file to make sure no tables are being locked for change.

How to use spool command in sql developer oracle

Dears,
I cannot use spool command. It is not working or i am not doing something right.
I am trying to save query result in txt file (tried also csv saving with select /*csv*/ * from table but it also did not work).
So what i wrote is:
set echo off
set trimspool on
spool 'C:\Users\username\Desktop\clobams\Test1.txt'
select pn, serial_number from stock ;
spool off;
And i am getting error when pressing execute statement ORA-00900:invalid SQL statement . Sql works fine - when i execute only that part,it gives me output to the screen. How should i save my output to the file?
I have already tried to remove set echo off and set trimspool on but none of these helped
this is what i have run on my sql developer:
spool C:\Users\**direcotory**\Desktop\old desktop\sql\Test1.txt;
select * from YOUR_TABLE;
spool off;
and i got output in file test1. you need to run all commands as script.

Executing sql file from another sql file

I have a .sql file with name Alter_table.sql which have the following code.
alter table mytable add newcolumn VARCHAR2(1);
I don't want to edit this file and add a spool command. However I need to execute Alter_table.sql by writing spool in another file (execute_sql.sql) which should look like the below. I am not sure of the correct syntax. Can anyone please help here?
SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL Alter_Table_STD_SOURCE.log
EXEC username/password#database `Alter_table.sql`
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
(Thanks to Alex Poole) :-)
You need to connect first, then run your .sql file in the wrapper script using the '#' sign or 'START' commands:
...
-- Connect if not already connected.
CONNECT username/password#database
#Alter_table.sql
...
I'm not sure its a good idea to keep login/password in a file but you need to take security into account.