How can I use SQL functions in a NHibernate Update? - nhibernate

If certain conditions appear I want to set a field to the value of the MySQL function
NOW(). The condition is determinable shortly before the update (for example IInterceptor.OnSave or Session.SaveOrUpdate(myObj)). This action should happen in the Update statement of NHibernate where other fields are updated as well.
The resulting SQL could be similar to this:
UPDATE myTable
SET myStringCol = 'someVal', myDateCol = NOW()
WHERE myId = 123;
The following constraints should be satisfied, but I will take every solution which is practicable:
Querying NOW() before the update would flood the server with unnecessary requests
Unfortunately it's not possible to use SPs or triggers
It's not possible to use .Net DateTime.Now, it has to be the server time
Because of the conditions it's not possible to use the default-value for a table column
I know the possibility of custom SQL for create, update and delete (nhforge.org/doc/nh/en/index.html 17.3), but how to implement the conditions?
Using MySQL 5.1 / 5.5 / 5.6
Using the latest NH 3.3.2

You can use generated properties for INSERT and UPDATE
Please refer to the documentation, Chapter 5
http://nhibernate.info/doc/nh/en/index.html#mapping-generated

Related

How UPDATES Work in SQL Server When Values of Columns Have Not Changed

I have a quick question about updates that happen via s SQL Server stored procedure. My question is, if you explicitly use the UPDATE keyword in your stored procedure, that is looking at two different tables, for instance, will that UPDATE regardless of whether or not there is a difference in the values? Or will it ONLY run if there is a difference in values between columns? And will it selectively ONLY update the values that have changed, or the whole row, if it's found that any column is different between the two?
According to the ISO/ANSI SQL standards, all data manipulations, especially writings, must be done even if there is no changes in the values between the old and the new values…
To undestand why, thainks that this query :
UPDATE MyTable
SET Column1 = Column1
Will fire all the UPDATEs triggers and this will make a functionnal difference if the triggers had not been reached !
A +

How to decrement Auto column or Field by one using UPDATE SET on LibreOffice Base?

I have a table with auto increment column (ID) and have already filled my table with records. Then, after sometime I noticed that the auto increment (ID) column started from 2 instead of 1. I really wanted the count to start from 1. So, what I want to do is decrease the ID column by one for all the records using SQL statement UPDATE SET. I have used this SQL Statement on MySQL database and it worked. However, on LibreOffice base, it won't even allow me to execute Update statement saying that it is NOT a query. So, following is what I want to do.
UPDATE Accounts SET ID=ID-1;
Apparently, LibreOffice base doesn't like that sql statement. So, how can I do this?
It sounds like you tried to create a query, but that is not how to run an update command. Instead, go to Tools -> SQL and enter the following:
UPDATE "Accounts" SET ID=ID-1;
This was tested using the default HSQLDB engine.

Is there any simple way to set max id of ID column using trigger before insert in MS SQL Server

I've almost seen every post concerning this question but haven't captured the best one. Some of them recommend using Identity but some triggers to perform incrementing integer column.
I'd like also to use triggers as there will be more delete happen in my table in this case. In addition, as I have mainly come from Interbase DBMS where I used to create a before insert trigger on table this issue sucks until now as I migrated from Interbase to MS SQL Server.
This is how I did in Interbase
CREATE trigger currency_bi for currency
active before insert position 0
AS
declare variable m integer;
begin
select max(id)+1 from currency into :m;
if (:m is NULL ) then m=1;
new.id=:m;
end
So, as I should frequently use this, which is the best way to create a trigger that increments integer column using max(id)+1 ?
Don't use triggers to do this, it will either kill the performance or cause all sorts of concurrency problems, depending on your use of transactions and locking.
It's better to use one of mechanisms available in the engine -- identity property or sequence object.
If you're running a newer version of SQL Server, with sequence feature available, use sequence. It will allow you to reserve a range of ids from the client applcation, and assign them to new rows on the client, before sending them to server for insert.
Always use Identity option , because as you told that you frequently delete the record, in this case trigger will some time give wrong information ( Called Isolation level).
Suppose one transaction delete the highest one record and just before or same time your trigger fired. So it get the deleted highest record which is not exist after few second.
So when you fired select query, it show the gap which is wrong.
Sqlserver give the inbuilt mechanism of this type of situation with auto identity true option.
http://mrbool.com/understanding-auto-increment-in-sql-server/29171
You donot bother about this. Also draw back of trigger is if multiple insert happened, then it always fired after the last insert statement.
Try to never use trigger , as it is harmful and not controllable.
Still if you want , then add in your insert statement , not use trigger
How can I auto-increment a column without using IDENTITY?

Update a table and display updated rows with a single SQL command

I need your help with PostgreSQL. I have a homework in which I have to update a column of a table and display all the information that has been updated, but I have to use a single command to do all that. Even worse, I just can use basic clauses like SELECT, UPDATE, SET, WHERE etc.
Is this possible? I didn't find any example.
I've tried several combinations like:
SELECT * FROM customer
(UPDATE custumer SET bithdate = bithdate + INTERVAL '1 DAY'
WHERE bithcity = 'New York');
This didn't work!
Since this is homework, I'll leave something for you.
Single SQL statement
Start reading about the RETURNING clause of the UPDATE command in the manual.
Another alternative (for more complex scenarios) would be a data-modifying CTE. You still need the RETURNING clause for that.
Or you could create a function you can call in a single statement, but that would violate your requirement of only using simple DML commands.
Single transaction
If more than a single statement is allowed, another option would be a simple UPDATE plus simple SELECT wrapped into a transaction.

In SQL, is 'FROM' in 'DELETE FROM' optional if you plan to use 'WHERE'?

I'm new to SQL. We have some code that should work on SQL Server 2005/2008, Oracle 10 as well as Sybase.
I was writing a script to try to figure out which tables a given stored procedure modifies (but does not drop), e.g insert, update and delete.
The delete one turned out being puzzling - sometimes I see statements like:
delete phone_book where ...
as opposed to:
delete from phone_book where ...
So ... is the from keyword truly optional in this case? Does this cause any problems? Is it just a bad style, or does it not matter?
I have not found a reference to T-SQL that would make from optional. I suppose that this is what would unify all 3 vendors I mentioned above.
Questions/comments/links are welcomed (or is it welcome?).
At this place the FROM is optional (SQL Server, Oracle, Sybase).
However, there are subtle differences: Oracle for instance allows assigning an alias to the table name, where SQL Server doesn't; and other things are also a little bit different.
Also note that your FROM sample is differnet from the following where it is mandatory:
DELETE phone_book FROM some_table WHERE ...
Short Answer: Luceros answer is correct: it is optional
I have to maintain sql and adapt it between sql-server and Oracle. Here are some rules:
Write Scripts manually, don't use generated code.
Always use INSERT INTO.
Always DELETE -- without FROM.
Do not use " - quoted identifier.
Remove all [ ] and dbo. (Schema names)
Attention when you see DELETE ... FROM ...
Attention when you see UPDATE ... FROM ...
ORACLE Select statements need a from clause you can use from DUAL
OK you can script your objects and edit them in a standard way
USE [Current_DB] -- you don't want a reference to your test database go into production script
SET ANSI_NULLS ON -- decide once which settings to use -- don't switch on and off
SET QUOTED_IDENTIFIER ON -- quoted identifiers are case-sensitive.
INSERT INTO is required by Oracle.
That is my personal style don't use optional keyword, learn the defaults
You have to quote an identifier, if you use one of ORACLES reserved keywords as column name, we entered that pitfall and in the long run it would have been better to rename the column on the sql-Server side.
Oracle doesn't use these.
Oracle doesn't support this syntax.
Oracle doesn't support this syntax.
From the Microsoft SQL Server documentation, FROM is optional.
from is optional in delete from in those three DBMSes but it is mandatory according to the SQL standard. I would always use delete from to ease the migration of SQL code from one DBMS to another.
In SQL Server, FROM of DELETE FROM is optional and DELETE without FROM is not SQL standard while DELETE FROM is SQL standard.
I experimented DELETE FROM and DELETE without FROM on SQL Server, MySQL, PostgreSQL and SQLite as shown below:
Database
DELETE FROM
DELETE
SQL Server
Possible
Possible
MySQL
Possible
Impossible
PostgreSQL
Possible
Impossible
SQLite
Possible
Impossible
In addition, I also experimented INSERT INTO and INSERT without INTO on SQL Server, MySQL, PostgreSQL and SQLite as shown below.
Database
INSERT INTO
INSERT
SQL Server
Possible
Possible
MySQL
Possible
Possible
PostgreSQL
Possible
Impossible
SQLite
Possible
Impossible