d
if(@stusex!='男' and @stusex!='女')
begin
raiserror('性别只能是男或女',16,100)
return
end
insert into students(studentname,studentsex,studentdepid,studentage) values(@stuname,@stusex,@depId,@stuage)
end
go
proc_students_insert '佳佳','春',1,1
select * from students
--模糊查询,动态构造sql语句
drop proc proc_students_querrylike
create proc proc_students_querrylike
@input varchar(20)
as
begin
declare @sql varchar(1024)
set @sql='select * from students where 1=1'
set @sql=@sql+'and studentname like ''%'+@input+'%'''
set @sql=@sql+'or studentsex like ''%'+@input+'%'''
set @sql=@sql+'or studentage like ''%'+@input+'%'''
set @sql=@sql+'or studentdepid in (select depid from department where depname like ''%'+@input+'%'')'
exec(@sql)
end
go
select * from students
select * from department
proc_students_querrylike '男'
--zhege有问题?
--T-sql 事务处理
if exists(select 1 from sys.databases where [name]='bank')
drop database bank
go
create database bank
go
use bank
go
create table moneycounts
(
name varchar(20),
moneys money check(moneys>1) not null
)
go
insert into moneycounts values('a',100)
insert into moneycounts values('b',100)
if exists(select name from sysobjects where name='proc_moneycounts_update' and type='p')
drop proc proc_moneycounts_update
go
create proc proc_moneycounts_update
@nameout varchar(20),
@namein varchar(20),
@money money
as
if not exists(select 1 from moneycounts where name=@nameout)
begin
print '转出账户不存在!'
return
end
if not exists(select 1 from moneycounts where name=@namein)
begin
print '转入账户不存在!'
return
end
if(@money>(select moneys from moneycounts where name=@nameout))
begin
print '余额不足!'
return
end
declare @errorsum int
--标记事务开始
begin tran
/*
一般把DML语句(select ,delete,update,insert语句)放在BEGIN TRAN...COMMIT TRAN
之间作为一个事务处理也就是那些语句执行过程中如果遇到错误,无论哪句遇到错误,
所有语句都会回滚到BEGIN TRAN之前的状态。
*/
update moneycounts set moneys=moneys-@money where name=@nameout
set @errorsum=@errorsum+@@error
update moneycounts set moneys=moneys+@money where name=@namein
set @errorsum=@errorsum+@@error
if(@errorsum>0)
begin
print '转账失败@@@';
rollback; --回滚事务
end
else
begin
print 'ok!!!';
commit; --提交事务
end
go
proc_moneycounts_update 'a','b',10
proc_moneycounts_update 'a','b',1000
select * from moneycounts
--视图和索引
--检查视图是否已存在
if exists(select 1 from sys.objects where name='view_recordInfo')
drop view view_recordInfo
go
/*创建视图,查看上机信息*/
create view view_recordInfo
as
select 记录号=recordInfo.recordId,卡号=cardInfo.CardNumber,机器号=PCInfo.PCId,
上机时间=recordInfo.beginTime,下机时间
SQL_SERVER数据库开发之存储过程应用_理学_高等教育_教育专区 暂无评价0人阅读0次下载举报文档 SQL_SERVER数据库开发之存储过程应用_理学_高等教育_教育专区。...
sql 数据库 第十二章 SQL Server 2008存储过程_IT/计算机_专业资料。SQL Server 数据库程序设计 授课教师: 授课教师:姜姗 存储过程和触发器掌握存储过程的定义...
SqlServer数据库备份存储过程_IT/计算机_专业资料。SqlServer数据库备份存储过程SqlServer 数据库备份存储过程: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ...
上一页 下一页 返回本章首页 第5章 数据库对象的操作 5.5.1 创建存储过程在SQL Server中,可以使用三种方法创建存储过 程: ①使用创建存储过程向导创建存储...
剖析SQL Server数据库扩... 4页 免费 数据库(sql)视图、存储过... 15页 1下载券 SQL--存储过程实现数据库... 3页 免费 【原创:数据库】SQL SE... ...
SQL Server——如何正确理解存储过程_IT/计算机_专业资料。SQL Server 如何正确理解存储过程存储过程也称为存储查询,是存储在数据库中预先定义的 SQL 语句。它可 ...
SQL Server存储过程及触发器详细讲解_IT/计算机_专业资料。sql server 存储过程详细讲解和触发器的实例,很有帮助,数据库的一个重难点。...
SQL Server 存储过程详解 ◆优点: 执行速度更快。存储过程只在创造时进行编译,...TEXT、 SET SHOWPLAN_XML、USE database_name 其他数据库对象均可在存储过程中...
第8章 存储过程 8.1 存储过程简介 创建存储过程 执行存储过程 修改和删除存储过程 8.2 8.3 8.4 Server数据库系统中 数据库系统中, 在 SQL Server 数据库...
SQL Server 存储过程入门案例详解作者: Jackie, 出处:IT 专家网,责任编辑: 王晓晨, 2008-07-17 12:56 有了存储过程之后,与数据库的交互就没有必要在程序中写...
热门文档