SSMS - Collapse Query Code at open by default? - tsql

I have a Query with a few exceptionally [for me] long sections [Insert Into #TableName ...]. When I open the Query, these sections have minus signs in little squares to the left of the 'Insert Into ...' statements:
[-] Insert Into ...
so I can collapse the sections I am not actually working in at the time.
Is it possible to have the query [or all my queries in SSMS] open with the various sections collapsed - by default?
I hope I have laid out my question clearly.
Thanks!!

Related

Adding an index in mysql workbench corrupted the table?

Step-by-step:
Right clicked on tbl > Table Inspector > Clicked "Columns" tab > Right click > Create Index >
In that section I left the following defaults:
Algo: Default
Locking: Default (allow as much concurrency as possible)
It gave a timeout error
I then tried to run a simple "SELECT * ", but it's timing out every time now.
I didn't think that adding an index can corrupt a table so I didn't do a backup and now in a bit of a panic mode... Is there anything that can be done to reverse this?
When doing the show full processlist I see the following:
A header
Another header
'Waiting for table metadata lock'
'CREATE INDEX idx_all_mls_2_Centris_No ON mcgillim_matrix.all_mls_2 (Centris_No) COMMENT '''' ALGORITHM DEFAULT LOCK DEFAULT'
In the processlist, it's clearly visible your index creation is waiting for metlock which means your table is already locked by another query which is like select distinct t1.broker_name and running from 3460 seconds.
You have two options here.
Let that SQL complete first. Then index will create.
Another, Kill that Select SQL which will not harm your system and can run later.
To kill query, You can find ID in information_schema.processlist. then simply run the below query.
kill ID;

How to find column name in table

I'm trying to find a column in a very big table in ORACLE SQL Developer, its really difficult to find it visually.
Is there any easy way to find the column in the table?
For example, in DBeaver its just Tab and then Ctrl + f
Oracle has an awesome data dictionary. Most of the time it will be even faster to write a query that accesses some of its views than use IDE features.
You can get columns from view ALL_TAB_COLUMNS.
SELECT *
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = :your_table_name
AND COLUMN_NAME LIKE '%YOUR_SEARCH_STRING%'
As for SQL Developer, you can open table from your connections tree, go to Columns tab and just use Edit -> Find (Ctrl/Cmd + F). Works for me in 4.0.2.15.
I would prefer writing a query is more faster compared to IDE provided buttons and clicks.
If so this would work out.
Use the following.
You can try this via SQL tool that is used by you
select table_name from all_tab_columns where column_name = 'PICK_COLUMN';
Or if you have DBA privileges,
select table_name from dba_tab_columns where column_name = 'PICK_COLUMN';
But if you are not sure about the column names you can add LIKE statements to current query.
Eg:
select table_name from all_tab_columns where column_name LIKE '%PICK_COLUMN%';
There is no search feature in the SQL Developer data grids for finding/navigating to a specific Column, but I'll add that as a feature request.
You may find the Single Record View handy for browsing very WIDE records.
On toolbar, Click View->Find DB Object
Now select the connection, the type and which column the value has to be found in. (NAME,TYPE AND USAGE(ALL))
The tables are displayed, select the table to view the columns having the field you are searching for.
Right click on column header in the data grid, then you have a menu Columns where you can filter columns you want to display
Right click on your connection name.
Then click "Find DB Object" on the context menu.
It will take you to a textbox. Write your column name
And check the box with "Columns".
Then press "enter" or click on "Go".
Then sqldeveloper will search your column and will show you the result [ column name with table name].

PostgreSQL 9.3 sql result: long text has shortened

I'm trying to SELECT some text that so long (about 500~ char), when using pg-admin > sql to test my sql, its return result which has shortened (about 250 char + '(...)').
Anybody know how to config PostgreSQL to always show fully text result ?
Thank you.
Updated
my_table(
my_column text;
)
INSERT INTO my_table(my_column) VALUES ('this is long(500~ char more) long text');
SELECT my_column FROM my_table
output pane display:
-> this is long(250~ char more) (...)
I think this will more clearly :)
In pgadmin go in the menĂ¹:
File --> options
Under "Query tool", select "Query editor".
In the box "Max Characters per column", insert... a big number :-)
Maximum is 2147483647, but it can consume a lot of memory in some case... Anyway, if it is not a production server you have not to worry.

Insert and update records in one TSQL statement?

I have a table BigTable and a table LittleTable. I want to move a copy of some records from BigTable into LittleTable and then (for these records) set BigTable.ExportedFlag to T (indicating that a copy of the record has been moved to little table).
Is there any way to do this in one statement?
I know I can do a transaction to:
moves the records from big table based on a where clause
updates big table setting exported to T based on this same where clause.
I've also looked into a MERGE statement, which does not seem quite right, because I don't want to change values in little table, just move records to little table.
I've looked into an OUTPUT clause after the update statement but can't find a useful example. I don't understand why Pinal Dave is using Inserted.ID, Inserted.TEXTVal, Deleted.ID, Deleted.TEXTVal instead of Updated.TextVal. Is the update considered an insertion or deletion?
I found this post TSQL: UPDATE with INSERT INTO SELECT FROM saying "AFAIK, you cannot update two different tables with a single sql statement."
Is there a clean single statement to do this? I am looking for a correct, maintainable SQL statement. Do I have to wrap two statements in a single transaction?
You can use the OUTPUT clause as long as LittleTable meets the requirements to be the target of an OUTPUT ... INTO
UPDATE BigTable
SET ExportedFlag = 'T'
OUTPUT inserted.Col1, inserted.Col2 INTO LittleTable(Col1,Col2)
WHERE <some_criteria>
It makes no difference if you use INSERTED or DELETED. The only column it will be different for is the one you are updating (deleted.ExportedFlag has the before value and inserted.ExportedFlag will be T)

What means the statement "Select ... for all Entries ..."

I was trying to do select for all entries in select statement - abap. I'm not getting the clear idea what select for entries does. Does any one know ?
Kindly have a look at the statements below:
1.
select bukrs belnr xblnr budat
from bkpf
into table it_bkpf
where belnr in s_belnr
2.
select bukrs belnr buzei gsber zuonr wrbtr kunnr
from bseg
into table it_bseg
for all entries in it_bkpf
where belnr = it_bkpf-belnr.
Please let me know the difference in two statements.
Siva
Some obvious differences:
Different tables
Different target fields
The 2nd select had a syntax problem: You used form instead from (I corrected it with my edit)
Other differences:
The selection 1.) uses in in the where clause. So it uses a select-options (or a range-object).
for all entries in it_bkpf means, that the internal table it_bkpf contains a list of elements, you want to select. Or in other words: Select all entries in bseg, where a filed belnr is an element of hte internal table bseg.
You will get clear answer through ST05 transaction.
You could execute st05 transaction, choose trace SQL and activate
trace.
After that run your code.
Enter st05 again choose deactivate trace, then view trace result.
There you can see exact SQL code that is forwarded to database server. As BSEG is clustered table, you could not use intuitive header-item join to retrieve needed financial movements inforamation. It's just because there are several tables including BSEG are storing in single database table, so database server technically can not separate BSEG rows and find BSEG-specific fields to make proper join.
So you can do join-like construction at application server. First you are retrieving all header-related columns from header table ( BKPF). Next when SELECT ... FOR ALL ENTRIES IN ... is executed application server will take a little portions of header rows (typically 5) and construct SQL queries for retrieving packs of items, corresponding to that portions. Next all that portions will merged in single internal table. So there will be only items of desired document as it were if you could execute normal join.
Here's what happens the way I understand it. The two statements are probably executed after another:
The first statement selects a few entries from the bkpf table. These entries are stored in the internal table it_bkpf (say belnr 1, 2, 3).
Each of these entries is then used as part of the select #2. The "for all entries" matches the belnr in table bseg to those in the internal table it_bkpf from the first statement. The matching entries are then put into the internal table it_bseg.
With the example you've given this is pretty much the same if the where clause in SQL #2 was where belnr in s_belnr (instead of the whole for all entries). This would only make sense if you needed the contents of it_bkpf for some other purpose. Another typical situation is if you determine the contents of the internal table used in the for all entries clause with some program logic instead of reading it directly from the database.
One catch with "for all entries": Make sure the internal table in the for all entries is not empty - then the whole table in the from clause would be selected.