Microsoft SQL Server


This draft deletes the entire topic.

expand all collapse all

Examples

  • 32

    DML ("Data Manipulation Language") includes operations such as INSERT, UPDATE and DELETE:

    -- Create a table variable, which can be treated like a normal table
    
    DECLARE @HelloWorld TABLE (
        ID INT IDENTITY,
        Description VARCHAR(1000)
    )
    
    
    -- DML Operation - Insert, Inserting a row into the table
    INSERT INTO @HelloWorld (Description) VALUES ('Hello, World!')
    
    
    -- Displaying the table 
    SELECT * FROM @HelloWorld  
    
    -- Select a specific column from table
    SELECT Description FROM @HelloWorld
    
    
    -- Display number of records in the table
    SELECT Count(*) FROM @HelloWorld
    
    
    -- DML Operation - Update, Updating a specific row in the table
    UPDATE @HelloWorld SET Description = 'Hello World!' WHERE ID = 1
    
    
    -- Selecting the table. See how the DESCRIPTION changed after the UPDATE?
    SELECT * FROM @HelloWorld
    
    
    -- DML Operation - Delete, Deleting a row from the table
    DELETE FROM @HelloWorld WHERE ID = 1
    
    
    -- Selecting the table. See table content after DELETE operation 
    SELECT * FROM @HelloWorld
    

    In this script we're creating a table variable to demonstrate some basic queries.

    The @ symbol in the example above needs to be used as a prefix for all variables you declare in T-SQL. Its purpose is to distinguish between a variable and a table, e.g.

    USE Northwind;
    GO
    SELECT TOP 10 * FROM Customers 
    ORDER BY CompanyName
    

    will select the first 10 records of the Customer table, ordered by the column CompanyName from the database Northwind:

    Northwind database query

    Note that Use Northwind; changes the default database for all subsequent queries. You can still reference the database by using the fully qualified syntax:

    SELECT TOP 10 * FROM Northwind.dbo.Customers 
    ORDER BY CompanyName
    
    SELECT TOP 10 * FROM Pubs.dbo.Authors
    ORDER BY City
    

    This is useful if you're querying data from different databases. Note that dbo, specified "in between" is called a schema and needs to be specified always when using the fully qualified syntax. If it is not changed, dbo is the default schema. The default schema may be omitted. All other user defined schemas need to be specified.

    If the database table contains columns which are named like reserved words, e.g. Date, you need to enclose the column name in brackets, like so:

    -- descending order
    SELECT TOP 10 [Date] FROM dbo.MyLogTable
    ORDER BY [Date] DESC
    

    The same applies if the column name contains spaces in its name (which is not recommended). An alternative syntax is to use double quotes instead of square brackets, e.g.:

    -- descending order
    Select top 10 "Date" from dbo.MyLogTable
    order by "Date" desc 
    

    is equivalent but not so commonly used. Notice the difference between double quotes and single quotes: Single quotes are used for strings, i.e.

    -- descending order
    Select top 10 "Date" from dbo.MyLogTable
    order by "Date" desc 
    where UserId='johndoe'
    

    is a valid syntax. Notice that T-SQL has a N prefix for NChar and NVarchar data types, e.g.

    SELECT TOP 10 * FROM Northwind.dbo.Customers 
    ORDER BY CompanyName
    WHERE CompanyName like N'AL%'
    

    returns all companies having a company name starting with AL (% is a wild card, use it like you would use the asterisk in a DOS command line, e.g. DIR AL*).

  • 10

    Syntax:

    SELECT *
    FROM table_name
    

    Using the asterisk operator * serves as a shortcut for selecting all the columns in the table. All rows will also be selected because this SELECT statement does not have a WHERE clause, to specify any filtering criteria.

    This would also work the same way if you added an alias to the table, for instance e in this case:

    SELECT *
    FROM Employees AS e
    

    Or if you wanted to select all from a specific table you can use the alias + " .* ":

    SELECT e.*, d.DepartmentName
    FROM Employees AS e
        INNER JOIN Department AS d 
            ON e.DepartmentID = d.DepartmentID
    

    Database objects may also be accessed using fully qualified names:

    SELECT * FROM [server_name].[database_name].[schema_name].[table_name]
    

    This is not necessarily recommended, as changing the server and/or database names would cause the queries using fully-qualified names to no longer execute due to invalid object names.

    Note that the fields before table_name can be omitted in many cases if the queries are executed on a single server, database and schema, respectively. However, it is common for a database to have multiple schema, and in these cases the schema name should not be omitted when possible.

  • 7
    UPDATE HelloWorlds
    SET HelloWorld = 'HELLO WORLD!!!'
    WHERE Id = 5
    

    The above code updates the value of the field "HelloWorld" with "HELLO WORLD!!!" for the record where "Id = 5".

    Note: In an update statement, It is advised to use a "where" clause to avoid updating the whole table unless and until your requirement is different.

Please consider making a request to improve this example.

Remarks

This is a set of examples highlighting basic usage of SQL Server.

Versions

VersionRelease Date
SQL Server 20162016-06-01
SQL Server 20142014-03-18
SQL Server 20122011-10-11
SQL Server 2008 R22010-04-01
SQL Server 20082008-08-06
SQL Server 20052005-11-01
SQL Server 20002000-11-01
Still have a question about Getting started with Microsoft SQL Server? Ask Question

Topic Outline