写SQL存储过程里的一些常用函数
声明1个变量:
DECLARE @id INT
声明多个变量:
DECLARE @id INT, @name varchar(10)
给变量赋值:
SET @id = 15
--------------------------------------------------------------------
IF语句写法(值为string类型的,用单引号扩着,如 @id = 'aaa'):
if @id = 1
begin
//sql语句
end
else if @id = 2
begin
//sql语句
end
else
begin
//sql语句
end
--------------------------------------------------------------------
CASE语句进行多条件判断:
简单Case函数
CASE sex
WHEN 1 THEN '男'
WHEN 2 THEN '女'
ELSE '其他' END
Case搜索函数
CASE WHEN age= 1 THEN '周岁婴儿'
WHEN age < 12 THEN '儿童'
WHEN age >= 18 THEN age <= 30 THEN '青年'
ELSE '非青年或老年' END
--------------------------------------------------------------------
WHILE循环语句:
DECLARE @rmb INT
SET @rmb = 1
WHILE @rmb < 10 BEGIN
set @rmb = @rmb + 1
END
--------------------------------------------------------------------
创建临时表:
Create TABLE #temp(
UID int identity(1, 1) PRIMARY KEY,
UserName varchar(16),
Pwd varchar(50),
Age smallint,
Sex varchar(6)
)
--------------------------------------------------------------------
打开临时表:
Select * from #temp