Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding SQL Definition syntax

Tags:

sql

sql-server

I am pretty novice in writing SQL but would like to get better. One of the things that I have never really understood is how to interpret the construct syntax for any give object. Here is an example of OPENROWSET definition taken from https://msdn.microsoft.com/en-us/library/ms190312.aspx. I understand the pipe "|" represents "OR" but not sure about the other tags. Is there a good place to learn how to interpret this?

OPENROWSET 
( { 'provider_name' , { 'datasource' ; 'user_id' ; 'password' 
   | 'provider_string' } 
   , {   [ catalog. ] [ schema. ] object 
       | 'query' 
     } 
   | BULK 'data_file' , 
       { FORMATFILE = 'format_file_path' [ <bulk_options> ]
       | SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} ) 

<bulk_options> ::=
   [ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ] 
   [ , ERRORFILE = 'file_name' ]
   [ , FIRSTROW = first_row ] 
   [ , LASTROW = last_row ] 
   [ , MAXERRORS = maximum_errors ] 
   [ , ROWS_PER_BATCH = rows_per_batch ]
   [ , ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) [ UNIQUE ]
like image 760
Will Avatar asked May 05 '16 13:05

Will


2 Answers

On every page of Microsoft Docs (previously MSDN) that refers to SQL Server (even about OPENROWSET) there is a link called Transact-SQL Syntax Conventions (@Ghost post it in a comment)

Transact-SQL Syntax Conventions link

That link will guide you to the page with explanation of SQL Syntax Conventions that you seek.

like image 180
gofr1 Avatar answered Sep 24 '22 20:09

gofr1


I think the easiest way to understand this syntax is visual Railroad Diagram.

How to use it:

  1. Get syntax from documentation
  2. Paste to Grammar Translator written by Colin Daley
  3. You may need to manually correct some errors (like missing brackets, '')
  4. Click Generate Railroad Diagram
  5. Learn by moving from start to end

enter image description here

Image generated using: http://bottlecaps.de/rr/ui


You can also search for Railroad Diagrams for SQL Server.

SQL Server CREATE TABLE syntax diagrams

Most important syntax:

  • Optional items are enclosed in square brackets
  • Groups of expressions are enclosed in curly braces
  • A '|' is a binary operator meaning 'or'.
like image 27
Lukasz Szozda Avatar answered Sep 23 '22 20:09

Lukasz Szozda