1 函数:
2
3 create function Fun_Test(@TypeName nvarchar(300))
4
5 return @TableTest(id,int)
6
7 as
8
9 begin
10
11 insert @TableTest select id from sysobjects where type=@TypeName
12 return
13
14 end
15
16 函数调用:
17
18 select * from Fun_Test('P')
19 删除:
20
21 drop function Fun_Test
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 sysobjects :系统对象表(保存当前数据库的对象)
2 select * from sysobjects where type='C'
3 字段:
4
5 type ,xtype:
6
7 取值
8
9 C = CHECK 约束
10
11 D = 默认值或 DEFAULT 约束
12
13 F = FOREIGN KEY 约束
14
15 FN = 标量函数
16
17 IF = 内嵌表函数
18
19 K = PRIMARY KEY 或 UNIQUE 约束
20
21 L = 日志
22
23 P = 存储过程
24
25 R = 规则
26
27 RF = 复制筛选存储过程
28
29 S = 系统表
30
31 TF = 表函数
32
33 TR = 触发器
34
35 U = 用户表
36
37 V = 视图
38
39 IT = 内部表
40
41 PC = 程序集 (CLR) 存储过程
42
43 PK = PRIMARY KEY 约束(type 为 K)
44
45 SN = 同义词
46
47 SQ = 服务队列
48
49 TA = 程序集 (CLR) DML 触发器
50
51 TT = 表类型
52
53 UQ = UNIQUE 约束(type 为 K)
sysobjects
1 视图:
2 create view V_Test
3 as
4 begin
5
6 select * from Test
7 end
8
9
10 存储过程:
11 create proc P_Test
12 @name nvarchar(300)
13 ,@result nvarchar(300) output
14 as
15 begin
16 select @result =result from Test
17 return result
18 end
1 create trigger tri_Test
2
3 on {Test}
4
5 {for | After | Instead of } [ insert, update,delete ]
6 ---After Inserted和Updated临时表插入数据和对物理表操作后,执行触发器
7 ---Instead of Inserted和Updated临时表插入数据, 不对物理表操作后,直接执行触发器
8 as
9 begin
10
11
12 ----异常处理
13 begin try
14 ----事务管理
15 begin Tran
16
17 insert into Test(name) values('a');
18 --继续事务
19 If @@ERROR>0
20 GoTo TranRollback
21
22 begin end
23 begin catch
24 select error_message();
25 GoTo TranRollback
26
27 end catch
28
29 TranRollback:
30 If @@ERROR>0 Or @@ROWCOUNT<>1
31 Rollback Tran --如果发生错误则滚回事务
32 Else
33 Commit Tran --如果没有发生错误则提交事务
34 Go
35
36
37 end
38
39 drop trigger tri_Test