How to shorten paths when accessing linked-server objects in SQL - sql

Is there a way to shorted access paths in SQL? I am thinking something similar to alias's but I don't know how to ask this question in google to get the appropriate application of alias'ing
This:
select * from ServerName.DBName.dbo.TableName
To:
declare #RDB as RemoteDatabaseObject
set #RDB = ServerName.DBName.dbo
select * from #RDB.TableName
I know this doesnt work, but I want to know if there is a way to alias objects that have long paths.

Maybe you are looking for SYNONYM?
http://msdn.microsoft.com/en-us/library/ms177544.aspx

Aliasing works for what you're wanting here.
SELECT * FROM ServerName.DBName.dbo.TableName AS myAlias
The cool thing is that you can almost always use AS in the middle of sql statements.
http://www.w3schools.com/sql/sql_alias.asp

Other than using dynamic SQL, I dont think of any way which makes this possible.

Related

PLSQL to SQL - DatabaseLink Select

straight forward question, but interesting enought, I didn't find anything. Probably I'm searching for the wrong keywords:
We have 2 Databases, one Oracle, one SQL, connected via Links.
I'd like to check, if some data is in the Oracle-Part, but not in the SQL one.
Selecting it from the PLSQLDev is pretty straightforward:
SELECT * from Core.Event#Link
But as soon as I try to select specific fields like :
SELECT Id from Core.Event#Link
It tells me the qualifier is invalid.
I tried all shennanigans like alias select:
SELECT ie.Id from Core.Event#Link ie
But it keeps telling me the qualifier is invalid.
Is there a special syntax I have to keep in mind?
Thanks in advance and a good weekend.
Matthias

Operations on selected items

I want to have query like this:
SELECT
sum(data_parts.size) as size_sum,
(size_sum/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.some_id='1';
Of course it won't work, because there's no column named size_sum, so my question is:
Is there a way to use size_sum as a parameter in next select item?
Other than using a subquery containing your current query (see Davide's answer), I don't think there is a way to do that.
But you could always do:
SELECT
sum(data_parts.size) as size_sum,
(sum(data_parts.size)/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.id='1';
Postgres is smart enough to only get that sum once. Only if this query will be greatly expanded with more calculations being done on size_sum would I recommend the subquery approach. Not because it works better, but because it will be easier to read. But if the current calculation is all you need, don't bother with a subquery. It would actually be less easy to read.
yes, a (somewhat) ugly way of making the query run there is...
SELECT
SIZE_SUM,
SIZE_SUM/2 AS HALF_OF_SIZE_SUM
FROM (
SELECT
sum(data_parts.size) as size_sum)
FROM
data_parts
WHERE
data_parts.id='1') X;
But I don't think there is a way on postgre to do operations directly based on the previous select fields
No, and too unnecesary to use subselects for this, simply use SUM(data_parts.size) again.
I'm not sure whether I catch your point.
Do you want the code like this?
SELECT
sum(data_parts.size) as size_sum,
(sum(data_parts.size)/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.some_id='1';

Is this possible with the SELECT LIKE specification?

Looking at this link: SQL SELECT LIKE
What if you were searching for a name that starts with H and ends with dinger?
Would I use:
SELECT NAME LIKE
'H_dinger'
'H...dinger' or
'H%dinger' ?
I'll assume H_dinger would think there is only 1 character in between, but I don't know what it is -- so I'm searching for it.
H...dinger isn't valid.
And H%dinger seems like it would check it all, but on the site, that isn't even listed?
You would use %, which is the variable-sized wildcard.
But you need to get the syntax right, such as with:
select NAME from TABLE where NAME like 'H%dinger'
Keep in mind that queries using % may be a performance issue (depending on how it's used and the DBMS engine). It can prevent the efficient use of indexes to speed up queries. It probably won't matter for small tables but it's something to keep in mind if you ever need to scale.

Search in every column

I'm building an abstract gem. i need a sql query that looks like this
SELECT * FROM my_table WHERE * LIKE '%my_search%'
is that possible?
edit:
I don't care about querys performance because it's a feature function of a admin panel, which is used once a month. I also don't know what columns the table has because it's so abstract. Sure i could use some rails ActiveRecord functions to find all the columns but i hoped to avoid adding this logic and just using the *. It's going to be a gem, and i can't know what db is going to be used with it. Maybe there is a sexy rails function that helps me out here.
As I understand the question, basically you are trying to build a sql statement which should check for a condition across all columns in that table. A dirty hack, but this generates the required Sql.
condition_string = MyTable.column_names.join(' LIKE ? OR ')
MyTable.all(:conditions => [condition_string, '%my_search%'])
However, this is not tested. This might work.
* LIKE '...' isn't valid according to the SQL standards, and not supported by any RDBMS I'm aware of. You could try using a function like CONCAT to make the left argument of LIKE, though performance won't be good. As for SELECT *, it's generally something to be avoided.
No, SQL does not support that syntax.
To search all columns you need to use procedures or dynamic SQL. Here's another SO question which may help:
SQL: search for a string in every varchar column in a database
EDIT: Sorry, the question I linked to is looking for a field name, not the data, but it might help you write some dynamically SQL to build the query you need.
You didn't say which database you are using, as there might be a vendor specific solution.
Its only an Idea, but i think it worth testing!
It depends on your DB you can get all Columns of a table, in MSSQL for example you can use somethink like:
select name from syscolumns where id=object_id('Tablename')
Under Oracle guess its like:
select column_name from USER_TAB_COLUMNS where TABLE_NAME = 'Tablename'
and then you will have to go through these columns usign a procedure and maby a cursor so you can check for each Column if the data your searching for is in there:
if ((select count(*) from Tablename where Colname = 'searchingdata') > 0)
then keep the results in a separated table(ColnameWhereFound, RecNrWhereFound).
The matter of Datatye may be an Issue if you try to compare strings with numbers, but if you notice for instance under SQL-Server the syscolumns table contains a column called "usertype" which contains a number seems to refer to the Datatype stored in the Columne, like 2 means string and 7 means int, and 2 means smallint, guess Oracle would have something similar too.
Hope this helps.

Using (-) in fields

I'm stuck on my application. I have named my table and fields name using ( - ) as glue instead of using ( _ ). Since i love the - method i was figuring out how to keep it.
So i fixed my sql queries's table as follow:
First:
SELECT * FROM cool-table
Then:
SELECT * FROM `cool-table`
And it worked. But now with the fields name i don't really know hot to make this work:
SELECT * FROM `cool-table` WHERE cool-id = 1
The error i'm getting is related to the - of cool-id.
Any help?
EDIT
I swear i've tried to put
`cool-id`
but it seemed not to work for me. Now it does. So sorry for my stupid question.
Do
SELECT * FROM `cool-table` WHERE `cool-id` = 1
Note the single ticks (called identifier quotes, at least by me) added to the column name.
PS. This is a really bad idea, but you probably know that already.
Have you tried:
SELECT * FROM `cool-table` WHERE `cool-id` = 1
I'm pretty sure this should work.
You should be able to use the backtick just like you would for the table name, like so:
SELECT * FROM `cool-table` WHERE `cool-id` = 1
Although MySQL allows hyphens in identifiers, standard SQL doesn't. By using hyphens, you pretty much guarantee that
a lot of applications simply won't work with your data,
you won't be able to move to another platform should you need to,
and anyone who uses your app will have to install MySQL.
Don't do it.