We are migrating our database to new SQL server version from SQL Server 2000 to SQL Server 2016. Some stored procedures use old syntaxes which represents different clauses in new SQL version. For eg:
for left joins, old server uses something like the below.
select 1 from table1 t1, table2 t2
where t1.ID *= t2.ID
New version of SQL would be
select 1 from table1 t1 left outer join table2 t2 on t1.ID = t2.ID
Similarly, I am looking for a corresponding new syntax for the below code used in SQL server 2000
select 1 from table1 t1 inner join table2 t2 on (t1.Field1 = t2.Field1 and. t1.Field2 = t2.Field2)
The above query executes without error in 2000 server but in 2016 version it shows error in the place where and. is mentioned.
Thanks for any help.
Related
Here's sample SQL Full Outer Join code from w3schools:
SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Testing it in online validators (https://www.piliapp.com/mysql-syntax-check/ and https://www.eversql.com/sql-syntax-check-validator/) throws an error:
You have an error in your SQL syntax; it seems the error is around: 'OUTER JOIN table2 ON table1.column_name = table2.column_name' at line 5
w3resource has this sample SQL code:
SELECT *
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Sample code found on SO is pretty much the same code and throws the same error in the validators and on my dev system.
Does that make any sense?
What is the correct SQL for this?
Those query are MSSQL Query not mysql and that two sites are mysql query validator.
You don't have FULL JOINS on MySQL, but you can sure emulate them.
sample code to emulate FULL OUTER JOIN on MYSQL :
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
WHERE t1.id IS NULL
I have 2 servers with 2 different databases.
S1(Server) WITH DB1(Database) WITH T1(Table)
and
S2(Server) WITH DB2(Database) WITH T2(Table)
DB1 has a linked server to S2 DB2.
So i can run a query such as
SELECT * FROM T1
JOIN [S2].[DB2].[T2] T2
ON T1.[ID] = T2.[ID]
And this runs correctly.
However, how can i run it in the opposite direction
e.g.
SELECT * FROM T2
JOIN [S1].[DB1].[T1] T1
ON T2.[ID] = T1.[ID]
This errors with "Could not find server [S1].[DB1].[T1]"
Is there anyway i can join in this direction without having to create a linked server from S2 to S1?
This query:
SELECT *
FROM T2 JOIN
[S1].[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
Presumes that T2 is local. You can sort of do a hybrid:
SELECT *
FROM [S2].[DB2].[T2] T2 JOIN
[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
It is possible to link a server to itself, so if you did that, you could run this identical query on both servers:
SELECT *
FROM [S2].[DB2].[T2] T2 JOIN
[S2].[DB1].[T1] T1
ON T2.[ID] = T1.[ID];
If you are trying to implement "equivalent" queries on two servers, then synonyms could help. You might be interested in the answers to this question.
Gordon's answer is more thorough but you can rewrite your second query as:
SELECT *
FROM [S2].[DB2].[T2] T2
JOIN T1
ON T2.ID = T1.ID
I noticed today that this query
select * from table1 table2 where column_from_table1 = ?;
works. It works the same as (same columns return)
select * from table1 where column_from_table1 = ?;
Shouldn't the former be a syntax error? What is it interpreting table2 as?
Appears it's interpreting it as renaming the table, even though table2 exists it happily allows the rename, this also works:
select * from table1 asdf where asdf.column_from_table1 = ?;
select * from table1 table2 where column_from_table1 = ?;
table2 is working as a table alias for table1. It's not being used as the name of an object in the database at all. The fact that a table named table2 exists is wholly irrelevant to this query. Usually you'd see something like this:
select t.id, t.name from table1 t where t.column_from_table1 = ?;
Some RDBMSs require the as keyword, so you'll also see this:
SELECT t.id, t.name FROM table1 AS t WHERE t.column_from_table1 = ?;
Table aliases are useful for making queries with multiple tables easier to write, especially if they have shared column names which need to be qualified. They're also essential for self-joins where a table is joined to itself.
Example of a join using aliases:
SELECT t1.Id,
t1.Name as t1_Name
t2.Name as t2_Name
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.id
WHERE t1.column_from_table1 = ?;
Or, for a self-join to look for duplicate Name values, for example:
SELECT t1.Name,
t1.Id
t2.Id as Dupe_Id
FROM table1 t1
JOIN table1 t2
ON t1.Name = t2.Name
WHERE t1.Id < t2.Id;
Notice that this query is referring to table1 twice and uses the aliases of t1 and t2 to differentiate which it's referring to.
Note that a comma join, such as FROM table1, table2 WHERE table1.id = table2.id is very old syntax that should be explicitly avoided when writing queries. The older syntax is difficult to read and maintain and doesn't support outer joins except by vender-specific extensions. The newer syntax with the JOIN keyword was introduced in standard SQL in 1992. There's no reason to still be using comma joins.
How can I join 2 tables from different DB with SQL Server?
Simply just use multipart identifier names if they are in the same server.
SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [database_2].[dbo].[table_2] t2
ON t1.id = t2.id
If not you can use linked servers and 4-part names.
SELECT *
FROM [database_1].[dbo].[table_1] t1
JOIN [linked_server].[database_2].[dbo].[table_2] t2
ON t1.id = t2.id
Do I have to create a stored procedure to call to get this working? I am no friend of adapters in Visual Studio (The GUI-ones that gets destroyed instantly if you edit them :) ).
However I have this query that I got working (SQL Management Studio 2008 R2) with using Outer Apply (Similar to Left join). My VS-adapter does not accept this throwing "The OUTER APPLY SQL construct or statement is not supported". I therefore need help writing the code below in "Normal" t-sql :)
SELECT DISTINCT t1.col1,t2.col2,t3.col2,t3.col4
FROM t1
OUTER APPLY
(
SELECT TOP 1 col1,col2,col3,col4
FROM t2
WHERE col3 = value
AND t2.col1 = t1.col1
ORDER BY col4 ASC
) AS t3
OUTER APPLY is equivalent to a LEFT JOIN, will the following code fit your needs?
SELECT DISTINCT t1.col1,t3.col2,t3.col4
FROM t1
LEFT JOIN
(
SELECT TOP 1 col1,col2,col4
FROM t2
WHERE col3 = value
ORDER BY col3 ASC
) t3
ON t3.col1 = t1.col1