attempt to index global variable OE (a nil value) Lua with sql - sql

mk = conn:query({live = true, sql = "SELECT DISTINCT OE.OderNumber,(SELECT R.ReportBody + ' CRLF ' +'Modified By: ' + UM.FirstName + ' at ' + UM.LastName + ' CRLF '+ 'Authored By: ' + UC.FirstName + ' at ' + UC.LastName + ' CRLF ' +'CRLF' AS [text()] FROM dbo.OrderReports R INNER JOIN dbo.Orders O ON R.OrderId = O.OrderId INNER JOIN dbo.Users UM ON R.ModifiedById = UM.UserId INNER JOIN dbo.Users UC ON R.CreatedById = UC.UserId WHERE O.OrderNumber = '"..OE.OderNumber.."' ORDER BY R.CreateDateFOR XML PATH('')) 'OrderReport' FROM dbo.vw_OrderLevelEvents OE"})
This query works just fine in SQL but while I have it with Lua, it says attempt to index global variable OE(a nil value)
How can i fix this

From your code :
WHERE O.OrderNumber = '"..OE.OderNumber.."' ORDER BY R.CreateDateFOR
Here you're using the OderNumber key of the OE table and its value is nil. You'll need to fix that before using its key.
EDIT : you really should rename it to OrderNumber, too! ;)

Related

ORA-00920 [42000]: invalid relational operator

Here is the SQL Source Code for database selection that generates the following
error: "Query execution failed, Reason: SQL Error [920] [42000]:
ORA-00920: opérateur relationnel non valide.
Please Help!
WITH adr_siege as (
SELECT Nvl(adr.libadr, ' ') AS libadr, Nvl(adr.adress, ' ') AS adress,
Nvl(adr.codpos, ' ') AS codpos, Nvl(adr.locali, ' ') AS locali,
Nvl(adr.codpay, ' ') AS codpay
FROM gnx.tie
JOIN gnx.adr
ON adr.codsoc = tie.codsoc
AND adr.typtie = tie.typtie
AND adr.sigadr = tie.sigtie
AND typadr = 'COM'
WHERE tie.typtie = 'DEP'
AND tie.codsoc=1
AND tie.sigtie = '00000137'
)
SELECT Nvl(a.codpro, ' ') AS codpro, Nvl(a.nompro, ' ') AS nompro,
Nvl(ll.prix_achat, 0) AS prix_achat, Nvl(ll.qte, 0) AS qte,
Nvl(l.nom_fournisseur, ' ') AS nom_fournisseur, Nvl(m.raison, ' ') AS raison,
Nvl(concat(m.adresse1,' ' || m.adresse2), ' ') AS adresse_mag,
Nvl(m.cp, ' ') AS cp, Nvl(m.ville, ' ') AS ville,
Nvl(p.libelle, ' ') AS libelle,
Nvl(To_Char(c.date_validation, 'DD/MM/YYYY'), ' ') AS date_validation,
Nvl((
SELECT sum(fll.prix_achat * fll.qte)
FROM fourniture.frnt_livraison_ligne fll
WHERE fll.noliv
), 0) AS total_prix,
To_Char(SYSDATE, 'DD/MM/YYYY') AS date_edition,
Nvl(f.libadr, ' ') AS fou_lib_adr,
Nvl(f.tel, ' ') AS fou_tel,
Nvl(f.adress, ' ') AS fou_adr, Nvl(f.codpos, ' ') AS fou_cp,
Nvl(f.locali, ' ') AS fou_ville, Nvl(pf.libelle, ' ') AS fou_pays,
Nvl(to_char(p.date_livraison_s1, 'DD/MM/YYYY'), ' ') AS date_livraison_s1,
Nvl(to_char(p.date_livraison_s2, 'DD/MM/YYYY'), ' ') AS date_livraison_s2,
Nvl((
SELECT tbl.lib1
FROM gnx.tie
LEFT JOIN gnx.tbl
ON tbl.codtbl = 'mrg'
AND tbl.lib1 IS NOT NULL
AND tbl.codsoc=1
AND tbl.cletbl = tie.modrgl
WHERE tie.typtie = 'FOU'
AND tie.sigtie = l.code_fournisseur
AND tie.codsoc =1
), ' ') AS mode_paiement,
l.num_sous_periode,
adr_siege.*,
Nvl(m.nummag, ' ') AS nummag,
Nvl(m.tel1, ' ') AS tel1,
l.ref_gnx,
Nvl(tva.taux_tva, 0)/100 AS taux_tva,
Nvl(f.code, ' ') AS code_fournisseur,
(
CASE WHEN (m.lundi_ouverture != '0' OR m.mardi_ouverture != '0' OR m.mercredi_ouverture != '0'
OR m.jeudi_ouverture != '0' OR m.vendredi_ouverture != '0' OR m.samedi_ouverture != '0') THEN 1
ELSE 0
END
) AS AFFICH_HORRAIRE,
a.conditionnement,
a.refpro as REFPRO
FROM adr_siege, fourniture.frnt_livraison l
INNER JOIN fourniture.frnt_livraison_ligne ll ON ll.noliv = l.noliv
INNER JOIN fourniture.frnt_article a ON a.codpro = ll.codpro
INNER JOIN fourniture.frnt_commande c ON c.nocde = l.nocde
INNER JOIN polymag.magasin m ON m.nummag = c.nummag
INNER JOIN polymag.pays p ON p.code = m.codepays
LEFT JOIN fourniture.frnt_fournisseur f ON f.code =l.code_fournisseur
INNER JOIN polymag.pays pf ON pf.code = f.codpay
INNER JOIN fourniture.frnt_periode p ON p.noperiode = c.noperiode
LEFT JOIN fourniture.frnt_tva tva ON tva.code_pays_fou = f.codpay AND tva.code_pays_mag = m.codepays
WHERE l.type_livraison = 'FOU'
(we are selecting in schema named 'fournisseur')
Thank you in advance for your help.
problem is at line 25 because of WHERE fll.noliv that's without any relational operator like <,>,!,= after this statement.
TOTAL_PRIX seems to be invalid; look at its WHERE clause:
(
SELECT SUM(fll.prix_achat * fll.qte)
FROM fourniture.frnt_livraison_ligne fll
WHERE fll.noliv ), 0) AS total_prix,

Get specific columns from xml path

I have this query here:
SELECT
T.AKSIONERET,
sd.Identification_No, sd.Date_Of_Incorp,
sd.Ownership_Desc, sd.Ownership_Code,
sd.Subject_Name_Code, sd.Subject_Name,
sd.Subject_Type_Code,
LEFT(sd.Object_Desc, 3999) AS Object_Desc,
sd.Subject_Status_Name, sd.Subject_Name,
nr2.NACE_Code, nr2.NACE_PARENT, nr2.Description,
sd.Subject_Type_Name, sd.Subject_Status_Name,
sd.Subject_Type_Name, f.Place_Of_Registration, sd.Object_Desc
FROM
NRC_Subject_DEF AS sd
LEFT JOIN
NRC_Nace_REV2 AS nr2 ON nr2.Subject_DEF_Code = sd.Subject_DEF_Code
LEFT JOIN
NRC_ForeignCompany AS f ON sd.ForeignCompany_Code = f.ForeignCompany_Code
LEFT JOIN
(SELECT
SDP.Subject_DEF_Code,
(SELECT
SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc AS FullAksionerCitizenship
FROM
NRC_PeopleAddress P
INNER JOIN
NRC_SubjectDefPeople SDP1 ON SDP1.People_Code = P.People_Code
WHERE
SDP1.Subject_DEF_Code = SDP.Subject_DEF_Code
GROUP BY
SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc
FOR XML PATH('')) AS AKSIONERET
FROM
NRC_SubjectDefPeople SDP
WHERE
SDP.Groyp_Type IN (1,2)
GROUP BY
SDP.Subject_DEF_Code) AS T ON T.Subject_DEF_Code = SD.SUBJECT_DEF_CODE
I need to retrieve, instead of T.AKSIONERET the FullAksionerCitizenship which I get from the for xml path.
I know I have to do some other SELECT but I kept getting errors, syntax errors.
Could someone help?
Thanks in advance
I'm quite sure, that there could be a better design and I'm not able to run this against test data off course, but this could work:
SELECT
T.AKSIONERET,
T2.FullAksionerCitizenship,
sd.Identification_No, sd.Date_Of_Incorp,
sd.Ownership_Desc, sd.Ownership_Code,
sd.Subject_Name_Code, sd.Subject_Name,
sd.Subject_Type_Code,
LEFT(sd.Object_Desc, 3999) AS Object_Desc,
sd.Subject_Status_Name, sd.Subject_Name,
nr2.NACE_Code, nr2.NACE_PARENT, nr2.Description,
sd.Subject_Type_Name, sd.Subject_Status_Name,
sd.Subject_Type_Name, f.Place_Of_Registration, sd.Object_Desc
FROM
NRC_Subject_DEF AS sd
LEFT JOIN
NRC_Nace_REV2 AS nr2 ON nr2.Subject_DEF_Code = sd.Subject_DEF_Code
LEFT JOIN
NRC_ForeignCompany AS f ON sd.ForeignCompany_Code = f.ForeignCompany_Code
LEFT JOIN
(SELECT
SDP.Subject_DEF_Code,
(SELECT
SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc AS FullAksionerCitizenship
FROM
NRC_PeopleAddress P
INNER JOIN
NRC_SubjectDefPeople SDP1 ON SDP1.People_Code = P.People_Code
WHERE
SDP1.Subject_DEF_Code = SDP.Subject_DEF_Code
GROUP BY
SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc
FOR XML PATH('')) AS AKSIONERET
FROM
NRC_SubjectDefPeople SDP
WHERE
SDP.Groyp_Type IN (1,2)
GROUP BY
SDP.Subject_DEF_Code) AS T ON T.Subject_DEF_Code = SD.SUBJECT_DEF_CODE
--here starts the new block (difficult for me without knowing your db...)
LEFT JOIN
(SELECT
(SELECT
SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc AS FullAksionerCitizenship
FROM
NRC_PeopleAddress P
INNER JOIN
NRC_SubjectDefPeople SDP1 ON SDP1.People_Code = P.People_Code
WHERE
SDP1.Subject_DEF_Code = SDP.Subject_DEF_Code
GROUP BY
SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc
) AS FullAksionerCitizenship
FROM
NRC_SubjectDefPeople SDP
WHERE
SDP.Groyp_Type IN (1,2)
GROUP BY
SDP.Subject_DEF_Code) AS T2 ON T2.Subject_DEF_Code = SD.SUBJECT_DEF_CODE

Concatenating 2 columns in sql and applying like operation on the result

SELECT
CONCAT(SG.firstname +' ', SG.lastName) AS 'Name',
SG.entityId 'ID',
dbo.GetTitleDescriptionByEntityID(SG.entityId) 'Title',
CASE ISNULL(EC.address2, '')
WHEN '' THEN EC.address1
ELSE EC.address1 +', ' + EC.address2
END +', ' + EC.city + ', ' + LDState.ItemName + ', ' + EC.zip 'Address',
CR.clinicalRate
FROM
StaffGeneral SG WITH (NOLOCK)
JOIN
LookupDetails LD WITH (NOLOCK) ON SG.employeeStatus = LD.lookupDetailsId AND LD.ItemAbbreviation = 'SCSSC'
JOIN
othersRating or1 WITH (nolock) ON SG.entityId = or1.entityId AND dateRated = (SELECT TOP 1 MAX(dateRated) FROM OthersRating r1 WITH (nolock) WHERE r1.entityid = SG.entityId)
JOIN
LookupDetails LDRating with (nolock) ON or1.rating = LDRating.lookupDetailsId AND LDRating.ItemAbbreviation NOT IN ('RTNP', 'RTDNU', 'RTPENDING', 'RTDELETE', 'RTTERMI')
JOIN
EntityContacts EC with (nolock) ON SG.entityId = EC.entityId AND (eC.contactId = 'General' or EC.contactId = 'General1') AND EC.deleted = 0
JOIN
LookupDetails LDState WITH (NOLOCK) ON EC.state = LDState.lookupDetailsId
JOIN
ContractorRate CR ON SG.entityId = CR.entityId AND CR.deleted = 0
WHERE
SG.isActive = 0 AND SG.deleted = 0
AND ((SG.entityId = #subContractorID) OR (#subContractorID IS null))
AND ((#subContractorName IS NULL) OR ((EC.firstName + ' ' + EC.lastName) LIKE #subContractorName + '%'))
In the above query I wanted to concat the first and last name with space and query it against the input to the stored procedure #subcontractorname.
When the subcontractorname is null that is fine but I am not able to get results when it jumps to Or clause .
Tried the SQL Server Profiler but does not show anything how the query is formed
I tried executing the SQL by putting the whole SQL in a var but I am hitting the problem of varchar(max).
Please advise

How to Convert the SQL Server Query to Equivelent SQLite Query?

I have this query which works in SQL server:
SELECT
T_Cbt.cbtName,
T_HDD.hddName,
T_HDD.ip,
T_HDD.compName,
T_Cbt.mobRack,
T_HDD.size,
T_OS.osName + ' ' + T_OS.osVersion + ' - ' + T_OS.osLanguage AS Expr1,
T_OS.osBit,
T_SWList.swName + ' ' + T_SWDetails.swVersion + ' - ' + T_SWDetails.swLang + ' ' + T_SWDetails.swArch AS Expr2
FROM T_OS INNER JOIN
T_Cbt INNER JOIN
T_HDD ON T_Cbt.cbtId = T_HDD.cbtId ON T_OS.id = T_HDD.osId INNER JOIN
T_Division ON T_HDD.division = T_Division.divId LEFT OUTER JOIN
T_SWDetails INNER JOIN
T_SW ON T_SWDetails.sdPk = T_SW.swdid INNER JOIN
T_SWList ON T_SWDetails.swdId = T_SWList.swId ON T_HDD.hddid = T_SW.hddid
WHERE T_Cbt.cbtName <> 'FREE'
ORDER BY T_Cbt.purpose
While i am executing this query in Sqlite i get error like Error while executing query: near "ON": syntax error How to fix it?
SQLite does not allow nesting JOIN and ON arbitrarily. Reordering your JOINs to not use nesting should allow the query to execute without a problem;
SELECT
T_Cbt.cbtName, T_HDD.hddName, T_HDD.ip, T_HDD.compName,
T_Cbt.mobRack, T_HDD.size,
T_OS.osName || ' ' || T_OS.osVersion || ' - ' || T_OS.osLanguage AS Expr1,
T_OS.osBit, T_SWList.swName || ' ' || T_SWDetails.swVersion || ' - ' ||
T_SWDetails.swLang || ' ' || T_SWDetails.swArch AS Expr2
FROM T_OS
INNER JOIN T_HDD ON T_OS.id = T_HDD.osId
INNER JOIN T_Cbt ON T_Cbt.cbtId = T_HDD.cbtId
INNER JOIN T_Division ON T_HDD.division = T_Division.divId
INNER JOIN T_SW ON T_HDD.hddid = T_SW.hddid
LEFT OUTER JOIN T_SWDetails ON T_SWDetails.sdPk = T_SW.swdid
INNER JOIN T_SWList ON T_SWDetails.swdId = T_SWList.swId
WHERE (T_Cbt.cbtName <> 'FREE')
ORDER BY T_Cbt.purpose

Syntax for a SQL query

k=conn:query({live=true,sql ="select DISTINCT O.OrderId, (SELECT R.ReportBody + (char(13)+char(10)) + 'Modified By: ' + UM.FirstName + ' at ' + UM.LastName + (char(13)+char(10)) +'Authored By: ' + UC.FirstName + ' at ' + UC.LastName + (char(13)+char(10)) + (char(13)+char(10)) AS [text()] FROM dbo.OrderReports R INNER JOIN dbo.Users UM ON R.ModifiedById = UM.UserId INNER JOIN dbo.Users UC ON R.CreatedById = UC.UserId WHERE R.OrderId = M[1].OrderId ORDER BY R.CreateDate DESC FOR XML PATH('')) 'OrderReport'FROM dbo.Orders O "})
I have above mentioned query and I am getting incorrect syntax error at where R.OrderId = M[1].OrderId , I am using sql with Lua.
P.S: The value in M[1].OrderId is a uniqueidentifier.
But if I have this query:
mk = conn:query({live = true, sql= "select ReportBody,DENSE_RANK() over (Order by ModifyDate DESC) from OrderReports where OrderId = '"..M[1].OrderId.."'"})
It works with M[1].OrderId though M[1].OrderId gives syntax error in the first query. How can I fix this in first query?
OK, you have not answered my last question. But assuming that the query you have written is working this is what you are after:
k=conn:query({live=true,sql ="select DISTINCT O.OrderId, (SELECT R.ReportBody + (char(13)+char(10)) + 'Modified By: ' + UM.FirstName + ' at ' + UM.LastName + (char(13)+char(10)) +'Authored By: ' + UC.FirstName + ' at ' + UC.LastName + (char(13)+char(10)) + (char(13)+char(10)) AS [text()] FROM dbo.OrderReports R INNER JOIN dbo.Users UM ON R.ModifiedById = UM.UserId INNER JOIN dbo.Users UC ON R.CreatedById = UC.UserId WHERE R.OrderId = '"..M[1].OrderId.."' ORDER BY R.CreateDate DESC FOR XML PATH('')) 'OrderReport'FROM dbo.Orders O "})