SQL Server equivalent of MySQL's USING - sql

In MySQL, you can use the keyword USING in a join when you join on columns from different tables with the same name. For example, these queries yield the same result:
SELECT * FROM user INNER JOIN perm USING (uid)
SELECT * FROM user INNER JOIN perm ON user.uid = perm.uid
Is there an equivalent shortcut in SQL Server?

nope, have to use:
SELECT * FROM user INNER JOIN perm ON user.uid = perm.uid

No, SQL Server does not support this kind of shortcut.
I would like to point out that even if it did, shortcuts like these are NOT A GOOD IDEA. I recently worked on a database where the developers thought it would be a good idea to use the *= and =* shortcuts for RIGHT JOIN and LEFT JOIN. Good idea until someone upgraded the SQL Compatibility level to 90. Then it became an extremely Bad Idea.
So, learn from us. Shortcuts are bad. A little extra typing never killed anyone.

Also, I would add not to use wild characters, "*" in your select statement - explicitly specify the column name.

Related

SQL error Statement, Missing Operator

Is there anything wrong with this statement?
SELECT *
FROM Movies INNER JOIN
Sessions
ON Movies.MovieID=Sessions.MovieID INNER JOIN
Tickets
ON Sessions.SessionID=Tickets.SessionID;
When ever I run it on Access I get a Syntax error 'Missing Operator'.
Also are there any alternatives to Access that I can import data from an excel spread sheet?
In general, no. In MS Access, yes. It likes extra parentheses, probably because the database developers don't believe in readability:
SELECT *
FROM (Movies INNER JOIN
Sessions
ON Movies.MovieID = Sessions.MovieID
) INNER JOIN
Tickets
ON Sessions.SessionID = Tickets.SessionID;
You could enable OPENROWSET if you have a local instance of SQL, and install MDACs (I would install both x86 and x64 if you have a 64 bit pc). Below is a link to an article that will help you get setup. Also, be sure to run the management studio with elevated privileges.
How to enable Ad Hoc Distributed Queries
Below is how the query would look. In my example I use Excel 8.0 instead of 12 because the column names are addressable in my select statements for 8.
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 8.0;Database=C:\Temp\MyExcelDoc.xlsx;',
'SELECT * FROM [Sheet1$]')
You can export data from an excel spreadsheet into a number of formats. I find two of the best to be;
Comma Separated Values (CSV)
XML
In many cases, you can deal with the data directly from the Excell spreadsheet.
It really depends on what you want to do.
Your SQL query looks fine - but with Access you need to do the extra joins in brackets;
SELECT * FROM ((Movies
INNER JOIN Sessions ON Movies.MovieID=Sessions.MovieID)
INNER JOIN Tickets ON Sessions.SessionID=Tickets.SessionID)
;

SQL join using USING: <column name> is not a recognized table hints option

I have the following JOIN:
SELECT * FROM tableA INNER JOIN tableB USING (commonColumn)
I get an error:
"commonColumn" is not a recognized table hints option. If it is
intended as a parameter to a table-valued function or to the
CHANGETABLE function, ensure that your database compatibility mode is
set to 90.
The following instead works:
SELECT * FROM tableA INNER JOIN tableB ON tableA.commonColumn = tableB.commonColumn
The compatibility level in my case is set to 100 (SQL Server 2008), while, by the way, I am working with SQL Server 2012.
What am I doing wrong? I find it very difficult to find example of the use of the keyword USING, as it is almost impossible to do a relevant web search. Yet, it seems the right thing to use when the "joining columns" have the same name...
USING is not supported SQL Server syntax. It's not a reserved keyword, either, so the query engine is using that as a table alias.
It is an ODBC keyword, but those are handled somewhat differently. The engine won't always complain if you use them, but you're not supposed to use them anyways.
It is also listed as a possible future reserved keyword. It's common for new editions of SQL Server to add words to the core reserved list.
Personally, I don't see them adding NATURAL JOIN syntax support, even with USING. A lot of DBAs consider NATURAL JOINs problematic.
The USING keyword is used to specify the source data for MERGE statements (called <table source>) in the documentation.

How do I construct a cross database query in MySQL?

I've got two databases on the same server. The Google gave me some hints but there wasn't anything "official" that I could find. Could someone point me to the documentation that explains how to do this? An explanation using PHP would be useful as well. Thanks!
I've got two databases on the same server. ...How do I construct a cross database query in MySQL?
You access other databases on the same MySQL instance by prefixing the table with the appropriate database name. IE:
SELECT *
FROM this_database.table_1 t1
JOIN that_database.table_2 t2 ON t2.column = t1.column
Keep in mind
A query executes with the credentials of the authentication used to set up the
connection. If you want to query two tables simultaneously across two (or more)
databases, the user used to run the query will need SELECT access to all
databases involved.
Reference:
Identity Qualifiers
SELECT * FROM DB1.myTable1 AS db1, DB2.myTable2 AS db2
http://www.dottedidesign.com/node/14 provides the following example:
SELECT
arbogast.node.nid as anid,
mcguffin.node.nid as mnid,
arbogast.node.title as atitle,
mcguffin.node.title as mtitle
FROM arbogast.node, mcguffin.node
WHERE arbogast.node.nid = 1
AND mcguffin.node.nid = arbogast.node.nid;
Where arbogast and mcguffin are different databases.

Microsoft Access SQL query

Can someone please help me out with a query? In Microsoft SQL Server, I have the following query, which executes fine:
SELECT * FROM ControlPoint
INNER JOIN Project ON ControlPoint.ProjectID = Project.ProjectID
INNER JOIN Site ON Site.SiteID = Project.SiteID
WHERE Project.ProjectName LIKE '%Flood%'
My problem is, when I try to execute this on Microsoft Access, it gives me some kind of syntax error. It's been forever since I've used Access, but if I remember right, I think the joins need to be in parenthesis or something. Any help would be useful!
You will need some parentheses in addition to changing the wild cards:
SELECT * FROM (ControlPoint
INNER JOIN Project ON ControlPoint.ProjectID = Project.ProjectID)
INNER JOIN Site ON Site.SiteID = Project.SiteID
WHERE Project.ProjectName LIKE '*Flood*'
Note that the asterisk is used in the Access query window and with DAO, percent is used with ADO.
Access uses different wildcard patterns.
In your case, it will be - LIKE '?Flood?' (replace question mark with asterisk).
I don't know formatting codes to apply here so that it shows it correctly.
See the link for details - http://www.techonthenet.com/access/queries/like.php
The syntax error is caused because access uses " instead of ' as the string delimiter. As previously mentioned you will also need to change the % wildcards to *
Use this sintax in access never fails
(this is an example)
select * from customer, adress
where customer.id = adress.customerId

Access DB update one table with value from another

I'm trying to update all records in one table with the values found in another table.
I've tried many versions of the same basic query and always get the same error message:
Operation must use an updateable
query.
Any thoughts on why this query won't work in Access DB?
UPDATE inventoryDetails as idet
SET idet.itemDesc =
(
SELECT bomItemDesc
FROM BOM_TEMPLATES as bt
WHERE bt.bomModelNumber = idet.modelNumber
)
also tried this because I realized that since the second table has multiple model number records for each modelnumber - and I only need the first description from the first record found for each model number.
UPDATE inventoryDetails as idet
SET idet.item_desc =
(
SELECT TOP 1 bomItemDescription
FROM BOM_TEMPLATES as bt
WHERE bt.bomModelNumber = idet.modelNumber
)
...still getting the same error though.
You have to use a join
UPDATE inventoryDetails
INNER JOIN BOM_TEMPLATES ON inventoryDetails.modelNumber = BOM_TEMPLATES.bomModelNumber
SET inventoryDetails.itemDesc = [bomItemDesc];
Any thoughts on why this query won't work in Access DB?
The answer is, because ACE/Jet SQL syntax is not SQL-92 compliant (even when in its ANSI-92 Query Mode!).
I'm assuming yours is a scalar subquery. This construct is simply not supported by ACE/Jet.
ACE/Jet has its own quirky and flawed UPDATE..JOIN syntax, flawed because the engine doesn't force the JOINed values to be scalar and it is free to silently use an arbitrary value. It is different again from SQL Server's own UPDATE..JOIN syntax but at least SQL Server supports the Standard scalar subquery as an alternative. ACE/Jet forces you to either learn its quirky non-portable ways or to use an alternative SQL product.
Sorry to sound negative: the ACE/Jet engine is a great piece of software but UPDATE syntax is absolutely fundamental and the fact it hasn't been changed since the SQL-92 Standard really show its age.
try:
update idet
SET idet.itemDesc = bt.bomItemDesc
from inventoryDetails as idet
inner join BOM_TEMPLATES as bt
on bt.bomModelNumber = idet.modelNumber
This is how I would write it for SQL server. Hope Access understands the same command.