Unknown Sql Exception (Number = 102, Message=Incorrect syntax near ','.) - sql

I got this syntax error near ','
I already tried some solutions as this question is already asked in this forum before, but to no avail.
string sqlSelect = "SELECT"
+ " 'RI' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount,"
+ "(Outstanding * CurrencyRate) AS LocalBalance"
+ " FROM ARINVOICE"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RD' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount"
+ ",Outstanding AS LocalBalance"
+ " FROM ARDN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RC' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",'' AS SalesAgent, LocalNetTotal AS LocalAmount"
+ ",(NetTotal - KnockOffAmt + RefundAmt) * CurrencyRate AS LocalBalance"
+ " FROM ARCN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RP' AS 'DocType', 'DebtorCode', 'DocNo', 'DocDate',
[Description]"
+ ",'' AS SalesAgent, LocalPaymentAmt AS LocalAmount"
+ ",LocalUnappliedAmount AS LocalBalance"
+ " FROM ARPAYMENT"
+ " WHERE Cancelled = 'F'";
Is their anything wrong with my query? Because i already check it for 2 days and could not find any mistakes, not that i known.
Hope anyone could help.

have u tried to unquote the DocType ?
string sqlSelect = "SELECT"
+ " 'RI' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount,"
+ "(Outstanding * CurrencyRate) AS LocalBalance"
+ " FROM ARINVOICE"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RD' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",SalesAgent, LocalNetTotal AS LocalAmount"
+ ",Outstanding AS LocalBalance"
+ " FROM ARDN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RC' AS DocType, DebtorCode, DocNo, DocDate, [Description]"
+ ",'' AS SalesAgent, LocalNetTotal AS LocalAmount"
+ ",(NetTotal - KnockOffAmt + RefundAmt) * CurrencyRate AS LocalBalance"
+ " FROM ARCN"
+ " WHERE Cancelled = 'F'"
+ " UNION"
+ " SELECT 'RP' AS DocType, DebtorCode, DocNo, DocDate,
[Description]"
+ ",'' AS SalesAgent, LocalPaymentAmt AS LocalAmount"
+ ",LocalUnappliedAmount AS LocalBalance"
+ " FROM ARPAYMENT"
+ " WHERE Cancelled = 'F'";

In SQL SERVER, check below script-
declare #sqlSelect nvarchar(max)
select #sqlSelect = N'
SELECT ''RI'' AS DocType,DebtorCode,DocNo,DocDate,[Description],SalesAgent,LocalNetTotal AS LocalAmount,
(Outstanding * CurrencyRate) AS LocalBalance
FROM ARINVOICE WHERE Cancelled = ''F''
UNION
SELECT ''RD'' AS DocType,DebtorCode,DocNo,DocDate,[Description],SalesAgent,LocalNetTotal AS LocalAmount,Outstanding AS LocalBalance
FROM ARDN WHERE Cancelled = ''F''
UNION
SELECT ''RC'' AS DocType,DebtorCode,DocNo,DocDate,[Description],'''' AS SalesAgent,LocalNetTotal AS LocalAmount,
((NetTotal - KnockOffAmt + RefundAmt) * CurrencyRate) AS LocalBalance
FROM ARCN WHERE Cancelled = ''F''
UNION
SELECT ''RP'' AS DocType,DebtorCode,DocNo,DocDate,[Description],'''' AS SalesAgent,LocalPaymentAmt AS LocalAmount,
LocalUnappliedAmount AS LocalBalance
FROM ARPAYMENT WHERE Cancelled = ''F'' '
print (#sqlSelect)
--exec (#sqlSelect)

Related

subquery in FROM must have an alias for sql query

I have this code snippet for a query below.
"SELECT DISTINCT(CODE) FROM (" +
"SELECT TRIM(BOTH ' ' FROM PROJECT_NUMBER) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE PROJECT_NUMBER IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
"UNION " +
"SELECT TRIM(BOTH ' ' FROM ANOTHER_CODE) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE ANOTHER_CODE IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
") " +
"WHERE CODE IS NOT NULL";
I'm in the process of migrating to postgres from oracle and I'm seeing these errors in our logs:
Caused by: org.postgresql.util.PSQLException: ERROR: subquery in FROM must have an alias
Hint: For example, FROM (SELECT ...) [AS] foo.
Where should the alias go, do I need for all subqueries, and do I need to use the alias in this query? Little confused here.
"SELECT DISTINCT(CODE) FROM (" +
"SELECT TRIM(BOTH ' ' FROM PROJECT_NUMBER) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE PROJECT_NUMBER IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
"UNION " +
"SELECT TRIM(BOTH ' ' FROM ANOTHER_CODE) CODE FROM SCHEMA.TABLE_NAME " +
"WHERE ANOTHER_CODE IS NOT NULL " +
"AND LAST_UPDATE_DATE >= :lastUpdateDate " +
") as qry " +
"WHERE CODE IS NOT NULL";

SQL cmd.command include an OR statement

Below is my SQL cmd.CommandText query:
cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " +
" FROM [dbo].[AuditLog] "+
" WHERE ActionTaken = 'Clicked' "+
" and Type = 'Attachment Link'" +
" and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
" GROUP BY CONVERT(date, [DateTime]), ActionBy "+
" HAVING COUNT(DISTINCT Identifier) > " + limit +
" ORDER BY COUNT(DISTINCT Identifier) DESC";
I would like to include an extra parameter OR for Type, like as follows:
" and Type = 'Attachment Link' OR 'Attachment Link - Search'" +
May I know how can it be done?
Thank you.
Edit: Apologies, did not add in the error message, as follow.
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.
specify the column after your OR operator
cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " +
" FROM [dbo].[AuditLog] "+
" WHERE ActionTaken = 'Clicked' "+
" and (Type = 'Attachment Link' OR Type = 'Attachment Link - Search') " +
" and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
" GROUP BY CONVERT(date, [DateTime]), ActionBy "+
" HAVING COUNT(DISTINCT Identifier) > " + limit +
" ORDER BY COUNT(DISTINCT Identifier) DESC";
I assume I am over-simplifying but do you mean:
cmd.CommandText = "SELECT CONVERT(date, [DateTime]) 'Date of Download', ActionBy AS 'User Full Name', COUNT(DISTINCT CAST(Identifier AS NVARCHAR(10)) + Remarks + Link) 'Number of Document Downloads' " +
" FROM [dbo].[AuditLog] "+
" WHERE ActionTaken = 'Clicked' "+
" and (Type = 'Attachment Link' OR Type = 'Attachment Link - Search') " +
" and CONVERT(date, [DateTime]) BETWEEN CONVERT(date,'" + scanStartDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') and CONVERT(date,'" + scanEndDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "') "+
" GROUP BY CONVERT(date, [DateTime]), ActionBy "+
" HAVING COUNT(DISTINCT Identifier) > " + limit +
" ORDER BY COUNT(DISTINCT Identifier) DESC";
You might need ( ) for order of operations and I think you mean "=" instead of "-".

SQL select, sum and group

I'm struggling with an Access database:
I have a large database with the headers TAG, ZST, KL and R1H00 to R1H23 (24 of them, each one stands for one hour of a day) and I need to get a specific dataset out of it:
SELECT TAG, ZST, (R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
So far so good, but the result contains many items with the same ID (ZST). I need to sum all entries with the same ZST, but I couldn't manage to do it so far. (I tried to use the GROUP BY statement, but that only results in errors)
Any experienced SQL people here that could help me with this?
use group by
SELECT TAG, ZST, sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
WHERE KL = "SWISS7_PW"
GROUP BY TAG, ZST;
Use the Sum() function
SELECT TAG, ZST, Sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL
FROM Klassendaten
where ZST = '(Whatever it is)'
group by tag, zst

ORA 00905 missing keyword in Select statement

try {
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
st3 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery("select insp_no,ro_code, inspectiondate from keroseneheader where ro_code='" + cust_code + "' and status='NO' and inspectedby='" + em.ro_code + "'");
if (rs.next())
{
session.setAttribute("insp_no", rs.getString("insp_no"));
out.print(rs.getString("insp_no") + "#" + rs.getString("inspectiondate"));
}
else
{
rs3 = st3.executeQuery("select * from keroseneheader where ro_code='" + cust_code + "' and inspectiondate>to_date('" + inspectdate + "','dd-mm-yyyy') and inspectedby='" + em.ro_code + "'");
if (!rs3.next())
{
String query2 = "insert into keroseneheader(insp_no,ro_code,inspectiondate,status,inspectedby) values((select nvl(max(INSP_NO)+1,1) from keroseneheader),'" + cust_code + "',"
+ "to_date('" + inspectdate + "','dd-mm-yyyy'),'NO','" + em.ro_code + "')";
boolean i = DbConn.insertupdatedata(query2, false);
if (i) {
st1 = con.createStatement();
rs1 = st1.executeQuery("select insp_no,to_char(inspectiondate,'dd-mm-yyyy') inspectiondate from keroseneheader where ro_code=" + cust_code + " and status='NO' and inspectedby='" + em.ro_code + "'");
if (rs1.next()) {
session.setAttribute("insp_no", rs1.getString("insp_no"));
out.print(rs1.getString("insp_no") + "#" + rs.getString("inspectiondate"));
}
}
}
else {
out.print("Invalid Date");
}
}
Change this line
st1.executeQuery("select insp_no,to_char(inspectiondate,'dd-mm-yyyy') inspectiondate from keroseneheader
To this
st1.executeQuery("select insp_no,to_char(inspectiondate,'dd-mm-yyyy') as 'inspectiondate' from keroseneheader

JPA Query with Case condition

(Case a.statusCd When 'ln' then 'loan' else a.status_cd end) as statusCd
Why this jpa query returns an error?
The whole query string is
String qryString = "select"
+ " a.biblioId, " // 0
+ " a.copyId, " // 1
+ " a.copyDescr, "// 2
+ " a.employeeId, "// 3
+ " (Case a.statusCd When 'ln' then 'loan' else a.status_cd end) as statusCd, "// 4
+ " a.statusBeginDt, "// 5
+ " a.libListingCopyId, "// 6
+ " a.barcodeNbr, " // 7
+ " b.title, " // 8
+ " b.author " // 9
+ " from LibListingCopy a, LibListing b " + " where "
+ curStmt + "and " + authStmt
+ "and a.biblioId = b.biblioId and a.siteId=b.siteId and "
+ bibStmt + " and " + cpyStmt + " and " + libCodeStmt
+ " and " + accnNbrStmt;
Please check your query, there are two seperate fieldnames for the same filed i think:
(Case a.statusCd When 'ln' then 'loan' else a.status_cd end) as statusCd
should be
(Case a.statusCd When 'ln' then 'loan' else a.statusCd end) as statusCd
or (for the other possibility)
(Case a.status_cd When 'ln' then 'loan' else a.status_cd end) as statusCd
So the failure may be the spelling of your field a.status_cd / a.statusCd