Sql Select Query Timeout - sql

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
)

Related

How to convert SQL Query to CodeIgniter?

How to convert this query :
SELECT pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah
FROM pembelian_detail_tb
INNER JOIN produk_tb ON pembelian_detail_tb.kode_produk = produk_tb.kode_produk;
I have try many code, and i still got undefine.
Try this
$query = $this->db->select('pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah')
->from('pembelian_detail_tb')
->join('produk_tb', 'pembelian_detail_tb.kode_produk = produk_tb.kode_produk', 'inner')
->get();
I like to use Query Bindings once a join is involved in a query:
https://www.codeigniter.com/userguide3/database/queries.html
search page for "query bindings"
this escapes values for you of course and if you need to debug dump $this->db->last_query();

ORA-00936: missing expression Java SQL Exception

I´ve been trying to find the error in this statement for a few hours and can´t seem to find it.
It must have something to do with the AND connecting the two WHERE´s since when deleting the first WHERE it works:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA,
E_AUFMASS_KOMMENTARE.AUFTR_NR,
E_AUFMASS_KOMMENTARE.KOMMENTAR,
AUFTR_EXT.ART_GRUPPE
FROM HHNG_AU.E_AUFMASS_KOMMENTARE
INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR
WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' )
AND WHERE NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )
Too many WHERE. You only need where once, then use ANDs and ORs to combine conditions:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA, E_AUFMASS_KOMMENTARE.AUFTR_NR, E_AUFMASS_KOMMENTARE.KOMMENTAR, AUFTR_EXT.ART_GRUPPE FROM HHNG_AU.E_AUFMASS_KOMMENTARE INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' ) AND NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )

SQL join with 2 columns with same name

I have created this query:
SELECT *
FROM arrangement, booker
WHERE arrangement.arrangement_id = booker.arrangement_id
AND arrangement.dato BETWEEN '2017-09-29' AND '2017-14-10'
AND booker.dato > '2017-22-09 18:31:53'
AND arrangement.remind = '0'
Both arrangement and booker has a field called 'dato'. I try to use the 'dato' from booker as parameter.
I select 'arrrangement.dato between' that works fine
then I select 'booker.dato <'
However, the 'booker.dato <' fails and run in arrangement.dato when executing the query. Can anyone explain it to me?
use JOIN keyword with ON clause

Invalid syntax for SQL Server

I'm trying to execute the following query in SQL Server, but it's throwing an error. How can I fix it?
select
T.T_Email
from
Stu_Question S, Tutor_Answer T
where
S.S_Quest_Id = '4f7a1518-a765-40c0-ae53-3ee61eef6673'
and S.S_Quest_Id = T.S_Quest_Id
and (T_Email,T_Answer_Update_Status)
IN (T_Email, Select MAX(T_Answer_Update_Status)
from Tutor_Answer
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
group by T_Email)
and S.S_Quest_Update_Status = (Select MAX(S_Quest_Update_Status)
from Stu_Question
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673')
This is the offending part of your statement:
and (T_Email,T_Answer_Update_Status)
IN (T_Email, Select MAX(T_Answer_Update_Status)
from Tutor_Answer
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
group by T_Email)
What on earth are you trying to do here???
T-SQL's IN operator works on one column at a time - like this:
WHERE T_EMail IN (SELECT EMail FROM .....)
marc_s correctly pointed out the offending part of your query.
You'll have to try to convert that to a join instead. Here is how I would do it:
select T.T_Email
from Stu_Question S
join (select T_Email,
row_number() over (partition by S_Quest_Id order by S_Quest_Update_Status desc) as rn
from Tutor_Answer) T
on T.S_Quest_Id=S.S_Quest_Id
and T.rn = 1
where S.S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
AND S.S_Quest_Update_Status=(Select MAX(S_Quest_Update_Status)
from Stu_Question
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673')
Notice that you can definitely improve this further. But it should get you going.

This query runs perfact in SQL but giving error in PHP

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..