- 1、本文档共8页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
sqlserver2008关于游标存储过程等的总结
数据库基础知识
By新浪微博矢马特
1.游标的使用
--定义的时候赋值
declare test_cursor cursor for
select * from 学生表
--先定义后赋值
declare @test_cursor cursor
set @test_cursor = cursor for
select * from 学生表
--打开游标
open test_cursor
2.表变量
--定义
declare @a table(number int,name nvarchar(100))
--插入
insert into @a (number, name)
values(123,谢俊)
--查询
select * from @a
--临时表区别
--①不可以建立索引
--②重新编译的存储过程较少,需要锁定和记录的资源较少。
--③在初始declare后不可以更改表定义。
--④作用域中引用,因此exec和sq_executesql将在引用范围之外。
3.存储过程
Procedure:是一组为了完成特定功能的sql语句集合,经编译后存储在数据库中,用户通过调用存储过程的名称和给出相应的参数来执行。
系统存储过程,是系统创建的存储过程。
常用的系统存储过程有:
Exec sp_databases -–查看数据库
Exec sp_tables --查看表
Exec sp_columns student --查看列
Exec sp_helpindex student --查看索引
Exec sp_helpconstraint student -–查看约束
Exec sp_stored_procedure
Exec sp_helptext ‘sp_stroed_procedure’ -–查看存储过程创建定义语句
Exec sp_rename student, stuinfo –修改表、列、索引的名称
Exec sp_renamedb myTempdb,mydb –更改数据名称
Exec sp_helpdb master
--更改表名称
exec sp_rename Table_1,Table1
select * from Table1
--更改列名
exec sp_rename Table1.gender,gende,column
exec sp_help Table1
--更改索引名称
exec sp_rename Table_3.PK_Table_3, pk3,index
exec sp_help Table_3
--查询所有存储过程
select * from sys.objects where type = P;
select * from sys.objects where type_desc like %pro% and name like sp%;
--创建存储过程
IF(EXISTS(select * from sys.objects where name = pro_Table1))
drop proc pro_Table1
go
create proc pro_Table1
as
select * from Table1
--执行存储过程
exec pro_Table1
--修改存储过程
alter proc pro_Table1
as
select * from 学生表
exec pro_Table1
--带参数的的存储过程
if (exists(select * from sys.objects where name = pro_stu_name))
drop proc pro_stu_name
create proc pro_stu_name(@p int)
as
select * from 学生表 where @p = 学生班级
exec pro_stu_name 1
--带通配符参数的存储过程
if(OBJECT_ID(pro_tpf,p)is not null)
drop proc pro_tpf
create proc pro_tpf(@p nvarchar(40) = 4%)
as
select * from 学生表 where 学生姓名 like @p
go
exec pro_tpf
exec pro_tpf %a
--带输出参数的存储过程
if(OBJECT_ID(pro_output,p)is not null)
drop proc pro_output
create proc pro_output(@id int, @name nvarchar(20) out,@age in
文档评论(0)