Click here to Skip to main content
11,584,556 members (72,811 online)
Click here to Skip to main content

Tagged as

MSSQL Statements Syntax

, 13 Jun 2011 CPOL 10.1K 1
Rate this:
Please Sign up or sign in to vote.
MSSQL Statements Syntax

Many times, I forget the exact syntax I should use whether I'm writing in MSSQL, My-SQL or Oracle. This post is the first out of three posts designed to gather basic database statements and show a simple and clean example.

Select Statement

Syntax 1

SELECT column_1, column_3
FROM Table

Example

SELECT ID, Name
FROM tbl_customers

Syntax 2

SELECT * FROM Table

(meaning - select all columns).

Example

SELECT * FROM tbl_customers

Update Statement

Syntax 1

UPDATE Table
column_1 = 'value'

Example

UPDATE tbl_customers
Name = 'Elad'

This is a very bad example for UPDATE statement since this one will update -ALL- the names to 'Elad'.

Syntax 2

UPDATE Table
column_1 = 'value'
WHERE column_2 = 'Dependency_Term'

Example

UPDATE tbl_customers
Name = 'Elad'
WHERE ID = 13 AND Name = 'Noam'

Insert Statement

Syntax 1

INSERT INTO Table
VALUES ('value', 'value', 'value')

Example 1

INSERT INTO tbl_customers
VALUES (15, 'Elad', 28, 1)

This is a very bad example for INSERT statements since in this one you will have to write values for every column in you table. Even if it's nullable.

Syntax 2

INSERT INTO Table (column_1, column_2, column_3)
VALUES ('value_1', 'value_2', 'value3')

Example 2

INSERT INTO tbl_customers (Name, Age, Gender)
VALUES ('Elad', 28, 1)

Delete Statement

Syntax 1

DELETE FROM Table

Example 1

DELETE FROM tbl_customers

This is a very bad example for DELETE statement since this one will clean your table but will keep your rows number record. The following syntax is better for deleting a specific row:

Syntax 2

DELETE FROM Table
WHERE column_1 = 'value'

Example 2

DELETE FROM tbl_customers
WHERE ID = 13

Select Into Statement

The SELECT INTO statement is designed to copy data from one table to another.

Syntax 1

SELECT column_1, column_2
INTO Table_2
FROM Table_1

Example 1

SELECT ID, Name
INTO tbl_affiliates
FROM tbl_customers

Syntax 2

    SELECT * 
    INTO Table_2
    FROM Table_1

Example 2

    SELECT * 
    INTO tbl_affiliates
    FROM tbl_costumers

This will copy all of table_2 data to table_1.

Hope it helps.

Elad,
Lead Developer at Sports Betting Tech.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DevAffair
Team Leader Sports Betting Tech
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionmistake Pin
valhoti@yahoo.com22-Jun-11 5:13
membervalhoti@yahoo.com22-Jun-11 5:13 
QuestionA couple of points. Pin
smcnulty200020-Jun-11 14:56
membersmcnulty200020-Jun-11 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150707.1 | Last Updated 13 Jun 2011
Article Copyright 2011 by DevAffair
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid