SQL query works even with wrong syntax - sql

I am running a SQL query in stored procedure which is like following
SELECT
t1.id,t2.Name
FROM
table1 t1 , table2 t2 ,table2 t3,table4 t4
WHERE
t1.id=t3.t4.id
this query gets executed on SQL server 2008 when its compatible with SQL server 2000 but if we turn OFF the compatibility with SQL server 2000 then this Query gives syntax error which is expected.
Can some one help me to understand why this is happeneing ? thanks in advance
Original query:
SELECT
ConfigID , LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG , Location_LOBTeam LOCLOB , Location LOC , LOBTeam LOB, System SRC
WHERE CONFIG.LocationLOBTeamID = LOC.LOB.LocationLOBTeamID
AND CONFIG.SourceSystemID = SRC.SystemID
AND LOCLOB.LocationID = LOC.LocationID
AND LOCLOB.LOBTeamID = LOB.LOBTeamID
AND (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode

I think that original query, with current standard join syntax applied would be this:
SELECT
ConfigID
, LocationDesc + '-' + LOBTeamDesc LocLOBTeamSource
FROM Config CONFIG
INNER JOIN Location_LOBTeam LOCLOB
ON CONFIG.LocationLOBTeamID = LOCLOB.LocationLOBTeamID
INNER JOIN Location LOC
ON LOCLOB.LocationID = LOC.LocationID
INNER JOIN LOBTeam LOB
ON LOCLOB.LOBTeamID = LOB.LOBTeamID
INNER JOIN [System] SRC
ON CONFIG.SourceSystemID = SRC.SystemID
WHERE (GETDATE() BETWEEN CONFIG.effectiveDate AND CONFIG.EndDate
OR CONFIG.EndDate IS NULL)
ORDER BY
LOC.LocationCode
Perhaps this will help.
+EDIT
"System" as a table name, could that be a problem? Suggest you try it as [System]
+EDIT2
The original is given with this: LOC.LOB.LocationLOBTeamID but that appears to be an error as there is an alias LOCLOB

I think below post from msdn answers this issue Compatibility Levels and Stored Procedures
in the above post the point number 3 under section "Differences Between Compatibility Level 80 and Level 90" states "WHEN binding the column references in the ORDER BY list to the columns defined in the SELECT list, column ambiguities are ignored and column prefixes are sometimes ignored. This can cause the result set to return in an unexpected order."
on my database I am using compatibility level 80 i.e 2000 thats why it runs smoothly with the given syntax but when I remove this compatibility and make it to 100 i.e. 2008/R2 script gives syntax error which is expected

Related

Openfire ORA-00907: Missing right parenthesis

I have the following problem retrieving messages from the OpenFire Monitoring Service plugin. I discovered that the error is due to an incorrect query in the database but I still cannot detect the error for which the query is not working correctly.
SELECT
fromjid,
fromjidresource,
tojid,
tojidresource,
sentdate,
body,
stanza,
messageid,
barejid
FROM
(
SELECT
DISTINCT ofmessagearchive.fromjid,
ofmessagearchive.fromjidresource,
ofmessagearchive.tojid,
ofmessagearchive.tojidresource,
ofmessagearchive.sentdate,
ofmessagearchive.body,
ofmessagearchive.stanza,
ofmessagearchive.messageid,
ofconparticipant.barejid
FROM
ofmessagearchive
INNER JOIN ofconparticipant ON ofmessagearchive.conversationid =
ofconparticipant.conversationid
WHERE
(
ofmessagearchive.stanza IS NOT NULL
OR ofmessagearchive.body IS NOT NULL
)
AND ofmessagearchive.messageid IS NOT NULL
AND ofmessagearchive.sentdate >= 0
AND ofmessagearchive.sentdate <= 1602748770287
AND ofconparticipant.barejid = 'usuario3#192.168.0.79'
AND (
ofmessagearchive.tojid = 'usuario4#192.168.0.79'
OR ofmessagearchive.fromjid = 'usuario3#192.168.0.79'
)
ORDER BY
ofmessagearchive.sentdate DESC
LIMIT
100
) AS part
ORDER BY
sentdate
I get an error when doing the following query
ORA-00907: missing right parenthesis
Command line error:32 Column: 9
There is no LIMIT keyword available in Oracle and if you are using Oracle 12c you can use FETCH FIRST 100 ROWS ONLY instead of it.
You cannot use AS to give alias to the sub query and it is not recognised by Oracle. So either you can remove the alias completely as you are not using it anywhere or just remove the AS and keep the alias name part only which should be fine.
Here is a good SO link about the Oracle limiting result set and you can always look into other sites available such as Oracle base or the official document as well. For 11g solution you have to use row_number

"Circular reference caused by ..." error in Access SQL (but not in T-SQL)

I have the following SQL statement which returns the desired result in SQL Server 2012:
SELECT
S.ONOMA
, S.DIEY
, S.POLH
, S.TK
, S.IDIOT
, S.KODIKOS
, S.AFM
FROM
SYNERG AS S
INNER JOIN
(SELECT
G.AFM, MIN(KODIKOS) AS KODIKOS
FROM SYNERG AS G
WHERE LEN(ISNULL(AFM, '')) != 0
GROUP BY AFM) AS I ON S.KODIKOS = I.KODIKOS
ORDER BY
S.AFM
but when I run the same SQL statement in MS Access 2007 I get an error:
Circular reference caused by 'KODIKOS' in query definition's SELECT list.
Any help would be appreciated.
As explained in the link by HansUp:
The alias of a calculated field cannot be identical to any of the field names used to calculate the field.
This can be rather annoying (esp. if it is a field that is returned by the query), but there is no way around it.
So you need to change the alias, e.g.:
SELECT
S.ONOMA
, S.DIEY
, S.POLH
, S.TK
, S.IDIOT
, S.KODIKOS
, S.AFM
FROM
SYNERG AS S
INNER JOIN
(SELECT
G.AFM, MIN(KODIKOS) AS MinKODIKOS
FROM SYNERG AS G
WHERE LEN(Nz(AFM, '')) <> 0
GROUP BY AFM) AS I ON S.KODIKOS = I.MinKODIKOS
ORDER BY
S.AFM
Note also that an IsNull() function exists in Access, but has a different meaning (it takes one argument and returns a Boolean). The corresponding function is Nz()
And (thanks #HansUp), the unequal operator is <>, not !=. I always use <> in SQL Server too, no need to make things more complicated than necessary. :)

SQL Server Compact won't allow subselect but inner join with groupby not allowed on text datatype

I have the following sql syntax that I used in my database query (SQL Server)
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, (SELECT COUNT(*) AS Expr1
FROM NieuwsbriefCommentaar
WHERE (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID
AND NieuwsbriefCommentaar.Goedgekeurd = 1)) AS AantalCommentaren
FROM Nieuwsbrief
I'm changing now to sql-server-ce (compact edition) which won't allow me to have subqueries like this. Proposed solution : inner join. But as I only need a count of the subtable 'NieuwsbriefCommentaar', I have to use a 'group by' clause on my base table attributes to avoid doubles in the result set.
However the 'Nieuwbrief.Brief' attribute is of datatype 'text'. Group by clauses are not allowed on 'text' datatype in sql-server-ce. 'Text' datatype is deprecated, but sql-server-ce doesn't support 'nvarchar(max)' yet...
Any idea how to solve this? Thx for your help.
I think that the solution could be easier. I don't know exactly how is your metadata but I think that this code could fit your requirements by simply using LEFT JOIN.
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, COUNT(NieuwsbriefCommentaar.NieuwsbriefID) AS AantalCommentaren
FROM Nieuwsbrief
LEFT JOIN NieuwsbriefCommentaar ON (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID)
WHERE NieuwsbriefCommentaar.Goedgekeurd = 1
Edited: 2ndOption
SELECT N.ID, N.Titel, N.Brief, N.NieuwsbriefTypeCode, G.AantalCommentaren FROM Nieuwsbrief as N LEFT JOIN (SELECT NieuwsbriefID, COUNT(*) AS AantalCommentaren FROM NieuwsbriefCommentaar GROUP BY NieuwsbriefID) AS G ON (N.ID = G.NieuwsbriefID)
Please, let me know if this code works in order to find out another workaround..
regards,

SQL "column ambiguously defined"- when run from hibernate

When I run a SQL query from sqldeveloper , it runs fine When I run the same query from Hibernate SQL session, it gives me: "ORA-00918: column ambiguously defined" error.
Dies hibernate generate any kind of sql logs that can be used to debug the error?
Edit: here is the sql:(works fine from sqldeveloper)
SELECT main_contact.cont_name,
sub_contact.cont_name,
main_contact.cont_role_desc,
main_contact.cont_start_dte,
main_contact.cont_end_dte,
main_contact.cont_id,
sub_contact.cont_id,
main_contact.lookup_desc,
main_contact.cont_role_desc
FROM
(SELECT cont_rlat.cont_rlat_id ,
cont_role.cont_role_desc ,
cont.cont_name ,
cont.cont_ty_cde ,
cont.cont_sid ,
cont.cont_id ,
cont_rlat.rlat_from_dte ,
cont.cont_start_dte ,
cont.cont_end_dte ,
cont_rlat.app_id ,
lookup_data_mgmt.lookup_desc
FROM cont_rlat join
cont on cont_rlat.cont_sid = cont.cont_sid
join cont_role on cont_rlat.cont_role_id=cont_role.cont_role_id
join app on cont_rlat.app_id = app.app_ID
join LOOKUP_DATA_MGMT on app.app_TY_CDE = LOOKUP_DATA_MGMT.LOOKUP_ID
where
app.app_id =:investmentProfileCId
) main_contact left join
(SELECT cont.cont_id,
cont.cont_name,
cont_sub_rlat.cont_rlat_id
FROM cont_sub_rlat join
cont on
cont_sub_rlat.individual_cont_id = cont.cont_sid
) sub_contact on
main_contact.cont_rlat_id = sub_contact.cont_rlat_id
You're selecting main_contact.cont_role_desc twice. Also, you have two columns called 'cont_name' and two columns called 'cont_id', which also might not make it happy. If dropping the extra cont_name doesn't work, maybe try aliasing those?
SELECT main_contact.cont_name,
sub_contact.cont_name AS sub_cont_name,
main_contact.cont_role_desc,
main_contact.cont_start_dte,
main_contact.cont_end_dte,
main_contact.cont_id,
sub_contact.cont_id AS sub_cont_id,
main_contact.lookup_desc
Neither of those will be a problem in SQLDeveloper, but I imagine that at the very least having two exact copies of main_contact.cont_role_desc in the result set will be confusing Hibernate -- how will it tell them apart?
in hibernate.cfg.xml to make it log sql statements
<property name="hibernate.show_sql">true</property>

Multi-part Identifier could not be bound - Update Query SQL Server 2005

SQL Server novice here.
UPDATE dbo.ObjectivesApproved
SET dbo.ObjectivesApproved.VAP = 'Y'
WHERE ((dbo.Approved.Cri_Group In ('X01' ,'X02' ,'X03' ,'X04' ,'X05' ,'X07' ,'X08' ,'X09' ,'X10' ,'X11' ,'X12' ,'X13' ,'X14')))
gives the following error
The multi-part identifier "dbo.Approved.Cri_Group" could not be bound.
What's causing the error?
Update: The above query was a result of trial and error. I'm updating an Access Application to SQL server, and having some trouble with the slightly different dialects of SQL
This is my original Query.
UPDATE dbo.Approved
INNER JOIN dbo.ObjectivesApproved ON dbo.Approved.ID = dbo.ObjectivesApproved.ID
SET dbo.ObjectivesApproved.VAP = 'Y'
WHERE ((dbo.Approved.Cri_Group
In ('X01' ,'X02' ,'X03' ,'X04' ,'X05' ,'X07' ,'X08' ,'X09' ,'X10' ,'X11' ,'X12' ,'X13' ,'X14')));
This gives the error - Incorrect syntax near the keyword 'INNER'
thanks
That would translate into
UPDATE
OA
SET
OA.VAP = 'Y'
FROM
dbo.Approved AS A
INNER JOIN dbo.ObjectivesApproved OA ON A.ID = OA.ID
WHERE
A.Cri_Group IN ('X01' ,'X02' ,'X03' ,'X04' ,'X05' ,'X07' ,'X08' ,'X09' ,'X10' ,'X11' ,'X12' ,'X13' ,'X14')
You are not specifying that the update also uses the Approved table anywhere, instead you just go ahead and use one if its columns.
On another note, your update looks logically flawed too as you'll update the ObjectivesApproved records irrespective of what they contain, i.e. there is no relation mentioned between ObjectivesApproved and Approved.