DEV Community

Haku B.
Haku B.

Posted on

SQL Learning notes

  • sys.tables, sys.columns, add new column to a table after checking if it's existing cross 2 tables
IF EXISTS (
        SELECT *
        FROM   sys.tables [Table]
        JOIN   sys.columns [Column]
               ON [Column].object_id = [Table].object_id
        WHERE  [Table].[name] = 'Table1'
        AND    [Column].[name] = 'Column1'
) BEGIN
    ALTER TABLE Table1
    DROP COLUMN Column1
END

ALTER TABLE Table1
ADD Column1 nvarchar(max) NULL
Enter fullscreen mode Exit fullscreen mode

sys.tables returns a row for each user table in SQL Server.

sys.columns returns a row for each column of an object that has columns, such as views or tables. (Object types that have columns: Table-valued assembly functions, Inline table-valued SQL functions, Internal tables, System tables, Table-valued SQL functions, User tables, Views.)
object_id - int - ID of the object to which this column belongs.

More reading: https://www.sisense.com/blog/sql-cheat-sheet-retrieving-column-description-sql-server/

  • COALESCE Evaluates the arguments in order and returns the current value of the first expression that initially doesn't evaluate to NULL.
SELECT COALESCE(NULL, NULL, 'third_value', 'fourth_value');
-- Above query returns the 3rd value because it is the first value isn't null
Enter fullscreen mode Exit fullscreen mode
  • SQL Operators

    • Arithmetic operators: +, -, *, /, %
    • Bitwise operators: & (Bitwise AND), | (Bitwise OR), ^ (Bitwise exclusive OR)
    • Comparison operators: =, >, <, >=, <=, <> (Not equal to)
  • [] : used to delimit identifiers.

    From MSDN:

Delimited identifiers

Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers may or may not be delimited.

SELECT *
FROM [TableX]         --Delimiter is optional.
WHERE [KeyCol] = 124  --Delimiter is optional.

Identifiers that do not comply with all of the rules for identifiers must be delimited in a Transact-SQL statement.

SELECT *
FROM [My Table]      --Identifier contains a space and uses a reserved keyword.
WHERE [order] = 10   --Identifier is a reserved keyword.
  • GO

GO will execute the related sql commands n times.

The GO command isn't a Transact-SQL statement, but a special command recognized by several MS utilities including SQL Server Management Studio code editor.

The GO command is used to group SQL commands into batches which are sent to the server together. The commands included in the batch, that is, the set of commands since the last GO command or the start of the session, must be logically consistent. For example, you can't define a variable in one batch and then use it in another since the scope of the variable is limited to the batch in which it's defined.

For more information, see http://msdn.microsoft.com/en-us/library/ms188037.aspx.
More reading: https://stackoverflow.com/questions/2299249/what-is-the-use-of-go-in-sql-server-management-studio-transact-sql

Top comments (0)