TRANSACT-SQL can be referred to "Transact-SQL Reference" (TSQL.HLP) (Shredders "T-SQL")
suggestion:
It is best to use uppercase when writing SQL Script.
Note:
The syntax format here is only a common format, not a SQL standard format, and see "T-SQL" in the standard format.
(No practical significance in the example in the example)
select
SELECT
SELECT can select the specified data column
Such as:
SELECT *
SELECT [Name] from syscolumns
When there is a system reserved word in SQL, "[]" is caused, or there is special characters in SQL, "[]" is caused,
Such as:
SELECT [Object Name] from Objects
At the time of using an alias, you should also pay attention to the above principles, and the alias can be used with the following two methods:
COLUMN_NAME AS Alias
Column_name alias
The middle AS can be omitted
You can use the condition selection syntax in SELECT to see "Conditions" below
Such as:
SELECT [Name], Xtype, Case When Xtype = 'u' TEN 'User Table' Else Case When Xtype = 'S' THEN 'System Table' End End As Type from Sysobjects
Return to the table:
Name
Xtype
Types of
Syscolumns
S
System table
TableDefine
U
user table
Separate two queries synthesized separate returns:
UIY keyword
Such as SELECT A, B from Table1
Unoin
SELECT C, D from Table2
Description:
When using union, if there is no ALL parameter, the same record will be approved by default.
Such as:
Table1
Table2
Id
TF1
Value1
Id
TF2
Value2
1
A
10
5
A
10
5
B
20
6
Di
twenty one
2
A
30
3
C
31
3
C
40
1
B
41
SELECT TF1, VALUE1 from Table1
Union
SELECT TF2, Value2 from Table2
Return to the table:
TF1
Value1
A
10
B
20
A
30
C
40
Di
twenty one
C
31
B
41
It can be seen that a record of "TF2 = A, Value2 = 10"
But with the following query
SELECT TF1, VALUE1 from Table1
Union all
SELECT TF2, Value2 from Table2
Return to the table:
TF1
Value1
A
10
B
20
A
30
C
40
A
10
Di
twenty one
C
31
B
41
Just this query will return all records
This problem may appear on the report statistics, such as an employee has done the same product and data in different dates, but will be less a record when using non-ALL mode.
Common with INTO
SELECT .... INTO B from A
You can store the specified data of a table in B table
Application Type:
Backup data table:
SELECT * INTO TABLE1_BAK from Table1
Create a new table
SELECT * INTO new_TABLE1 from Table1 Where 1 <> 1
SELECT TOP 0 * INTO new_TABLE1 from Table1
Save query results
Select Field1, Field2 INTO Result from Table1 Where Id> 1000 Create a new table and add an automatic serial number in a new table
One table has some tables that require an automatic number to distinguish between various lines.
Select Id, 1, 1) AS Autoid, * INTO new_TABLE1 from Table1
Where Identity function description:
format:
Identity (
Parameter Description:
DataType: Data type, depending on the number of types, usually set the INT type, specifically refer to the limit parameters of SQL
SEED: Start value, the beginning of the base, default is 1
Increment: increment, step size, interval between data, default is 1
The above SQL is said, the automatic number starts from 1 and add 1 per line.
The returned table is:
AutoID
Field1
Field2
1
Hello
Joy
2
Hello
Tom
3
Hi
Lily
4
Hello
Lily
Note:
Identity can also set up when you create a table
format:
Identity ([Seed, INCREMENT])
Such as:
Create a table
Create Table Table1
Autoid Int Idness (1, 1), or Autoid Int Idnessity
Field1 nvarchar (30),
FIELD2 NVARCHAR (30)
)
Modification table
ALTER TABLE TABLE1 Add Autoid Int Idness (1, 1)
Pay attention to the setting of Identity_Insert attribute when performing data insertion
If set identity_insert