4. Transact_Sql language basics

Grammatical conventions

 

Identifier

Custom regular identifier (legally named)

-Starting with a letter or other language characters _, @, #

Split identifier (when there are spaces in the identifier, symbols that do not meet the basic rules, and the system reserves words, use a separator to identify)

Custom identifier

(1) The first character of the identifier must be one of the following characters.
The letters defined in the Unicode 2.0 standard include the Latin letters az and AZ, as well as alphabet characters in other languages. Underscore "_", symbol "@" or number symbol "#".
(2) The following characters of the identifier can be the following three types.
The letters defined in the Unicode 2.0 standard. Decimal numbers from Latin alphabet or scripts from other countries. "@" Sign, dollar sign "$", number sign "#" or underscore "_".
(3) The identifier is not allowed to be a reserved word of Transact-SQL.
(4) No spaces or other special characters are allowed
 
Delimiter
 Identifier in double quotes ("")
 Identifiers in square brackets ([])
• The identifier may not conform to the format rules of the identifier, such as [MR GZGLXT], there are spaces between MR and GZGLXT, but because square brackets are used, they are regarded as delimited identifiers. For example: [USER]
• Note: The number of characters contained in the regular identifier and the delimited identifier must be between 1 and 128. For a local temporary table, the identifier can have a maximum of 116 characters.

 

System reserved words

Similar to many other languages, SQL Server 2014 uses more than 180 reserved keywords (Reserved Keyword) to define, operate, or access databases and database objects.
These reserved keywords are part of the T-SQL syntax and are used to analyze and understand the T-SQL language, including DATABASE, CURSOR, CREATE, INSERT, BEGIN, etc.
• In general, you cannot use these reserved keywords as object names or identifiers.
 
constant
Constants are values ​​whose values ​​remain unchanged during the running of the program.
1. String constant
'Hello'. 'Anyang City', N 'Henan Province' (Unicode constant)
2. Numeric constants
Integer constants 500, 899, 0x23 (hexadecimal number)
Real constants
Fixed point constant 3.6415
floating point constant 0.56E-3
3. Date time constant
‘1990/2/28’、’20-02-02’
4. Currency constant
56.45 (do not add ¥, $ in the table) ¥ 890, $ 893
 
variable
Variable refers to the amount that changes with the running of the program during the running of the program. It can save the query result and the return value of the stored procedure, and can also be used in the query.
Global variables
System-defined, no need to declare, use directly
Beginning with @@
For example: @@ error (error number of the last sql statement)
• Bureau of Ministry of variables
User defined, scope is only inside the program
DECLARE variable name variable type
– At the beginning
All variables are set to NULL after declaration
 
Global variables
 
Variable assignment
There are two ways to assign values ​​to variables: use SET statements to directly assign values ​​to variables and SELECT statements to select values ​​in the table to assign values ​​to variables. The syntax is as follows:
Format 1: Assignment using SET statement
SET variable name = expression
Format 2: Assignment using SELECT statement
SELECT variable name = expression [,… n]
Description:
 The expression can be any valid SQL expression;
A SELECT statement can assign values ​​to multiple variables, and a SET statement can only assign values ​​to one variable at a time.
 
Operator
Operator precedence
When the levels of the operators are different, the higher-level operators are operated first, and then the lower-level operators are operated. When the levels of operators in an expression are the same, the operations are generally performed from left to right. When there are parentheses in the expression, the expression in the parentheses should be evaluated first; if there are nested parentheses in the expression, the expression with the deepest nesting is evaluated first.
 

 

expression

Field expression after the table name field expression
• The target expression after the select statement
Conditional expressions, conditional expressions after the where statement
Conditional expression
1. Compare sizes>
2. Specify the range between and
3. Collection in
4. Character matching like '<match string>'
5. Null value is null
6. Multiple conditions and or
 
Wildcard
Wildcards in the T-SQL language can replace one or more characters. Wildcards must be used in conjunction with the LIKE operator.
 
Comment
1. Single line comment
When the double hyphen "-" is used as a comment character, everything from the double hyphen to the end of the line is the comment content.
2. Multi-line comments
When using the comment character "/ * * /" as a multi-line comment character, everything from the beginning "/ *" to "* /" is a comment.
These comment characters can be used for multi-line text or code blocks.

 

Data Definition Language DDL

Data Definition Language DDL (Data Definition
Language): Realize the definition function of SQL. Used to define the logical structure of the database, including basic tables, views and indexes. The basic DDL includes three categories: definition, modification and deletion.
 
Data Manipulation Language DML
Realize the data manipulation function of SQL. Including two types of operations: data query and data update. Among them, data query refers to query, statistics, grouping, sorting and other operations on the data in the database; data update includes three operations of insert and delete modification.
 
Data Control Language DCL
Used to grant / cancel control rights to data objects to users, to authorize, recycle or deny access through GRANT, REVOKE and DENY. Database control refers to the security and integrity control of the database. SQL data control includes authorization of basic tables and views, description of integrity rules, and control statements such as transaction start and end.
 
Other basic sentences
Data statement
Variable declaration DECLARE variable name data type
The first character of the urine variable must be @; all variables are in the sound
Set the initial value to NULL after tomorrow
Data assignment
Assignment in two ways: SET and SELECT
SET @ variable = value (SET can only assign a variable)
SELECT @variable 1 = value 1, @variable 2 = value 2, ...
Data output
PRINT Urine variable (character type) or string
SELECT Urine variable AS custom column name

Data control statement

Flow control statements are statements used to control program execution and flow branching.
BEGIN……END
IF……ELSE
CASE
WHILE (CONTINUE、BREAK)
GOTO
WAITFOR
RETURN
 
BEGIN……END )begin……end
A statement block composed of multiple Transact-SQL statements can be regarded as a statement.
BEGIN
{sql_statement |statement_block}
END
Among them, {sql_statement | statement_block} is any valid Transact-SQL statement or statement group defined by the statement block
 
if...else
 
case statement
1. Simple Case Statement
Case <condition judgment expression>
when condition judgment expression result 1 then <Transact-SQL command line or block
Statement>
when condition judgment expression result 2 then <Transact-SQL command line or block
Statement>
when condition judge expression result n then <Transact-SQL command line or block
Statement>
else <Transact-SQL command line or block statement>
end
 
2.Case search statement
Case
when conditional expression 1 then <Transact-SQL command line or block statement>
when conditional expression 2 then <Transact-SQL command line or block statement>
when conditional expression n then <Transact-SQL command line or block statement>
else <Transact-SQL command line or block statement>
end
 
 

while statement

 

waitfor statement

• The Waitfor statement is used to suspend the execution of the statement until the specified time point or specified time interval.
WAITFOR DELAY ’ time’|TIME ’ time’
Role:
(1) Delay execution at intervals
(2) Specify when to execute from, used to specify the trigger block, stored procedure and transaction execution time
 
return statement
 
• RETURN [integer expression]
• The RETURN statement is used to unconditionally exit from a query, stored procedure, or batch, and the statement will not execute after the RETURN statement
Row.
• (1) The stored procedure can return an integer value to the calling procedure or application. When used in a stored procedure, the RETURN statement cannot return a null value;
• (2) The system stored procedure returns a value of 0 to indicate success, and a non-zero value indicates failure.

 

 
Published 105 original articles · Like 536 · Visits 70,000+

Guess you like

Origin blog.csdn.net/qq_41934990/article/details/105475520