How to separate SQL code in blocks in SSMS? - tsql

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:

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

Column editor in Oracle Sql Developer

Anyone knows if there is a column editor function in Oracle SQL Developer?
I have to modify the same column in a large number of insert statement and it would be really useful to have a column editor.
To explain better which is my problem i am searching a function similar to Eclipse ones when you type Alt + Shift + A (How do I enable the column selection mode in Eclipse?), and it allow you to edit in one time a lot of columns.
There is something similar or i have to do it manually?
I am using 4.1.5.21 version.
Thanks
If you're using a newer version of Oracle Sql Developer (at least 18.3.x but perhaps before) there is an even easier way.
Simply go to the "Edit" menu and then choose "Block Selection":
Edit/Wrap Selection (enable it):
https://www.thatjeffsmith.com/archive/2012/07/how-to-block-select-text-in-oracle-sql-developer/
Just press Shift and move cursors up and down, etc.
Be careful to press Esc after each edition as it seems to "stick" or something and it keeps adding cursors and modifying where you don't expect it.
Trick: The multi-cursor is useful to edit several parts in the same line.

Making a basic query on PSQL does not display anything

I have connected to my PSQL database on command line and can run commands like \dt or similar just fine.
However, when I type in a basic query such as SELECT * FROM myTable and hit return, it does not display anything and just goes to the next line waiting for another command.
Any idea why?
You need to terminate the query with ;
SELECT * FROM myTable;
There is actually a less obvious cause of the above. If you've ommitted a closing bracket ()) somewhere in the query, you'll get the same behaviour. E.g.
SELECT *
FROM some_table
WHERE (NOW() - date_value;
It won't attempt to execute the statement until it gets a closing ); before it spits out a syntax error. While the above is fairly obvious where the issue it, it gets a lot trickier to spot with more complex queries. To get out of it, close with multiple brackets ))))))))); which will still resolve and spit out the syntax error so you can continue and correct.
A way to spot if this is the case, is to use the Up arrow through your history - if you're not seeing your previously typed (but not yet executed) statements, then you know you're still in a state where it's trying to close the statement before attempting a syntax check.
The issue isn't limited to brackets, but any other closing symbol such as quotes. The trick is to pay attention to the psql prompt as it shows the parsing state. E.g.;
DATABASE=> the => means all is normal and it's parsing a single line
DATABASE-> the -> means you're on a new line and likely missing a ;
DATABASE(> the (> means you haven't closed a bracket
DATABASE'> the '> means you haven't closed a quote.
Etc, you get the idea.
I fixed this exact issue by clicking the maximize button in the top right corner of the query terminal. (PGADMIN4 - PostgreSQL 15)
I'm using PostgreSQL 15 and was running queries. I then maximized the query terminal, and following that, I was no longer able to view the query that was ran successfully. I then clicked the maximize screen and found that the results had simply been hidden behind the maximized query terminal.
Hope this helps!

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