Postgres - Finding an open cursor and closing it - postgresql

In SQL Server I used following SQL to find an open cursor and closing it in a 'catch' block.
IF EXISTS(SELECT 1 FROM sys.dm_exec_cursors(##SPID) WHERE [name] = 'Crsr_IDs' AND IS_OPEN = 1)
BEGIN
CLOSE Crsr_IDs
DEALLOCATE Crsr_IDs
END
In similar way how to find an open cursor in Postgres with specific name in EXCEPTION block and close it?

You can use pg_cursors table with list of opened cursors:
IF EXISTS(SELECT * FROM pg_cursors WHERE name = 'Crsr_IDs) THEN
CLOSE Crsr_IDs;
END IF;
But usually it is not necessary - if cursor was opened in protected block - BEGIN EXCEPTION WHEN END, then handler of exception closes opened cursors automatically.

You can use the pg_cursors catalog to query for open cursors in your session, but be aware that some of the cursors might have been opened by the system.
Usually you use CLOSE ALL to close all open cursors.

Here I assume that PL/pgSQL is used, and a cursor is assigned to a variable.
If you need to close a cursor if it is open, you can just execute CLOSE (see Using Cursors), and catch exceptions which can happen. For example:
do $$
declare
Crsr_IDs refcursor = 'Crsr_IDs';
begin
begin
close Crsr_IDs;
exception
when invalid_cursor_name or null_value_not_allowed then null;
end;
end;
$$;
Explanations:
CLOSE can raise exceptions in some conditions which we expect to happen.
Possible conditions:
invalid_cursor_name - there is no open cursor with the assigned name. Either the cursor is already closed, or it was not open at all.
null_value_not_allowed - the cursor variable is null. It could be not initialized at all, and the cursor name was not assigned to it.
See PostgreSQL Error Codes.
See Trapping Errors.

Related

executing create plpgsql function in vscode runs properly only if selecting the all function definition lines

I'm using vscode version 1.74.2 with SQLTools extension v0.26.0 in order to work with a postgresql 15 database.
when i'm writing a plpgsql function, the editor marks in color the function besides the last line of END $$ LANGUAGE plpgsql.
SQLTools provides the shortcut called SQLTools Connection: Run current query
i can't use it because i get unterminated quoted string error.
as you can see in the following screenshot it marks only the 3 first lines when i go with the keyboard cursor over the function definition:
so when I try to execute the run current query shortcut i get the following error:
unterminated dollar-quoted string at or near "$$
BEGIN
return query select 1::int;" at character 61
of course if i manually mark all the 4 lines and then run the query it creates the function properly, but this is very annoying since this is an example and i deal with complex functions and to start marking all of them in order to execute them each time is a hassle.
any ideas how i can resolve this ?

How to separate SQL code in blocks in SSMS?

In Oracle, I can separate blocks of PL/SQL code with the / character, and Oracle SQL Developer knows that when I run code with Ctrl+Enter, it's supposed to run all the code it finds until finding a /. Is there anything similar for SQL Server Management Studio (or T-SQL)?
As I note in the comment, one method is to highlight the SQL you want to execute, and then press the Execute Button (or F5) and only the SQL you have selected will be run.
This works in other Microsoft based IDEs too, such as Azure Data Studio:
Note, however, that when using such techniques that only variables defined in the highlighted SQL will be accessible. Take, the following statement for example:
DECLARE #MyString nvarchar(50) = N'This is my string';
SELECT #MyString;
SELECT CONCAT(#MyString, N' again');
If you highlight the last row only, you will get an error stating that the variable #MyString has not be declared:

how to execute and view the result set of the below DB2 stored procedure using sql developer tool

create or replace procedure customer_p (in cids int)
language sql
DYNAMIC RESULT SETS 1
p1:begin
declare cursor1 cursor with return for
select * from customer
where cid > cids;
open cursor1;
end p1
There is more than one way...
One Simple way is to use Ctrl-F5 to invoke the CALL statement .
For example: highlight CALL customer_p(100) (for example) and when selected use Ctrl-F5 to run it.
This is same as "Run script output as a script" (you can see this button in the lower pane).
If the stored procedure completes without error, and returns a non-empty result-set then you will see the result-set in the lower pane "Script Output".

Can I use cursor deallocate without cursor close?

What is proper way to close t-sql cursor?
Is it always required to call close before deallocate
Can I use cursor deallocate without cursor close?

SQLDeveloper: execute current line without highlighting

In Toad one can easily execute current line without highlighting it. Let say, you have a worksheet like this:
select * from item -- cursor here
select * from product
When I click on CTRL+Enter I want only the line where the cursor is to be executed. In SQLDeveloper, if there is no second line, CTRL+Enter works as I want. Basically, I want to do the same as described here, but for some reason, I can't find the Tools -> Preferences -> Window Types -> SQL Window and check "AutoSelect statement" in the version of the SQLDeveloper I am using: 4.0.0.13, build Build MAIN: 13.80.
Seems like, this functionality is taken out in the 4.x of Oracle SQLDeveloper?
If you have a block (anonymous or such) of code before your sql statement, make sure to end with forward slash, for the CTRL+enter to work.
CTRL+Enter on the select sysdate statement below works in second example below, but not on the first example.
Example 1:
begin
NULL;
end;
select sysdate from dual; -- press CTRL+Enter on this statement
Example 2:
begin
NULL;
end;
/
select sysdate from dual; -- press CTRL+Enter on this statement
For those who also wonder about the same thing, here is what you gotta do. End each statement with ; and it works.
select * from item
;
select * from product;
Actually the best option is mentioned here :
http://forums.allroundautomations.com/ubb/ubbthreads.php?ubb=showflat&Number=46683
1) Press Ctrl-F8 when the cursor is on the statement. Now only the
current statement is executed. You can assign a different key through
the preferences (Tools > Preferences > Key Configuration > "SQL
Window: Execute current statement").
2) Enable the "AutoSelect statement" preference (Tools > Preferences >
SQL Window). Now the standard execute function will automatically
select the current statement under the cursor and execute it. To
execute multiple statements you now have to explicitly select them
first in the editor, or use Ctrl-F8.
I also ran into a similar situation where Ctrl+Enter shortcut stopped working for executing any statement and had to highlight the entire statement for executing it.
How it worked:
(i) By removing/commenting out all the comments in the worksheet i.e. just have only the Sql statements in the worksheet, if at all you have put any statements/comments in the worksheet for your understanding, just disable those and then Ctrl+Enter starts working.
PS: I didn't do any other changes apart from this to make this shortcut work.
Cheers