This query runs perfact in SQL but giving error in PHP - sql

I have one sql query like this :
select * from tbl_ServiceRequest SR left join tbl_Events e on s.SRId = e.SRId where CustomerId = 65 and convert(nvarchar(10),s.CreatedDate,120) > (select convert(nvarchar(10),OrganisationCycleDate,120) from tbl_OrganizationDetail where OrgId = (select OrgId from Organization))))
When i run it into the sql server 2008 than it runs perfact and gives the correct records.
But i am using PHP with zend framework and its giving this error in php :
[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ';'.
there is not any ';' in my query.
The problem is in the second condition. if i will remove the second condition than its not giving any error. Second condition is :
and convert(nvarchar(10),s.CreatedDate,120) > (select convert(nvarchar(10),OrganisationCycleDate,120) from tbl_OrganizationDetail where OrgId = (select OrgId from Organization))))
Its giving me this error when i try to download pdf of these records. I am using FPDF library for it. But it is error of SQL Server only.
Can anyone know how to solve this problem ?

Try this:
SELECT *
FROM tbl_ServiceRequest s
LEFT JOIN tbl_Events e ON s.SRId = e.SRId
WHERE CustomerId = 65
AND convert(nvarchar(10),s.CreatedDate,120) >
(SELECT convert(nvarchar(10),OrganisationCycleDate,120)
FROM tbl_OrganizationDetail
WHERE OrgId =
(SELECT OrgId
FROM Organization));
Renaming your tbl_ServiceRequest as s not SR I guess.

My Problem is solved. There isn't any problem with sql.
There is a problem with php.
I just replace ">" with > and "<" with < in my query and it runs perfactly.
Thanks a lot A.S. Roma for your help..

Related

Syntax error: Expected end of input but got identifier

I'm trying to run the following query but keep getting the same error
Syntax error: Expected end of input but got identifier "BD_Dictionary"
at [3:1]
My query is:
SELECT
MI.* except(period_date, entity_level, email_access, period_filter)
BD_Dictionary.BD_EMAIL, BD_Dictionary.AREA_LEAD, BD_Dictionary.BD_LEADER
FROM
`mi-trial-365509.Trial.MI` as MI
INNER JOIN
`mi-trial-365509.Trial.BD Dictionary` as BD_Dictionary
ON
A.bd_email = BD_Dictionary.BD_EMAIL
LIMIT
1000
Sorry my bad, i forgot to put a comma in between the table source in SELECT command.
my code is now like this:
SELECT
MI.* except(period_date, entity_level, email_access, period_filter),
BD_Dictionary.BD_EMAIL, BD_Dictionary.AREA_LEAD, BD_Dictionary.BD_LEADER
FROM
`mi-trial-365509.Trial.MI` as MI
INNER JOIN
`mi-trial-365509.Trial.BD Dictionary` as BD_Dictionary
ON
MI.bd_email = BD_Dictionary.BD_EMAIL
LIMIT
1000

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

Group_concat with Query builder laravel

Hello! , i have an issue with an sql Call on laravel 5.5 using query builder. when i do this
$result = DB::table(self::$TABLA_COMPONENTE)
->join(self::$TABLA_ARCHIVOS ,self::$TABLA_COMPONENTE.'.com_id','=',self::$TABLA_ARCHIVOS.'.com_id')
->select(self::$TABLA_COMPONENTE.'.*',DB::raw('group_concat('.self::$TABLA_ARCHIVOS.'.ar_url) as com_archivos'))
->where(self::$TABLA_COMPONENTE.'.com_id',$id)->first();
i get the following error
SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select componente.*, group_concat(archivos.ar_url) as com_archivos from componente inner join archivos on componente.com_id = archivos.com_id where componente.com_id = 2 limit 1)
this is the raw sql i get by using ->toSql()
This is the sql with ->toSql()
"select `componente`.*, group_concat(archivos.ar_url) as com_archivos from `componente` inner join `archivos` on `componente`.`com_id` = `archivos`.`com_id` where `componente`.`com_id` = ?
And it works fine on Phpmyadmin.
i also tried using Group by with no luck.
If you could help me with a solution I would be very grateful!
This is actually mysql problem not laravel problem. Try add
->groupBy(self::$TABLA_COMPONENTE . '.id')
to you query.
EDITED: something like this one :
$result = DB::table(self::$TABLA_COMPONENTE)
->join(self::$TABLA_ARCHIVOS ,self::$TABLA_COMPONENTE.'.com_id','=',self::$TABLA_ARCHIVOS.'.com_id')
->select(self::$TABLA_COMPONENTE.'.*',DB::raw('group_concat('.self::$TABLA_ARCHIVOS.'.ar_url) as com_archivos'))
->groupBy(self::$TABLA_COMPONENTE . '.id')
->where(self::$TABLA_COMPONENTE.'.com_id',$id)->first();
note : it is not testet

Sql Select Query Timeout

Hello guys i am working on an query and every thing is working fine except when i run a query more than 2-3 times this return timeout is there is something wrong in my query or this is server error kindly suggest me here is my query
SELECT
Category_Feature_Mapping.Feature_ID,
Category_Feature_Mapping.Category_ID,
Option_Table.Option_ID,
Option_Table.Title,
Feature_Table.Title AS featuretitle,
Feature_Table.Type
FROM
Category_Feature_Mapping
INNER JOIN Feature_Table
ON Category_Feature_Mapping.Feature_ID = Feature_Table.Feature_ID
INNER JOIN Option_Table
ON Feature_Table.Feature_ID = Option_Table.Feature_ID
WHERE
Category_Feature_Mapping.Category_ID = #catid
AND Feature_Table.Feature_ID=#feid
AND Feature_Table.Feature_ID not in (SELECT
Feature_ID
FROM
Vendor_Value_Table
WHERE
Vendor_ID=#venid)
GROUP BY
Category_Feature_Mapping.Feature_ID,
Category_Feature_Mapping.Category_ID,
Option_Table.Option_ID,
Option_Table.Title,
Feature_Table.Title,
Feature_Table.Type
try to append ";Connection Timeout=100" to you connectionstring for up your timeout
may by a question of performance, try to replace "not in" by "not exists" like this
AND not exists (
SELECT * FROM Vendor_Value_Table vvt
WHERE Feature_Table.Feature_ID =vvt.Feature_ID and vvt.Vendor_ID=#venid
)

SQL query works even with wrong syntax

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