https://www.cnblogs.com/jiangyunfeng/p/12154681.html 谢大哥
https://www.cnblogs.com/1219zy/p/8328940.html
USE CMS ----Use 是跳转到哪个数据库,对这个数据库进行操作。 GO ----GO向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号,相当于提交上面的SQL语句。 ----GO是把t-sql语句分批次执行。(一步成功了才会执行下一步,即一步一个GO) /****** Object: StoredProcedure [dbo].[PROC_four_five_hr] Script Date: 07/30/2018 13:44:55 ******/ ----这一段是介绍本存储过程名字和创建时间 ----/* */ 是注释掉很多行内容的符号 ----object 对象 的意思 ----storeprocedure 存储过程 的意思 ----dbo 是每个数据库的默认用户,具有所有者权限 ----dbo后面是存储过程的名称 ----script date 脚本 创建时间 SET ANSI_NULLS ON GO ---- SET QUOTED_IDENTIFIER ON GO ---- /*首先准备数据,测试存储过程 use ssqadm; --创建测试books表 create table books_test ( book_id int identity(1,1) primary key, book_name varchar(20), book_price float, book_auth varchar(10) ); --插入测试数据 insert into books_test (book_name,book_price,book_auth) values ('论语',25.6,'孔子'), ('天龙八部',25.6,'金庸'), ('雪山飞狐',32.7,'金庸'), ('平凡的世界',35.8,'路遥'), ('史记',54.8,'司马迁'); select * from books_test; */ --1.创建无参存储过程 if (exists (select * from sys.objects where name = 'getAllBooks')) drop proc getAllBooks go create procedure getAllBooks as begin select * from books_test; --调用,执行存储过程 exec getAllBooks; end go --2、修改存储过程 alter procedure getallbooks as select book_name from books_test; --3、删除存储过程 drop procedure getallbooks; go --4、修改存储过程的名称 sp_rename getallbooks,proc_get_allbooks; go exec proc_get_allbooks; go --5、创建带参数的存储过程 use ssqadm go if (exists (select * from sys.objects where name = 'searchbooks')) drop proc searchbooks -- exec searchbooks 1;--执行存储searchbooks得到如下结果: go create procedure searchbooks (@bookid int)--括号里面是 as begin declare @book_id int;----定义一个标量变量,只是保证存储过程的完整性,在本存储是多此一举的。 set @book_id = @bookid; select* from books_test where book_id = @book_id; end; go -- exec searchbooks 1;--执行存储searchbooks得到如下结果: --6、创建带两个参数的存储过程 use ssqadm go if (exists (select * from sys.objects where name = 'book_test2')) drop proc book_test2 -- exec book_test2 1;--执行存储book_test2得到如下结果: go create procedure book_test2 (@bookid int,@bookname varchar(20))--括号里面是 as begin declare @book_id int;----定义一个标量变量,只是保证存储过程的完整性,在本存储是多此一举的。 declare @book_name varchar(20); set @book_id = @bookid; set @book_name = @bookname; select* from books_test where book_id = @book_id and book_name = @book_name; end; go -- exec book_test2 1,'论语';--执行存储book_test2得到如下结果: --7、创建带有返回值的存储过程 use ssqadm go if (exists (select * from sys.objects where name = 'book_test3')) drop proc book_test3 go create procedure book_test3(@bookid int,@out_book_name varchar(20) output) as declare @book_id int; set @book_id = @bookid; begin select @out_book_name = book_name from books_test where book_id= @book_id; end go -- --执行存储book_test2得到如下结果: --执行存储过程 declare @out_name varchar(20) --声明一个变量用来接收执行存储过程后的返回值 exec book_test3 1,@out_name output select @out_name as out_book_name;--as是给返回的列值起一个名字
附带另一位大哥的调用
--根据loginId查询教员信息 if OBJECT_ID('SelectTeacherByLoginId') is not null drop procedure SelectTeacherByLoginId go create procedure SelectTeacherByLoginId @LoginId varchar(50) --参数 as select TeacherID, TeacherName,Sex,LoginId,LoginPwd,Birthday,UserState,t.UserStateId from Teacher t,UserState u where t.UserStateId=u.UserStateId and LoginId=@LoginId go --调用 exec SelectTeacherByLoginId 't001'
Vs中代码调用
#region 根据LoginID查询教员信息 //根据LoginID查询教员信息 public static Teacher SelectTeacherByLoginId(string loginId) { Teacher tea = null; string sql = "SelectTeacherByLoginId"; SqlDataReader dr = DBHelper.ExecteReader(sql, CommandType.StoredProcedure //调用存储过程 , new SqlParameter[] {new SqlParameter("@LoginId",loginId)} ); if (dr.Read()) { tea = new Teacher(); tea.TeacherID = Convert.ToInt32(dr["TeacherID"]); tea.LoginId = dr["LoginId"] + ""; tea.LoginPwd = dr["LoginPwd"] + ""; tea.TeacherName = dr["TeacherName"] + ""; tea.Sex = dr["Sex"] + ""; tea.UserState = dr["UserState"] + ""; tea.UserStateId = Convert.ToInt32(dr["UserStateId"]); tea.Birthday = Convert.ToDateTime(dr["Birthday"]); } dr.Close(); DBHelper.CloseCon(); return tea; } #endregion