mysql/sql server: SELECT ##identity - sql

is there a difference in the way this functions between the two databases mysql and sql server:
SELECT ##identity

I prefer SCOPE_IDENTITY() in SQL Server to avoid issues with triggers.

Yes: Since that doesn't appear to be in the ANSI SQL standard, it is left to the DBMS vendor to roll their own methods.
mysql_insert_id()

Related

DB2 Select clause or using cursor

For reading bulk data from within a db2 function, is it preferred to read it via a general select clause or is it better to use here a cursor in relation to performance?

What kind of command is TRUNCATE in SQL ANSI

in SQL ANSI Truncate is a function or a statement ?
What kind of command is TRUNCATE in SQL ANSI ?
It's a statement, specifically a data manipulation (DML) statement, like DELETE or INSERT. It's basically DELETE FROM with no WHERE, except most engines handle logging the command differently from DELETE FROM.
I'm not sure about ANSI SQL, but in Microsoft SQL Server, TRUNCATE is a statement.

ODBC universal way to get deleted rows in trigger

Is there any universal way to get the deleted/inserted/updated rows in trigger? I want to create one trigger (I mean one pice of code in my application), which should work on Oracle, Sql Server and Postgres. Is it possible? I'm using odbc drivers to connect to different dbs.
It's working in SQL Server, but not in Oracle.
CREATE TRIGGER schema.trg_nameON schema.table_name
FOR DELETE
AS
BEGIN
DECLARE #id BIGINT;
SELECT #id = d.ID
FROM deleted d
DELETE
FROM schema.table_name
WHERE TOP_ID = #id;
END
no, each RDMS will have its own syntax. The syntax you used is a sql server syntax.
what do you mean by create trigger on the application? Please provide us with more info

Do I have to write the "GO" word in order to execute an SQL server statement?

I have little to no experience with TSQL and SQL Server - so in MySQL when I want to execute a statement I simply write:
Select * from users
...and then hit ENTER.
However now I see many SQL Server tutorials that you have the GO word immediately after each statement. Do I have to write this? For example:
Select * from users; GO
Or I can simply write:
Select * from users; <enter key pressed...>
In SQL Server, go separates query batches. It's optional in most situations.
In earlier versions of SQL Server, you had to do a go after altering a table, like:
alter table MyTable add MyColumn int
go
select MyColumn from MyTable
If you didn't, SQL Server would parse the query batch, and complain that MyColumn didn't exist. See MSDN:
SQL Server utilities interpret GO as a
signal that they should send the
current batch of Transact-SQL
statements to an instance of SQL
Server. The current batch of
statements is composed of all
statements entered since the last GO,
or since the start of the ad hoc
session or script if this is the first
GO.
GO separates batches, as Andomar wrote.
Some SQL statements (e.g. CREATE SCHEMA) need to be the first or only statements within a batch. For example, MSDN states
The CREATE PROCEDURE statement cannot
be combined with other Transact-SQL
statements in a single batch.
Local variables are also limited to a batch, and therefore are not accessible after a GO.
Go is optional, no need to write that in your sql statements.
You don't have to. What the GO will do is execute each statement (at least in Sql Server)
As the other answerers said before me, you don't really NEED Go.
There is only one case when you have to use it, and that's when you want to create a table or view and then select from it.
For example:
create view MyView as select * from MyTable
go
select * from MyView
Without Go, Sql Server won't execute this because the select statement is not valid, because the view doesn't exist at that moment.

SQL Server 2008: No 'release savepoint' for current transaction

I'm using a PHP database abstraction layer to work with both MySQL and SQL Server. MySQL has a 'release savepoint' statement which SQL Server does not support, and I can't find a comparable statement within T-SQL to use in its stead. Does anybody know of a way around this, or can the lack of functionality be safely ignored?
I'd appreciate any insight!
Cheers
In SQL Server you do not need to do any operation to release a savepoint. Savepoints are 'released' automatically at the final transaction commit or rollback, you don't need to manage them intermediately.
I don't know much about MySQL but it sounds a bit like using
Save Transaction <Name> and Rollback Transaction <Name>
to partially rollback a transaction to a named point. See MSDN
The ANSI standard syntax is SAVEPOINT , ROLLBACK TO SAVEPOINT , and RELEASE SAVEPOINT . Oracle, DB/2, MySQL, Postgres, Sybase, Informix, Interbase, and Firebird all use that same, standard, syntax. SQL Server is the oddball with a different syntax and no "release".
As Remus Rusanu said, though, it's not strictly required, but could help the database manage internal resources better if it knew that the savepoint was no longer necessary (it certainly helps on multigenerational architectures like Oracle, Interbase and Firebird).