IF NOT EXISTS in UCanAccess - sql

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);

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";

Dapper query, combining attributes to use with pagination

I have that query that shows all my Boats, but I would just like to show the active boats.
public Pagination<Boats> GetAll(string name , int pageSize, int pageNumber)
{
var cn = Db.Database.Connection;
var sql = #"SELECT * FROM Boats" +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%')" +
"ORDER BY [Name ] " +
"OFFSET " + pageSize * (pageNumber - 1) + " ROWS " +
"FETCH NEXT " + pageSize + " ROWS ONLY " +
" " +
"SELECT COUNT(Id) FROM Boats" +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%')";
var multi = cn.QueryMultiple(sql, new { Name = name });
var boats= multi.Read<Boats >();
var total = multi.Read<int>().FirstOrDefault();
var paginationList= new Pagination<Boats>()
{
List = boats,
Count = total
};
return paginationList;
}
I have the active and excluded attributes in the table, so I tried to show only the boats that were active, but with this query, it keeps returning all records from the boat table, like frist one
var sql = #"SELECT * FROM Boats " +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%' AND Active=1 AND Excluded=0)" +
"ORDER BY [Name] " +
"OFFSET " + pageSize * (pageNumber - 1) + " ROWS " +
"FETCH NEXT " + pageSize + " ROWS ONLY " +
" " +
"SELECT COUNT(Id) FROM Boats " +
"WHERE (#Name IS NULL OR Name LIKE #Name + '%' AND Active=1 AND Excluded=0)";
Any Ideas how can I combine the Name attribute with the Active and Excluded attributes?
Remove it from the parenthesis:
"WHERE (#Name IS NULL OR Name LIKE #Name + '%') AND Active=1 AND Excluded=0"

using Transaction query in jsp page

I use below query in my jsp page. but I'm not sure Is it true to use this query in jsp page.
int i = st.executeUpdate("'BEGIN TRANSACTION DECLARE #id [int] SELECT #id = SCOPE_IDENTITY() INSERT INTO Viewer(Reserve_ID, F_Name, L_Name, Competition_ID, City, Phone, [E-mail]) VALUES (#id, '" + fname + "','" + lname + "','" + 30 + "','" + city + "','" + phone + "','" + email + "' ) INSERT INTO Reservation_Inf(Reservation_Date, Competition_ID, NumberOfTicket, Position_ID) VALUES ('" + dNow + "','" + 30 + "','" + 1 + "','" + 8 + "' ) COMMIT TRANSACTION '" );
if (i > 0) {
response.sendRedirect("Success.jsp");
} else {
response.sendRedirect("Fail.jsp");
}
It gives this error :
Incorrect syntax near 'BEGIN TRANSACTION DECLARE #id [int] SELECT #id = SCOPE_IDENTITY() INSERT INTO Viewer(Reserve_ID, F_Name, L_Name, Competition_ID,'.

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.

How to fetch distinct values from multiple rows of select query?

I have used query as
string getPlayers = " Select Player1,Player2,Player3,Player4,Player5 from tbl_game where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + " OR Player4=" + userid + " OR Player5=" + userid + " AND Complete = 'No' ";
It gives result as multiple rows that fulfills the condition.
But I want to store values of distinct Player1,Player2,Player3,Player4,Player5 in variables returned from select query, so that i can use these values for further case.
How can i do this ?
Please help me. I am very confused.
EDITED :
I got the result of select query as -
(1,2,2,4,5)
(2,3,1,4,5)
(4,3,5,1,2)
Where 1,2,3,4,5 are userids(players).
I want store these userids in variables as -
p1=1 , p2=2 , p3=3 ,p4=4, p5=5
string getPlayer1 = (" select var1 from (" Select distinct Player1 as var1 ,Player2,Player3,Player4,Player5 from tbl_game where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + " OR Player4=" + userid + " OR Player5=" + userid + " AND Complete = 'No' "));
var1 will have a value of player1. Repeat this for all other players. Hope this is what you want.