Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the PostgreSQL equivalent to the TSQL "go" statement?

In T-SQL, I can state:

IF EXISTS (SELECT name 
           FROM   sysobjects 
           WHERE  name = 'tableName') 
  DROP TABLE [dbo].[tableName] 
go 

What is the batch terminator equivalent (i.e. "go") for the following SQL command?

DROP TABLE IF EXISTS tableName
like image 437
Joseph Idziorek Avatar asked Jun 09 '15 00:06

Joseph Idziorek


People also ask

Does PostgreSQL use T-SQL?

PostgreSQL provides a language called PL/pgSQL which is often used to write functions as well as procedures. The MS SQL equivalent to PL/pgSQL is called T-SQL (Transact-SQL).

What SQL is used in PostgreSQL?

PostgreSQL is an advanced, enterprise class open source relational database that supports both SQL (relational) and JSON (non-relational) querying.

Is PostgreSQL syntax same as SQL?

Despite the overwhelming popularity of MySQL, PostgreSQL may be a better choice because its syntax most closely conforms to Standard SQL. This means that you can easily translate your skills to other database management systems such as MySQL or SQLite.


3 Answers

From SQL Server documentation:

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

The syntax permits to have a number, after the keyword go, to repeat previous lines a number of times. Syntax is:

GO [count]

It separates batch. There is not an equivalent for PostgreSQL. The semicolon ends a statement, not a batch. And it is not possible to specify a number of executions.

Go references: https://msdn.microsoft.com/it-it/library/ms188037.aspx

like image 189
user_0 Avatar answered Oct 26 '22 14:10

user_0


The PostgreSQL equivalent would be:

DROP TABLE IF EXISTS tableName;

So the terminator is simply the SQL standard semi-colon ;.

like image 22
Patrick Avatar answered Oct 26 '22 13:10

Patrick


It depends. GO is separator (delimiter) - it is special keyword that is not used in any SQL statement. PostgreSQL uses semicolon ; as separator. In console psql you can use \g

postgres=# select 10 as a
postgres-# \g
+----+
| a  |
+----+
| 10 |
+----+
(1 row)

but it is not used often. Sometimes people uses \gset that execute SQL statement and store result to psql local variables. PostgreSQL can use semicolon, because PostgreSQL SQL statements has not to contain this symbol - It is different against T-SQL, because T-SQL allows some procedural constructs directly in SQL - and then T-SQL requires special separator. PostgreSQL doesn't allow it - procedural code is entered as string - and it is separated by apostrophes or custom string separators. So procedural conditional drop can looks like:

DO $$BEGIN
  IF EXISTS (SELECT * FROM information_schema.tables 
               WHERE table_name = 'someTable') -- attention CASE SENSITIVITY
  THEN
    DROP TABLE "someTable"; -- attention CASE SENSITIVE syntax
  END IF; 
END $$;

or more simply DROP TABLE IF EXISTS someTable (case insensitive syntax). I used custom string separator $$

DO $$ -- DO command with start of string (started by custom separator)
...   -- some procedural code
$$ ;  -- end string by custom separator and semicolon as end of DO command
like image 5
Pavel Stehule Avatar answered Oct 26 '22 14:10

Pavel Stehule