1、批处理
两个 go语句
begin...end语句——用于if...else与while等语句中
2、选择结构
if...else语句
case...
when...then...
else...
end
3、数据类型
创建:
create type ...from...[NULL||NOT NULL]
删除:
drop type ...
4、变量
局部变量
declare @a int,@b int//定义局部变量
set @a=1//赋值
select @b=2
print @a+@b//输出
select @a-@b
全局变量
以@@开头
5、运算符与表达式
运算符
‘abc’+‘def’ 即 abcdef
不等于:<>
6、循环结构
while
while(...)
begin
循环体
end
break
结束while循环
continue
跳过continue语句后的语句,回到while的布尔表达式继续执行
waitfor
暂停sql语句
waitfor delay‘00:01’//暂停1分钟 waitfor time‘22:20’//在晚上10:20时
return
终止查询
7、系统内置函数
字符串函数
ASCII(String) | 返回最左端字符的ASCII值 |
char(int) | 将ASCII码转换成字符 |
lower(Sring) | 转为小写 |
upper(String) | 转为大写 |
STR函数
STR(数值,总长度,小数位数)【默认总长度(小数点、符号、数字、空格)为10,小数位数为0】
STR(123.456):123
STR(123.456,7,3):123.456
STR(123.456,5,3):123.7
STR(123.456,2):**(2<整数位数)
去空格函数
LTRIM(String) | 去字符串头部空格 |
RTRIM(String) | 去字符串尾部空格 |
取子串函数
left(String,int)
right(String,int)
substring(String,index,length)
字符串操作函数
replace(string1,string2,string3):用3替换1中与2一样的的字符串
space(int):返回用指定数目的空格组成的字符串
stuff(string1,index,length,string2):删除length长度的1,并在index处插入2
数学函数
日期和时间函数
8、自定义函数