存储过程语法如下:
CREATE PROC [ EDURE ] procedure_name [ ; number ]
[ { @parameter data_type }
[ VARYING ] [ =
default
] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS
sql_statement [ ...n ]
简单实例:
1、创建存储过程
Create PROCEDURE WeekDay
as
--下面sql为sql_statement
begin
Declare
@week nvarchar(20),
@today int
begin
set @today=3
set @week=case
when @today=1 then '星期一'
when @today=2 then '星期二'
when @today=3 then '星期三'
when @today=4 then '星期四'
when @today=5 then '星期五'
when @today=6 then '星期六'
when @today=7 then '星期日'
else '值错误'
end
print @week
end
end
2、执行存储过程
exec WeekDay
常用sql语句:1)if语句
- DECLARE @d INT
- set @d = 1
- IF @d = 1 BEGIN
- PRINT '正确'
- END
- ELSE BEGIN
- PRINT '错误'
- END
2)多条件选择语句
- declare @today int
- declare @week nvarchar(3)
- set @today=3
- set @week= case
- when @today=1 then '星期一'
- when @today=2 then '星期二'
- when @today=3 then '星期三'
- when @today=4 then '星期四'
- when @today=5 then '星期五'
- when @today=6 then '星期六'
- when @today=7 then '星期日'
- else '值错误'
- end
- print @week
3)循环语句
- DECLARE @i INT
- SET @i = 1
- WHILE @i<1000000 BEGIN
- set @i=@i+1
- END
4)游标
- 变量赋值:1)set @a=5 2)
select
@user1=
'张三';
select
@user2 =
Name
from
ST_User
where
ID=1 3)
update
ST_User
set
@user3 =
Name
where
ID=1
- 触发器
- 标量函数:https://www.cnblogs.com/BrokenIce/p/5713225.html