What kind of command is TRUNCATE in SQL ANSI - sql

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.

Related

Pro*C can't force parallel DML while sqlplus can?

Whenever I issue 'alter session force parallel DML;' from Pro*C I get an error. Although, it works fine from sqlplus for the same user. Is there any reason for that?
This isn't directly related to Pro*C. The error ORA-12841: Cannot alter the session parallel DML state within a transaction happened in Pro*C because it had already performed DML, whereas a new SQL*Plus session did not. Moving the COMMIT or the ALTER statement should resolve the issue.
Thanks to Hemant K Chitale and €$ħ₪ on the Oracle Forum: https://community.oracle.com/message/10002348
Are you sure that the trailing semicolon ';' is part of the SQL statement?
Usually it only tells sqlplus that the SQL statement end here, but the semicolon is not part of the SQL syntax (except for PL/SQL)

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.

In SQL Server, when should you use GO and when should you use semi-colon ;?

I’ve always been confused with when I should use the GO keyword after commands and whether a semi-colon is required at the end of commands. What is the differences and why/when I should use them?
When I run the Generate-script in SQL Server Management Studio, it seems to use GO all over the place, but not the semi-colon.
GO only relates to SSMS - it isn't actual Transact SQL, it just tells SSMS to send the SQL statements between each GO in individual batches sequentially.
The ; is a SQL statement delimiter, but for the most part the engine can interpret where your statements are broken up.
The main exception, and place where the ; is used most often is before a Common Table Expression Statement.
The reason why you see so many GO's in Generated DDL scripts is because of the following rule about batches.
CREATE DEFAULT, CREATE FUNCTION,
CREATE PROCEDURE, CREATE RULE, CREATE
TRIGGER, and CREATE VIEW statements
cannot be combined with other
statements in a batch. The CREATE
statement must begin the batch. All
other statements that follow in that
batch will be interpreted as part of
the definition of the first CREATE
statement.
One of the use cases for Generated DDL is to generate multiple objects in a single file. Because of this a DDL generator must be able to generate batches. As others have said the GO statement ends the batch.
GO
Go is a batch separator. This means that everything in that batch is local to that particular batch.
Any declarations of Variables, Table Variables, etc do not go across GO statements.
#Temp tables are local to a connection, so they span across GO statements.
Semicolon
A Semicolon is a statement terminator. This is purely used to identify that a particular statement has ended.
In most cases, the statement syntax itself is enough to determine the end of a statement.
CTE's however, demand that the WITH is the first statement so you need a semicolon before the WITH.
You should use a semi-colon to terminate every SQL statement. This is defined in the SQL Standards,
Sure, more often than not SQL Server allows you to omit the statement terminator but why get into bad habits?
As others have pointed out, the statement preceding a common table expression (CTE) must be terminated with a semi-colon. As a consequence, from folk who have not fully embraced the semi-colon terminator, we see this:
;WITH ...
which I think looks really odd. I suppose it makes sense in an online forum when you can't tell the quality of code it will be pasted into.
Additionally, a MERGE statement must be terminated by a semi-colon. Do you see a pattern here? These are a couple of the newer additions to TSQL which closely follow SQL Standards. Looks like the SQL Server team are going down the road of mandating the use of the semi-colon terminator.
GO is a batch terminator, a semi-colon is a statement terminator.
you will use GO when you want to have multiple create proc statements in 1 script because create proc has to be the first statement in a batch. If you use common table expressions then the statement before it needs to be terminated with a semi-colon

mysql/sql server: SELECT ##identity

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

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