SQL request Exceptions from * - sql

If i create a sql database request with a * to get everything about one entry what do i have to do to get everything but one
for example: i want to have all information about the table article (ANR, BNR, BEZ, PC)(i don't want to have PC in it) so i start to write my request like this
SELECT a.*
FROM article a

In a select you can use * or specify all the columns you need, there's no way to extract everything but one:
SELECT ANR, BNR, BEZ
FROM article
Consider that using the *, even if it's shorter to write, may be dangerous, so I would only use it in simple check queries, and not in code to deploy.

Unfortunately there isn't a way to say 'select everything except...'.
I tend to go to the table in the object explorer, right click it and 'script as...' then choose 'SELECT To...' and new query window. It will then list all of the columns so you can remove the ones you do not want to select.
I should add, the above is only relevant for MSSQL and SSMS.

Related

How to quickly generate SELECT statement for a table in DataGrip?

In Microsoft SQL Server Management Studio (SSMS), you can right-click on a table, then go to Script Table as -> SELECT To, and then choose a destination for the generated script.
Is there anything similar in DataGrip, or can one be custom created in some way?
The reason I find this useful is because I often find that I'm working with a table with a lot of columns, and I want to select all but a few of them. So it's easier to just have it generate the SELECT statement with all the columns explicitly listed out, so that I can just go through and delete the ones I don't want.
Currently, my workaround in DataGrip is to right-click the table, then choose Copy DDL. This generates the CREATE TABLE statement, which lists out all the columns, but it also includes the column definitions. So I have to do a regex replace or run a macro to get rid of the extra info, which is kind of a pain. Does anyone have a better solution?
In DataGrip as other intellij-based IDEs everything is about source editing. So that can be achieved that way:
Open console
Start typing sel, you'll get completion popup (if not, hit Ctrl+Space)
Select sel here, which is live-template for select statement
Select statement will be generated, asking for table name and column list
Select desired table from completion, as column list provide *
Then hit Alt+Enter on asterisk and select Expand column list
I suggest you to look through https://www.jetbrains.com/datagrip/features/
In DataGrip 2018.3 you can use postfix completion. This is the flexible way to get needed queries.
Try typing
SELECT %table_name%.from
SELECT %table_name%.afrom
SELECT %table_name%.join
And this will be expanded to the needed queries. In the case of from completion you'll be able to write columns.
This makes writing SQL more logical: first, you point table, then columns.
See gif:

How to edit SQL records using Select statement

I got like 2000 rows of data, when I do select statement I can narrow my query but I cant change any value, as I have to change 50 columns, I dont want to use Update Command as then because values I have to update for each row is unique.
Anyone knows any easier way of selecting data using select command and then edit it.
EDIT
I just went to SQL Management studio, clicked on DB I am working on then find the table, I right clicked on it and then it says "Select top 1000 rows" so Now I can see the query and the data, I added "Where" into query and got what I wanted, but I cant modify the table rows below :S
*Edit 2 *
Or I can develop a Utility that will take Table Name , Column Name and its New value and simply updates it :)
You modify data using an UPDATE statement (with a WHERE clause) in a query window. While Management Studio has a feature called "Edit Top n Rows" that doesn't mean it's a good idea to use it - there are several behavioral bugs that are still unresolved even in the SQL Server 2012 version, and it can also place unnecessary and prohibitive locks on the underlying table.
I know it's not the answer you want to hear, but please become comfortable with proper DML commands. The documentation for UPDATE is found here:
http://msdn.microsoft.com/en-us/library/ms177523(v=sql.100).aspx
The long and short of it, IMHO: If you can identify the rows you want to update by using a SELECT with a WHERE clause, you can also write an UPDATE query using the same WHERE clause.
In SSMS - in the same place you saw "Select top 1000 rows", there should be another option "Open table" or "Edit top 200 Rows" (by default).
If you did that on a table, then pressed the following button:
You should then by able to paste in your select statement you've already written instead of what is there, press the execute button (Red exclamation mark button). The grid that appears is editable.
In general I'd be inclined to agree with #AaronBertrand though, it's better to learn the syntax to Update statements yourself.
If you have Microsoft Access, you can create a linked table to your sql server database and edit it by opening the table and modifying the relevant rows. You can create a query in Microsoft Access as well limiting the rows you want to return, and then you can inline edit the columns you'd like to change in the result set.
See for example here how to create a linked table to SQL Server.

Where is the table/view script generated and how to get it?

Say I have written a Create Table script in a query window and run it. So the table got created. Now, where is this script file being generated (system table). I mean if I do a
select * from sys.syscomments
I will get the script for stored procedure or function in the "Text" column. Likewise any way of getting the same for table or view?
Any DMV etc...
Thanks in advance
I'm not sure where the script is stored, but if you're looking to be able to view the scripted language to create the table, in SQL2008 R2 (and I'm pretty sure SQL2008) SSMS can generate the script on the fly. Just select your Table (or View, SP, etc...) and right click. From the context menu, choose Script Table as Create To (or whatever other modification you choose from the list). Then you have a choice of output locations-the New Query Editor Window is probably easiest to see your results. From there you have the base language for that table's creation and you can modify or save it from there.
below query is also works for views
select * from sys.syscomments
For getting script of tables , you have to
Right Click on database >> tasks >> Generate Scripts >> choose objects (select tables)
I think that is the possible way of getting table scripts.
For views you can use the column VIEW_DEFINITION in:
SELECT *
FROM INFORMATION_SCHEMA.VIEWS
I don't think there is an equivalent for tables because tables don't require a stored definition because the table itself is the definition. If you need to copy table structures then the best method is probably to use:
SELECT *
INTO NewTable
FROM OldTable
WHERE 0 = 1
You could try generating your own scripts using the system views and creating dynamic SQL, something like this would get you started
This does not create constraints, and there maybe things I've missed, or cause it to break but the general gist is there. You could also do this outside of SQL as demonstrated quite nicely In this Answer.
HOWEVER I should add I do not condone the use of these methods. There are few scenarios I can think of where it would be necessary to programatically copy a tables structure in this fashion. If it is necessary to copy structures as a one off you'd be better off doing this using the generate script functions that are built into SSMS.

sql or trick to search through whole database

is there a way to actually query the database in a such a way to search for a particular value in every table across the whole database ?
Something like a file search in Eclipse, it searches accross the whole worspace and project ?
Sorry about that .. its MS SQL 2005
SQL Workbench/J has a built in tool and command to do that.
It's JDBC based and should also work with SQL Server.
You will need to use the LIKE operator, and search through each field separately. i.e.
SELECT * FROM <table name>
WHERE (<field name1> LIKE '%<search value>%') OR
(<field name2> LIKE '%<search value>%') OR
... etc.
This isn't a quick way though.
I think the best way would be to
1) programatically generate the query and run it
2) use a GUI tool for the SQL server you are using which provides this functionality.
In mysql you can use union operator like
(SELECT * from table A where name = 'abc') UNION (SELECT * from
table B where middlename = 'pqr')
and so on
use full text search for efficency
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Well, your best bet is to write a procedure to do this. But to give you some pointers you can use the INFORMATION_SCHEMA.Tables to get a list of all the tables in a given database and INFORMATION_SCHEMA.Columns to get a list of all columns. These tables also give you the datatype of columns. So you will need a few loops on these tables to do the magic.
It should be mentioned most RDBMSs nowadays support these schemas.
In phpmyadmin, go to your database, reach the search tab.
Here you will be able to select all of your tables and search through your entire db in one time.

PL/SQL Developer - ignore/limit large data in queries

In PL/SQL Developer v7.1.x, is there way way to ignore large data types in queries or the "Query Data" feature. For example: If you right click on table FOO, and select "Query Data" this will execute a SELECT * FROM FOO. If that table contains BLOB data the query will take a while to complete and temporarily lock up the application. This is especially problematic when querying remote databases (for obvious reasons).
I would like a way to tell PL/SQL Developer not to retrieve large data by default. I know there is a way to limit the ResultSet size but this doesn't do what I am looking for.
I could just select each column I wanted ignoring certain ones but then I couldn't use the "Query Data" feature.
Thanks.
No, the Query Data feature does one thing and one thing only - queries all the data.
What you might find useful is that you can drag the name of a table or view from the Browser into a SQL Window, choose "Select" from the menu that pops up, and it will generate a SELECT statement on the table with all the column names included - but does not execute the query straight away. You can then edit it however you like (e.g. comment out the LOB columns) before you run it.
I know that Toad has something like that built in, but I'm not aware of a PL/SQL Developer option that disables BLOBS.
The option you are left with, for now, is to simply select all the columns individually and truncate the blob.
ie:
select foo, bar, trunc(baz,100) from foo where ...
Create a View that doesn't contain the blob column or whatever columns you don't routinely want to look at.