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?
Related
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".
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.
I have a legacy text based database whose data cannot be exported. I have to use mouse to select data on screen, and do ctrl+c to copy them to a txt file. I have managed to do this using AutoHotKey. Now I want this to be run in the background without interfering my other job on the computer.
The database will be an inactive hidden window. I could use ControlClick and ControlSend to type things. But I cannot select a piece of text like the way I do when the window is active. I tried the following but it does not work:
ControlClick, X%dat_x1% Y%dat_y1%, ahk_id %database_window_id%,,,,NA D
ControlClick, X%dat_x2% Y%dat_y2%, ahk_id %database_window_id%,,,,NA U
I guess it's because the mouse isn't moved using ControlClick. Can anyone please help me with this? Thanks.
"I guess it's because the mouse isn't moved using ControlClick."
If WinGetText (or Send, ^a^c to select all and copy) and then some regex pattern can't be used, maybe, after the click, you could just use Send, +{Right} to select..? Or for example Send, ^+{Right 7} to select 7 whole words/spaces.
You can query the database itself if you had the right permissions.
I've used this in the past: https://autohotkey.com/board/topic/90862-acclib-access-database/
In the clojure command line
user=>
Every time I close a parenthesis, the cursor ▌ quickly moves to the opening paren of that block and moves back right away (probably to help me see where I started).
This can quickly get very frustrating when I type fast. How can I turn this option off?
if you are using lein repl, it should be using readline - put the following in ~/.inputrc
set blink-matching-paren off
Okay, so here’s the script that I’ve written so far:
function MkCheck()
put = \"✓\"
endfunction
And it works all right, but it inserts the check mark on a line all by itself. I want to insert it right at where the cursor currently is. Is there any way to get put to place the character right before (or right after) the cursor?
You can enter in insert mode, append the character and return to normal mode:
function MkCheck()
execute "normal! i✓\<ESC>"
endfunction
The :put command, like most Ex commands, is linewise: it doesn't really care about the position of the cursor in the line.
You can use the :normal command to execute a normal mode command like i✓:
function MkCheck()
normal i✓
endfunction