IFEXISTS(SELECT*FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[User]')ANDtypeIN('U'))DROPTABLE[dbo].[User]
GO
CREATETABLE[dbo].[User]([id]intNOTNULL,[name]nchar(10)COLLATE Chinese_PRC_CI_AS NULL,[did]intNULL)
GO
ALTERTABLE[dbo].[User]SET(LOCK_ESCALATION =TABLE)
GO
-- ------------------------------ Records of User-- ----------------------------INSERTINTO[dbo].[User]([id],[name],[did])VALUES(N'1', N'one ',NULL)
GO
INSERTINTO[dbo].[User]([id],[name],[did])VALUES(N'2', N'tow ',NULL)
GO
INSERTINTO[dbo].[User]([id],[name],[did])VALUES(N'3', N'three ',NULL)
GO
INSERTINTO[dbo].[User]([id],[name],[did])VALUES(N'4', N'faure ', N'1')
GO
INSERTINTO[dbo].[User]([id],[name],[did])VALUES(N'5', N'five ', N'1')
GO
-- ------------------------------ Primary Key structure for table User-- ----------------------------ALTERTABLE[dbo].[User]ADDCONSTRAINT[PK_user]PRIMARYKEYCLUSTERED([id])WITH(PAD_INDEX =OFF, STATISTICS_NORECOMPUTE =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS =ON, ALLOW_PAGE_LOCKS =ON)ON[PRIMARY]
GO
if(exists(select*from sys.objects where name='proc_get_user'))dropproc proc_get_user
go
createproc proc_get_user
as--CreateUser:一棵小白杨select*from TestC.dbo.[User];
2.1.1执行该存储过程
exec proc_get_User
2.2创建带参数的存储过程
--带参存储过程if(object_id('proc_find_user','P')isnotnull)dropproc proc_find_user
go
createproc proc_find_user(@startIdint,@endIdint)asselect*from TestC.dbo.[User]where id between@startIdand@endId
go
2.2.1执行该存储过程
exec proc_find_user 2,4;
2.3带通配符参数存储过程
--带通配符参数存储过程if(object_id('proc_findUserByName','P')isnotnull)dropproc proc_findUserByName
go
createproc proc_findUserByName(@namevarchar(20)='%t%',@nextNamevarchar(20)='%')asselect*from TestC.dbo.[User]where name like@nameand name like@nextName;
go
if(object_id('proc_getUserRecord','P')isnotnull)dropproc proc_getUserRecord
go
createproc proc_getUserRecord(@idint,--默认输入参数@namevarchar(20)out,--输出参数@d_idint output--输入输出参数)asselect@name= name,@d_id=did from TestC.dbo.[User]where id =@idand did =@d_id;
go
--WITH RECOMPILE 不缓存if(object_id('proc_temp','P')isnotnull)dropproc proc_temp
go
createproc proc_temp
with recompile
asselect*from TestC.dbo.[User];
go
2.5.1执行该存储过程
exec proc_temp;
2.6不缓存存储过程
--加密WITH ENCRYPTION if(object_id('proc_temp_encryption','P')isnotnull)dropproc proc_temp_encryption
go
createproc proc_temp_encryption
with encryption
asselect*from TestC.dbo.[User];
go
2.6.1查看该存储过程
2.7带游标的存储过程
if(object_id('proc_cursor','P')isnotnull)dropproc proc_cursor
go
createproc proc_cursor
@curcursorvarying output
asset@cur=cursor forward_only static forselect id, name from TestC.dbo.[User];open@cur;
go