Issue with expected parameters in SQL statement - sql

I am trying to run a small append query in VBA to add new data for a duplicated record.
strsql = " INSERT INTO tblQuoteNumGeneration ( JobID, BaseQuote, Yearnum, RevisionNum )"
strsql = strsql & " SELECT JobID = [Forms]![JobQuote]![JobID], BaseQuote = " & _
QuoteNumber & ", YearNum = " & MyYear & ", RevisionNum = " & NewRev & ""
strsql = strsql & " FROM tblQuoteNumGeneration"
However, I am getting an Expected parameters error. On a debug.print, it outputs:
INSERT INTO tblQuoteNumGeneration ( JobID, BaseQuote, Yearnum, RevisionNum )
SELECT JobID = [Forms]![JobQuote]![JobID], BaseQuote = 14937, YearNum = 20, RevisionNum = 2
FROM tblQuoteNumGeneration
Could someone who is a bit more versed in SQL help point me to my mistake?

You have incorrect SQL statement generated. Instead of:
INSERT INTO tblQuoteNumGeneration ( JobID, BaseQuote, Yearnum, RevisionNum )
SELECT JobID = [Forms]![JobQuote]![JobID], BaseQuote = 14937, YearNum = 20, RevisionNum = 2
FROM tblQuoteNumGeneration
you should have:
INSERT INTO tblQuoteNumGeneration ( JobID, BaseQuote, Yearnum, RevisionNum )
SELECT JobID, BaseQuote, Yearnum, RevisionNum
FROM tblQuoteNumGeneration

Related

IF NOT EXISTS in UCanAccess

I have the following code, but UCanAccess does not allow me to use IF NOT EXISTS. Does anyone have any ideas?
"if not exists (select ArtikelNr from Artikel where ArtikelNr='"+ValueRead.prüfen1()+"')" +
"begin" +
" if not exists (select isa_id from Artikel where isa_id='"+Integer.parseInt(Data.textField.getText())+"')" +
" begin" +
"INSERT INTO Artikel ([HERE ARE THE TABLES) VALUES
[HERE ARE THE VALUES]
" end" +
"end";
You could do something like this:
// test data
int artikelNr = 123;
int isa_id = 1234;
String description = "artikel_123";
String sql = ""
+ "INSERT INTO Artikel (ArtikelNr, isa_id, description) "
+ "SELECT ? AS ArtikelNr, ? AS isa_id, ? as description "
+ "FROM DUAL "
+ "WHERE NOT EXISTS ("
+ " SELECT * FROM Artikel "
+ " WHERE ArtikelNr=? AND isa_id=?)";
PreparedStatement ps = conn.prepareStatement(sql);
// values to INSERT
ps.setInt(1, artikelNr);
ps.setInt(2, isa_id);
ps.setString(3, description);
// values for WHERE NOT EXISTS
ps.setInt(4, artikelNr);
ps.setInt(5, isa_id);
int rtn = ps.executeUpdate();
System.out.printf("%d row(s) inserted%n", rtn);

Error in JPQL Query: "unexpected token: ( near line 1"

i have this working sql query:
select temp.pratica_regola_id FROM ( SELECT TABLE1.pratica_regola_id, TABLE1.CON FROM ( SELECT pratica_regola_id, COUNT(*) AS CON FROM (SELECT DISTINCT PRATICA_REGOLA_ID, ENTITA_LEGAME_ID FROM legame_regola_pratica L1 LEFT JOIN (select LRP.entita_legame_id AS EL1 from legame_regola_pratica LRP JOIN entita_legame EL ON LRP.entita_legame_id = EL.entita_legame_id where LRP.pratica_regola_id = 418 AND LRP.REGOLA_ID = 1426 AND el.tp_entita_legame_id =1 ) TAB_ENT_LEG ON TAB_ENT_LEG.EL1 = L1.entita_legame_id WHERE TAB_ENT_LEG.EL1 IS NOT NULL AND l1.tp_livello_legame_id = 2 and l1.pratica_regola_id in (select pratica_regola_id from pratica_regola where tp_stato_pratica_id = 2)) GROUP BY PRATICA_REGOLA_ID ) TABLE1 INNER JOIN ( SELECT pratica_regola_id, COUNT(*) AS TOT FROM (SELECT DISTINCT PRATICA_REGOLA_ID, ENTITA_LEGAME_ID FROM legame_regola_pratica L1) group by pratica_regola_id) TABLE2 on TABLE1.pratica_regola_id = TABLE2.pratica_regola_id where TABLE1.CON = TABLE2.TOT) temp WHERE CON = (SELECT COUNT(*) FROM legame_regola_pratica where pratica_regola_id = 418 AND REGOLA_ID = 1426 and dat_ora_can is null) and pratica_regola_id <> 418;
I tried to tranlsate it in JPQL in this kind:
#Query("SELECT TABLE_TEMP_1.praticaRegolaId FROM ("
+ "SELECT TABLE_TEMP_2.praticaRegolaId, TABLE_TEMP_2.numero_entita FROM ("
+ "SELECT praticaRegolaId, COUNT(*) AS numero_entita FROM (SELECT DISTINCT PraticaRegola.praticaRegolaId, EntitaLegame.entitaLegameId FROM LegameRegolaPratica LRP_1 LEFT JOIN ("
+ "SELECT LRP.entitaLegame.entitaLegameId AS EL_1 FROM LegameRegolaPratica LRP JOIN EntitaLegame EL ON LRP.entitaLegame.entitaLegameId = EL.entitaLegameId"
+ "WHERE LRP.praticaRegola = :praticaRegola AND LRP.regola = :praticaRegola.regola AND EL.tpEntitaLegame = :tipoEntitaLegame)"
+ "TAB_ENT_LEG ON TAB_ENT_LEG.EL_1 = LRP_1.entitaLegame.entitaLegameId WHERE TAB_ENT_LEG.EL_1 IS NOT NULL AND LRP_1.tpLivelloLegame = :tipoLivelloLegame AND LRP_1.praticaRegola.praticaRegolaId IN (SELECT praticaRegolaId FROM"
+ " praticaRegola WHERE tpStatoPratica = :tipoStatoPratica))"
+ "GROUP BY praticaRegolaId) AS TABLE_TEMP_2"
+ "INNER JOIN ("
+ "SELECT praticaRegolaId, COUNT(*) AS TOT FROM (SELECT DISTINCT LRP_1.praticaRegola.praticaRegolaId, LRP_1.entitaLegame.entitaLegameId FROM LegameRegolaPratica LRP_1) GROUP BY praticaRegolaId"
+ ") TABLE_TEMP_3" + "ON TABLE_TEMP_2.praticaRegolaId = TABLE_TEMP_3.praticaRegolaId" + "WHERE TABLE_TEMP_2.NUMERO_ENTITA = TABLE_TEMP_3.TOT )"
+ "AS TABLE_TEMP_1 WHERE NUMERO_ENTITA = (SELECT COUNT(*) FROM LegameRegolaPratica WHERE praticaRegola = :praticaRegola"
+ "AND regola = :praticaRegola.regola AND dataOraCancellazione IS NULL) ")
but i get this error message:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 42 [SELECT TABLE_TEMP_1.praticaRegolaId FROM (SELECT TABLE_TEMP_2.praticaRegolaId, TABLE_TEMP_2.numero_entita FROM (SELECT praticaRegolaId, COUNT(*) AS numero_entita FROM (SELECT DISTINCT PraticaRegola.praticaRegolaId, EntitaLegame.entitaLegameId FROM it.aivebst.adaprice.rules.entities.LegameRegolaPratica LRP_1 LEFT JOIN (SELECT LRP.entitaLegame.entitaLegameId AS EL_1 FROM it.aivebst.adaprice.rules.entities.LegameRegolaPratica LRP JOIN EntitaLegame EL ON LRP.entitaLegame.entitaLegameId = EL.entitaLegameIdWHERE LRP.praticaRegola = :praticaRegola AND LRP.regola = :praticaRegola.regola AND EL.tpEntitaLegame = :tipoEntitaLegame)TAB_ENT_LEG ON TAB_ENT_LEG.EL_1 = LRP_1.entitaLegame.entitaLegameId WHERE TAB_ENT_LEG.EL_1 IS NOT NULL AND LRP_1.tpLivelloLegame = :tipoLivelloLegame AND LRP_1.praticaRegola.praticaRegolaId IN (SELECT praticaRegolaId FROM praticaRegola WHERE tpStatoPratica = :tipoStatoPratica))GROUP BY praticaRegolaId) AS TABLE_TEMP_2INNER JOIN (SELECT praticaRegolaId, COUNT(*) AS TOT FROM (SELECT DISTINCT LRP_1.praticaRegola.praticaRegolaId, LRP_1.entitaLegame.entitaLegameId FROM it.aivebst.adaprice.rules.entities.LegameRegolaPratica LRP_1) GROUP BY praticaRegolaId) TABLE_TEMP_3ON TABLE_TEMP_2.praticaRegolaId = TABLE_TEMP_3.praticaRegolaIdWHERE TABLE_TEMP_2.NUMERO_ENTITA = TABLE_TEMP_3.TOT )AS TABLE_TEMP_1 WHERE NUMERO_ENTITA = (SELECT COUNT(*) FROM it.aivebst.adaprice.rules.entities.LegameRegolaPratica WHERE praticaRegola = :praticaRegolaAND regola = :praticaRegola.regola AND dataOraCancellazione IS NULL) ]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
Thank you
You may not have subqueries in the from clause of a HQL query. Use a native SQL query.
Quote from the documentation:
Note that HQL subqueries can occur only in the select or where clauses.

How to insert if statement in sql query at vb.net?

How to insert if/else in the query?
This is my sqlcommand query :
sqlcommand ("select Machine_Code,Machine_Name,Status from
Machine_Master where '" & combolinecode.Text & "' = Line_Code" )
I wanna change the value of Status from 1 => active and 0 => not active.
You can do this with the ANSI standard case statement in almost all databases:
select Machine_Code, Machine_Name,
(case when Status = 1 then 'active' else 'not active' end) as status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code";
I fear that if you are using VBA you might be using Access, which has its own way:
select Machine_Code, Machine_Name,
iif(Status = 1, "active", "not active") as status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code";
You can use case :
select Machine_Code
, Machine_Name
, (case when Status = 1 then 'active' else 'not active' end) as Status
from Machine_Master
where '" & combolinecode.Text & "' = Line_Code

Can these two SQL statements be made into one? Changing multiple indices with two constraints

I have a form that users can use to edit data in my database. The database is structured like this:
If a user wants to edit both a FAVE_COLOR and a FAVE_FOOD, how would I go about doing that in my SQL statement? I can think of this, but is there a way to do this in one statement?
string sql1 = "UPDATE MYTABLE " +
"SET PROP_VALUE = '" + form["color"] + "' " +
"WHERE ID = " + form["id"] + " " +
"AND PROP_NAME = 'FAVE_COLOR'"
string sql2 = "UPDATE MYTABLE " +
"SET PROP_VALUE = '" + form["food"] + "' " +
"WHERE ID = " + form["id"] + " " +
"AND PROP_NAME = 'FAVE_FOOD'"
string sql = "UPDATE MYTABLE " +
"SET PROP_VALUE = CASE " +
"WHEN PROP_NAME = 'FAVE_COLOR' THEN '" + form["color"] + "' " +
"WHEN PROP_NAME = 'FAVE_FOOD' THEN '" + form["food"] + "' " +
"END " +
"WHERE ID = " + form["id"] + " " +
"AND PROP_NAME IN ('FAVE_COLOR', 'FAVE_FOOD')"
But beware of SQL injection! You really should be using prepared statements, into which you pass your variables as parameters that do not get evaluated for SQL. If you don't know what I'm talking about, or how to fix it, read the story of Bobby Tables.
You can use case statements:
UPDATE MYTABLE
SET PROP_VALUE = (
CASE favefood
WHEN PROP_NAME = 'FAVE_FOOD'
THEN 'PIZZA'
CASE favecolor
WHEN PROP_NAME = 'FAVE_COLOR'
THEN 'BLUE'
WHERE ID = #myIdValue
For MS SQL Server you can use an UPDATE FROM which will update two properties at the same time, like this:
CREATE TABLE MYTABLE (
ID INT,
PROP_NAME VARCHAR(20),
PROP_VALUE VARCHAR(20));
go
INSERT INTO MYTABLE VALUES (1, 'A','B')
go
INSERT INTO MYTABLE VALUES (1, 'C', 'D')
go
UPDATE MYTABLE
SET PROP_VALUE = X.PROP_VALUE
FROM MYTABLE MT JOIN (
SELECT 'A' AS PROP_NAME, 'F' AS PROP_VALUE
UNION
SELECT 'C' AS PROP_NAME, 'G' AS PROP_VALUE) AS X ON MT.PROP_NAME = X.PROP_NAME
WHERE ID = 1
For other SQL DB server the solution should be similar if not identical.

Is this UPDATE table statement correct in an msdn topic

I have seen this type of UPDATE statement (just like insert statement) in the following msdn topic:
http://msdn.microsoft.com/en-us/library/aa0416cz.aspx#Y2461
UPDATE statement:-
adapter.UpdateCommand = New SqlCommand("UPDATE Customers " &
"(CustomerID, CompanyName) VALUES(#CustomerID, #CompanyName) " & _
"WHERE CustomerID = #oldCustomerID AND CompanyName = " &
"#oldCompanyName", connection)
Is this statement correct or not?
I have tried executing it and it is giving syntax errors.
No, it should be:
UPDATE Customers
SET
CustomerID = #CustomerID,
CompanyName = #CompanyName
WHERE
CustomerID = #oldCustomerID AND
CompanyName = #oldCompanyName
Or to be complete with your sample code, it should be:
adapter.UpdateCommand = New SqlCommand("UPDATE Customers SET CustomerID = #CustomerID, CompanyName = #CompanyName WHERE CustomerID = #oldCustomerID AND CompanyName = #oldCompanyName", connection)
Here is another reference for you and this situation: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx
That SQL appears to be correct for an INSERT INTO but not for an UPDATE It should read:
adapter.UpdateCommand = New SqlCommand("UPDATE Customers" & _
" SET CustomerID = #CustomerID, CompanyName = #CompanyName)" & _
" WHERE CustomerID = #oldCustomerID AND CompanyName =" & _
" #oldCompanyName", connection)
That SQL is what one would call paramaterized, so that makes this code (lower in the snippet) very important:
adapter.UpdateCommand.Parameters.Add( _
"#CustomerID", SqlDbType.NChar, 5, "CustomerID")
adapter.UpdateCommand.Parameters.Add( _
"#CompanyName", SqlDbType.NVarChar, 30, "CompanyName")
' Pass the original values to the WHERE clause parameters.
Dim parameter As SqlParameter = dataSet.UpdateCommand.Parameters.Add( _
"#oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
parameter = adapter.UpdateCommand.Parameters.Add( _
"#oldCompanyName", SqlDbType.NVarChar, 30, "CompanyName")
parameter.SourceVersion = DataRowVersion.Original
As far as I can see the syntax is not valid. The following gives Incorrect syntax near '('.
I suggest changing it as per Dan's answer.
CREATE TABLE Customers
(
CustomerID INT,
CompanyName VARCHAR(10)
)
DECLARE
#CustomerID INT,
#CompanyName VARCHAR(10),
#oldCustomerID INT,
#oldCompanyName VARCHAR(10)
UPDATE Customers (CustomerID, CompanyName)
VALUES(#CustomerID, #CompanyName)
WHERE CustomerID = #oldCustomerID AND CompanyName = #oldCompanyName