SQL Server CE : if exist update else insert - sql

How can I INSERT a row if does not yet exist in a SQL Server CE database table and UPDATE it if it exists?
I have tried lot of SQL queries and keep getting errors. This is not working.
IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')
UPDATE Table1 SET (...) WHERE Column1='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
Update:
I have found this which is working for me. Any other good suggestion is welcome.
INSERT INTO Table1 VALUES (...)
SELECT (........)
WHERE NOT Exists (SELECT ........)
-- INSERT with Default value if not exist. Next, UPDATE it
UPDATE Table1 SET (...) WHERE Column1='SomeValue'

I know you've tagged sql and sql-server-ce but in case you're open to using c# code to fix this.. :
Using c# and result sets this is what I did for my mobile app using SQL CE:
// UpdateRow is a struct/class to hold the data for each row
// SqlCeConn = connection string for db
SqlCeCommand cmd = new SqlCeCommand("Table1", SqlCeConn);
cmd.CommandType = CommandType.TableDirect;
cmd.IndexName = "Column1";
using (SqlCeResultSet rsltSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable))
{
if (UpdateRow.Column1> 0) // or != "" if it's string etc
{
if (rsltSet.Seek(DbSeekOptions.FirstEqual, UpdateRow.Column1))
FoundRecord = true;
}
rsltSet.Read();
if (FoundRecord)
{
// Update
rsltSet.SetInt32(1, UpdateRow.Column1);
rsltSet.SetInt32(2, UpdateRow.Column2);
// etc
rsltSet.Update();
}
else
{
// Insert new record
SqlCeUpdatableRecord record = rsltSet.CreateRecord();
record.SetInt32(0, UpdateRow.Column1);
record.SetInt32(1, UpdateRow.Column2);
// etc
rsltSet.Insert(record, DbInsertOptions.PositionOnInsertedRow);
}
}
cmd.Dispose();
}
catch (Exception e)
{
// Deal with exception
}

Related

Pass SELECT MAX(`Id`) FROM Table to setval()

I want to pass (SELECT MAX(Id) FROM Table to mariadb's setval() function I tried with:
SELECT setval(`MySequence`, (SELECT MAX(`Id`) FROM `Table`));
but it doesn't work, I also tried:
SET #max_value = (SELECT MAX(`Id`) FROM `Table`);
SELECT setval(`MySequence`, #max_value);
how am I supposed to do this?
EDIT I made a mistake posting the question. I was using SET on the second code and is not working
EDIT As I said on the comments I'm trying to do this just once, executing from an Entity Framework Core migration. What I ended doing is executing the SELECT MAX(Id) FROM Table and recovering that value from the migration code to interpolate it later on the $"SELECT setval('sequence', {value}, true)"
I found working workaround using prepared statement:
SET #max_value = (SELECT MAX(`Id`) FROM `Table`);
EXECUTE IMMEDIATE CONCAT('SELECT SETVAL(`MySequence`, ', #max_value, ')');
In a select, use := to assign variables:
SELECT #max_value := MAX(`Id`) FROM `Table`;
SELECT setval(`MySequence`, #max_value);
You might want to add 1 to the value.
I think you can do:
SELECT setval(`MySequence`, MAX(Id))
FROM `Table`;
In a stand-alone statement (not a query), SET is generally used to assign value to a user-defined variable. Try the following instead:
SET #max_value = (SELECT MAX(`Id`) FROM `Table`);
SELECT setval(`MySequence`, #max_value);
It seems that is not possible to do this as of MariaDB 10.3.16
I've raised a bug on https://jira.mariadb.org/browse/MDEV-20111 for devs to consider adding this feature or upgrading the documentation to make explicit that it can't be done.
I worked arround this by selecting the value using the Mysqlconnector in c# code
private long ReadMaxIdFromTable()
{
MySqlConnection connection = null;
try
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder();
var builderenv = builder.AddJsonFile("config/appsettings.json", optional: false, reloadOnChange: false)
.AddJsonFile($"config/appsettings.{environment}.json", false, false).AddEnvironmentVariables();
IConfigurationRoot configuration = builderenv.Build();
var connectionString = configuration.GetConnectionString("ConnectionStringName");
connection = new MySqlConnection(connectionString);
connection.Open();
var cmd = connection.CreateCommand() as MySqlCommand;
cmd.CommandText = #"SELECT MAX(`Id`) FROM `Table`";
var result = (long)cmd.ExecuteScalar();
return result;
}
catch (Exception)
{
throw;
}
finally
{
if (connection != null && connection.State != System.Data.ConnectionState.Closed)
{
connection.Close();
}
}
}
Is not as clean as I would like it but, it gets the job done.
Later I use SETVAL interpolating the value in the sql string again from c# code.
var currentSeq = ReadMaxIdFromTable();
migrationBuilder.Sql($"SELECT SETVAL(`MySequence`, {currentSeq}, true);");
Also, beware that all the Up() code it's executed BEFORE anything gets to the database, this means that SELECT MAX(Id) FROM Table; has to result in the value we're looking for before the migration starts manipulating the database.

SQLite Error in statement insert with question mark

Hello I am writing a query to insert values in DB SQLite.
Firstly, I read from my records and searched for two values, for example, then I created a new table and inserted new values in.
My code snippet follows:
try
{
Class.forName("org.sqlite.JDBC");
Connection con=DriverManager.getConnection("jdbc:sqlite:tests.db");
con.setAutoCommit(false);
Statement st=con.createStatement();
st.executeUpdate("delete msearch");
ResultSet res=st.executeQuery("select * from newmobile_details");
Boolean rec=res.next();
if(!rec)
{
JOptionPane.showMessageDialog(null,"لايوجد سجلات");
}
else
{
do
{
String mid=res.getString(1);
String model=res.getString(2);
String name=res.getString(3);
int price=res.getInt(4);
String pcolor=res.getString(5);
String imei=res.getString(6);
java.sql.Date date=res.getDate(7);
String access=res.getString(8);
if(mname.equalsIgnoreCase(name))
{
PreparedStatement prp=con.prepareStatement("insert into msearch values(?,?,?,?,?,?,?,?)");
prp.setString(1,mid);
prp.setString(2,model);
prp.setString(3,name);
prp.setInt(4,price);
prp.setString(5,pcolor);
prp.setString(6,imei);
prp.setDate(7,date);
prp.setString(8,access);
prp.executeUpdate();
System.out.println("iam inside2");
rows++;
b=1;
jTextField2.setText("");
}
}while(res.next());
if(b==0)
{
JOptionPane.showMessageDialog(null,"لم يتم العثور على الموبايل ");
jTextField2.setText("");
}
}
con.commit();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"The error is1:" +e);
}
I get only exception as below:
The error is1 :sql error or missing database in msearch syntax error
Larry makes a good point and looking at your code more carefully I believe the problem is with the statement
st.executeUpdate("delete msearch");
It seems to be saying you should be qualifying msearch with a database name.
This is the syntax for an insert statement
INSERT INTO TABLE_NAME (c1, c2, c3,...cN)
VALUES (v1, v2, v3,...vN);
You give a list of column names and a list of values.
You don't have the list of column names.
This can't work.

SQL lock row,table on insert and select

I am trying to achieve some sort of lock on sql.
To explain what am i doing simple:
One table with Id int autoincrement as PK, and one field Data varchar(max) non-clustered IX
Now i have some C# code that simlpy checks if the item isn't in the db, makes an insert
The sql code that i am using behind is like:
INSERT INTO {0}.{1} WITH (TABLOCKX) VALUES(#data...)
and the select one is:
SELECT Id FROM {0}.{1} WITH (TABLOCKX) WHERE(Data = #data)
But i can see that there are items with the same value inserted multiple times
TABLOCK creates deadlocks, and i dont want to use unique index because its very slow.
Is there a way to achieve this with locking?
I'm not sure it is what you want, I hope that this reply is helpful.
private void test(string aConnectionString, string aData)
{
using (SqlConnection sqlConnection = new SqlConnection(aConnectionString))
{
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
sqlCommand.Connection = sqlConnection;
sqlCommand.Transaction = sqlTransaction;
try
{
sqlCommand.CommandText = #"IF NOT EXISTS(SELECT Id FROM {0}.{1} WHERE Data = #Data)
BEGIN
INSERT INTO {0}.{1}
SELECT #Data
END";
sqlCommand.Parameters.Add("#Data", System.Data.SqlDbType.VarChar).Value = aData;
sqlTransaction.Commit();
}
catch (Exception ex)
{
sqlTransaction.Rollback();
}
}
}

SQL Server CE update or insert query

In my Windows Forms application, I'm using a SQL Server Compact database. I have a function in which I want to update the columns 'id' and 'name' in table 'owner', unless the specified id does not exist, in which case I want new values inserted.
For example, my current table has 'id' 1 and 2. It MIGHT have 'id' 3. User enters data to insert/update id 3.
I want my query to do something like this:
UPDATE owner
SET name = #InputN
WHERE id = 3
IF ##ROWCOUNT = 0
INSERT INTO owner (id, name) VALUES 3, #InputN
How should I define my query in order to make this work in SQL Server Compact Edition?
You should do it in your form codes. This way you don't even need to check if there is an di with the value=3. It will check it by itself and update the row if it exists. If not you won't get any errors.
RSSql.UpdateNonQueryParametric("update owner set name=? where id=3", newname);
public static void UpdateNonQueryParametric(string query, params Object[] parameters)
{
SqlCeParameter[] param = new SqlCeParameter[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
param[i] = new SqlCeParameter();
param[i].Value = parameters[i];
}
_cnt = new SqlCeConnection();
_cnt.ConnectionString = ConnectionString;
_cmd = new SqlCeCommand();
_cmd.Connection = _cnt;
_cmd.CommandType = System.Data.CommandType.Text;
_cmd.CommandText = query;
_cmd.Parameters.AddRange(param);
if (_cnt.State != System.Data.ConnectionState.Open)
_cnt.Open();
_cmd.ExecuteNonQuery();
_cmd.Dispose();
if (_cnt.State != System.Data.ConnectionState.Closed)
_cnt.Close();
_cnt.Dispose();
}

MS Access ##identity query issue

Can someone please let me know the issue with the below query? I am running on MS Access and its giving
Syntax error in query expression 'id = ##IDENTITY'
Code:
public DosageBO SaveDosage(DosageBO dosage)
{
try
{
using (IDbConnection connection = OpenConnection())
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("INSERT INTO dosage_master ( medicine_type, dosage, remarks, updateby, updatedate )");
sql.AppendLine("VALUES (#type, #dose, #remarks, #updateby, NOW());");
var parameters = new
{
type = dosage.MedicineType,
dose = dosage.Dosage,
remarks = dosage.Remarks,
updateby = Environment.UserName
};
connection.Execute(sql.ToString(), parameters);
return connection.Query<DosageBO>("SELECT medicine_type as MedicineType, dosage, remarks FROM dosage_master WHERE id = ##IDENTITY").FirstOrDefault();
}
}
catch
{
throw;
}
}
SELECT ##Identity is a specialized query. And ##Identity is only valid in that context. If you attempt to use ##Identity elsewhere, as in a WHERE clause, the db engine will throw an error.
You will have to retrieve the value from SELECT ##Identity, save it, and then use that saved value in your other query.
Remove the ) at the end
WHERE id = ##IDENTITY)
^---here
Are you inserting a row in this batch prior to the select query?
To my knowledge ##IDENTITY is only available directly after inserting a row causing an identity value to be generated i.e an insert to an autoincremental identity column.
Edit again:
Try enclosing it in a subquery e.g id = (SELECT ##IDENTITY)