The "*" is not working in SQL Server 2016 - sql

I have a SQL query written in SQL Server version 2000. The query is not running in SQL Server 2016. The query is like below.
Select *
from ProjPace2 P, ProjPace2 P2
where P.DivCode *= P2.DivCode
and P.ProjGrp *= P2.ProjGrp
and P.ProjYr *= P2.ProjYr
and P.T_D *= P2.T_D
and P.Qtr *= P2.Qtr
and P.SRA_LRA *= P2.SRA_LRA
and P.District *= P2.District
and P.PICompany *= P2.PICompany
and P.ContCode *= P2.ContCode
and P.dtWkEnding > dateadd(dd,-1,'1/1/2015')
and P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')
I am getting the following error:
Msg 4147, Level 15, State 1, Line 20
The query uses non-ANSI outer join operators ("*=" or "=*"). To run this query without modification, please set the compatibility level for current database to 80, using the SET COMPATIBILITY_LEVEL option of ALTER DATABASE. It is strongly recommended to rewrite the query using ANSI outer join operators (LEFT OUTER JOIN, RIGHT OUTER JOIN). In the future versions of SQL Server, non-ANSI join operators will not be supported even in backward-compatibility modes.
I can understand the error is occurring due to "*" and I want to replace it with Left outer join so I can get the same result.
Any help will be thankfully accepted.
Partha

All conditions that are specified with *= operator denote ON clause for LEFT OUTER JOIN. So equivalent query would become:
Select *
from ProjPace2 P
left outer join ProjPace2 P2 on
P.DivCode = P2.DivCode
and P.ProjGrp = P2.ProjGrp
and P.ProjYr = P2.ProjYr
and P.T_D = P2.T_D
and P.Qtr = P2.Qtr
and P.SRA_LRA = P2.SRA_LRA
and P.District = P2.District
and P.PICompany = P2.PICompany
and P.ContCode = P2.ContCode
where P.dtWkEnding > dateadd(dd,-1,'1/1/2015')
and P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015')
One note though: Since you have condition where returned rows must have P2.dtWkEnding between dateadd(dd,-10,'1/1/2015') and dateadd(dd,-3,'1/1/2015') there is no need for LEFT OUTER JOIN since rows without matching P2 record will never be returned. So for this query you should use INNER JOIN.

Related

Legacy Query Sybase to TSQL MSSQL migration with multiple left join with or operator

I would like to convert this sybase legacy sql code to ansi tsql new standard for ms sql, but I can't find the right way. Any help would be appreciated.
SELECT 3 FROM x_linea_cred, x_linea_cred_priv, x_clt_prd
WHERE x_clt_prd.r_client = #rclient AND
(x_clt_prd.nro_prod *= x_linea_cred.nro_prod or
x_clt_prd.nro_prod *= x_linea_cred_priv.nro_prod))
For a while now, the standard notation for outer join is LEFT [OUTER] JOIN. Your query should be converted to:
select 3
from x_clt_prd c
left join x_linea_cred lc on lc.nro_prod = c.nro_prod
left join x_linea_cred_priv lcp on lcp.nro_prod = c.nro_prod
where c.r_client = #rclient

SQL Server : multi-join with tuple IN clause

I'm trying to join 4 tables that have a somewhat complex relationship. Because of where this will be used, it needs to be contained in a single query, but I'm having trouble since the primary query and the IN clause query both join 2 tables together and the lookup is on two columns.
The goal is to input a SalesNum and SalesType and have it return the Price
Tables and relationships:
sdShipping
SalesNum[1]
SalesType[2]
Weight[3]
sdSales
SalesNum[1]
SalesType[2]
Zip[4]
spZones
Zip[4]
Zone[5]
spPrices
Zone[5]
Price
Weight[3]
Here's my latest attempt in T-SQL:
SELECT
spp.Price
FROM
spZones AS spz
LEFT OUTER JOIN
spPrices AS spp ON spz.Zone = spp.Zone
WHERE
(spp.Weight, spz.Zip) IN (SELECT ship.Weight, sales.Zip
FROM sdShipping AS ship
LEFT OUTER JOIN sdSales AS sales ON sales.SalesNum = ship.SalesNum
AND sales.SalesType = ship.SalesType
WHERE sales.SalesNum = (?)
AND ship.SalesType = (?));
SQL Server Management Studio says I have an error in my syntax near ',' (appropriately useless error message). Does anybody have any idea whether this is even allowed in Microsoft's version of SQL? Is there perhaps another way to accomplish it? I've seen the multi-key IN questions answered on here, but never in the case where both sides require a JOIN.
Many databases do support IN on tuples. SQL Server is not one of them.
Use EXISTS instead:
SELECT spp.Price
FROM spZones spz LEFT OUTER JOIN
spPrices spp
ON spz.Zone = spp.Zone
WHERE EXISTS (SELECT 1
FROM sdShipping ship LEFT JOIN
sdSales sales
ON sales.SalesNum = ship.SalesNum AND
sales.SalesType = ship.SalesType
WHERE spp.Weight = ship.Weight AND spz.Zip = sales.Zip AND
sales.SalesNum = (?) AND
ship.SalesType = (?)
);

ERROR at line 3: ORA-00933: SQL command not properly ended

I am running the below SELECT statement to:
Return prices from a standard price list customer = 0
If the customer has been quoted a special price customer = X then use that price instead
I am getting the error message:
ERROR at line 3:
ORA-00933: SQL command not properly ended
Oracle version is: Oracle8i Enterprise Edition Release 8.1.7.4.0 - Production.
SELECT glas_daten_basis.idnr, glas_daten_basis.gl_bez, NVL(p2.zum2, p1.zum2)
FROM glas_daten_basis
JOIN os_przu p1 ON p1.idnr = glas_daten_basis.idnr
LEFT JOIN os_przu p2 ON p2.idnr = glas_daten_basis.idnr AND p2.kunr = 63
WHERE p1.kunr = 0;
Line 3 is the JOIN, is there something wrong here?
Update: There are 137 rows in the standard price list, so I should be given 137 rows regardless of whether the price is from customer = 0 or customer = X. The answers so far give me a ~60 rows for some reason.
SELECT os_przu.idnr, os_przu.zum2
FROM os_przu
WHERE os_przu.kunr = 0;
...
137 rows selected.
As #a_horse-with_no_name said, ANSI joins don't work in 8i; they weren't added until 9i. So if you're really stuck on this ancient and unsupported version you're stuck with the old Oracle-specific syntax:
SELECT glas_daten_basis.idnr, glas_daten_basis.gl_bez, NVL(p2.zum2, p1.zum2)
FROM glas_daten_basis, os_przu p1, os_przu p2
WHERE p1.idnr = glas_daten_basis.idnr
AND p1.kunr = 0
AND p2.idnr (+) = glas_daten_basis.idnr
AND p2.kunr (+) = 63;
Which is pretty similar to #nelucon's answer, except that only had one (+) left-join marker and it was on the wrong side of the condition.
SQL Fiddle.
The (+) is the Oracle-specific outer-join operator, and it has to be applied to each condition for the outer-joined table - if one is missed then the rest are ignored and it effectively becomes an inner join again. (One of the reasons ANSI joins are easier to work with, though you can still get that wrong by referring to the joined table in the where clause as well as the on.)
something like this, i didn't test the code but you got it. (+) means the left join. for more infos, google for "oracle 8 left join"
SELECT glas_daten_basis.idnr, glas_daten_basis.gl_bez, NVL(p2.zum2, p1.zum2)
FROM glas_daten_basis, os_przu p1, os_przu p2
WHERE p1.kunr = 0
AND p1.idnr = glas_daten_basis.idnr
AND p2.idnr = glas_daten_basis.idnr (+)
AND p2.kunr = 6;

*= and =* sql operators

There are
*= and =*
sql equalities in my application queries. What does it mean? I have investigated on google but i could not find sufficient information. For example :
SELECT ODR_YO_ID, OGR_OKUL_NO, OGR_ADI+' '+OGR_SOYADI OGR, COUNT(ODR_YO_ID) SAYI
FROM OGRENCI,YIL_SUBE,YIL_OGRENCI, OGRENCI_DAVRANIS
WHERE ODR_OLUMLU = 1 AND YO_OGR_ID = OGR_ID AND ODR_YO_ID = YO_ID AND YO_AKTIF = 1 AND
YO_YSB_ID = YSB_ID AND YSB_ID = 2183 AND YSB_YIL_KOD *= ODR_YIL_KOD AND ODR_OGR_ID =* OGR_ID
GROUP BY ODR_YO_ID, OGR_OKUL_NO, OGR_ADI+' '+OGR_SOYADI, YO_OKUL_DEGIS_TARIHI
ORDER BY OGR
When i execute this sql query in my local computer, i take this error:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '*='.
But i run same query in related server, this query works without any error.I use sql server 2012 express edition in my local computer and there is sql server 2005 in related server. Why ?
Thanks in advance.
*= was the ANSI-89 syntax for a left join. It is now deprecated which is why you get the error.
Similarly =* is analogous to right join.
You need to move the join clauses from the where clause to the join syntax, but without knowing which tables the fields come from, I can't be more specific
ie
YSB_YIL_KOD *= ODR_YIL_KOD
becomes
ysbtable
left join odrtable
on YSB_YIL_KOD = ODR_YIL_KOD
and
ODR_OGR_ID =* OGR_ID
becomes
odrtable
right join ogrtable
on ODR_OGR_ID = OGR_ID
You may be able to make the query work on your local machine by changing the database compatibility level ( http://technet.microsoft.com/en-us/library/bb510680.aspx ) but I would advise rewriting it to the current syntax.

hibernate - HQL joins on many clauses

I've been reading Hibernate documentation, but I haven't found anything that would explain how to do the following.
I have the following SQL code that I'm trying to convert to HQL:
SELECT {msg.*}, {cmd.*}
FROM Schema.Messages AS msg
LEFT OUTER JOIN schema.send_commands AS cmd
ON cmd.message_key = msg.unique_key
AND ( lower(cmd.status) IN (lower('failed') ) )
WHERE msg.sequence_received < 10";
The mainissue I'm having is that I'm unable to have two clauses on a LEFT OUTER JOIN. HQL allows me to have
ON cmd.message_key = msg.unique_key
, but how do I add the
AND clause 2?
You can add extra join conditions using with keyword, something like this (depends on your mapping):
SELECT m, c
FROM Message m LEFT JOIN m.commands c WITH (lower(c.status) = 'failed')
WHERE m.sequenceReceived < 10
See also:
16.3. Associations and joins