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

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".

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:

Automatic Code Identation

I have a huge code and now for testing purpose I have to add that whole script into an infinite while loop is there any short way (without pressing space for each row) to add a space for indentation so the whole code is consider part of the one while loop ? Such as for example when we press ctrl +r it comments out the line
Ctrl-I/Cmd-I will automatically indent the file. Other wse you just select multiple row and use Tab/Shift-Tab to move them backwards and forwards.
For indentation is a must, however Matlab as a language does not care so it is not really a must to indent it. Additionally, you can just execute the code from the command line, say that you script or function is called Umar, then from the command line you just type while 1, Umar; end.
You can copy the code into notepad++.
Activate Column mode selection holding alt+shift and use the mouse to select the column of all the text you want to insert a space/tabulation/etc. and just insert it.
Final step is to copy back the code to matlab.
Matlab does not currently support column selection.
MATLAB has the option to select all your code, then press the right click and select smart indent button.
If you like to use shortcuts, just type the combination of Ctrl+A (select all) followed by Ctrl+I (smart indent)

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

How can I move the cursor quickly to a specific location in a text I have just copy pasted in an Advanced Scripting command?

I am writing an Advanced Scripting voice command in Dragon NaturallySpeaking Professional 11.5 to write SQL commands by voice. Regarding the ORDER BY variable_name ASC command, I would like to move cursor right before ASC:
Sub Main
Clipboard(" ORDER BY ASC ")
SendKeys "^v"
End Sub
How can I do so efficiently as in the built-in double quotes command?
Add some arrow keys right after ^v:
Sub Main
Clipboard(" ORDER BY ASC ")
SendKeys "^v^{LEFT}{LEFT}"
End Sub