日期:2014-05-18  浏览次数:20444 次

初学 sql存储过程和触发器
sql server 
存储过程的 流控制语言具体怎么用?

请大家举个列子详细 解释下哈

还有触发器的 print方面的也举个列子(列如:成绩大于100或小于0 则不能保存)……等等……  
谢谢 谢谢大家啊!


------解决方案--------------------
这段在我网上,先给你参考:
SQL code
4. 程序流程控制

    打开查询分析器,在查询窗口输入下列程序,理解其中条件语句、循环语句、等待语句的写法。运行程序时,注意查询分析器状态栏上的执行时间提示(程序保存为s6_4.sql)。

/*
随机产生十个两位数,求出奇数和偶数的个数及各自的和,并延时输出
*/

DECLARE @a int, @b1 int, @b2 int, @c1 int, @c2 int, @i int

SET @i=0
SET @b1=0
SET @b2=0
SET @c1=0
SET @c2=0

WHILE @i<10
  BEGIN
    SET @a=floor(89*rand())+10
    PRINT @a
    IF @a % 2=0
      BEGIN
        SET @b1=@b1+@a
        SET @c1=@c1+1
      END
    ELSE
      BEGIN
        SET @b2=@b2+@a
        SET @c2=@c2+1
      END
    SET @i=@i+1
  END
WAITFOR DELAY '000:00:10'
SELECT @c1 as 偶数个数, @b1 as 偶数和, @c2 as 奇数个数, @b2 as 奇数和
GO

------解决方案--------------------
晴天大大出书了?
什么书?
改明儿去买一本!
------解决方案--------------------
表示关注您的出书!
------解决方案--------------------
菜鸟的我在学习中
------解决方案--------------------
存储过程(feixianxxx)
http://topic.csdn.net/u/20091127/21/10a70c07-8683-4f9e-be7e-2415fa8f6956.html?seed=296887941&r=61490884#r_61490884

有关存储过程的讨论
http://topic.csdn.net/u/20080511/21/ffe1bd32-fff2-4067-bcf9-cadea806ac8f.html?1953379669

常用存储过程语法收藏 
http://topic.csdn.net/u/20090216/10/fca7534f-e881-4e37-b9b7-8fe141ee186b.html?15784
------解决方案--------------------
触发器综述
http://topic.csdn.net/u/20081005/11/57061a18-c234-40ee-ba4b-1f4c3bc7f09a.html

T-MAC学习笔记19之--浅谈触发器
http://topic.csdn.net/u/20091203/20/ef22e48d-4560-437e-9500-6efa4044284d.html?41106

SQL code
--触发器的操作1

create table 化验室纱组(本厂编号 int,客户 int,色号 int,纱支 int)
create table 化验室布组(本厂编号 int,客户 int,色号 int,布类 int)
go
create trigger my_trig on 化验室纱组 for insert ,update ,delete
as
if not exists(select 1 from inserted)
   delete 化验室布组 from deleted t where 化验室布组.本厂编号 = t.本厂编号 
else if not exists(select 1 from deleted) 
   insert into 化验室布组(本厂编号 ,客户 ,色号) select 本厂编号 ,客户 ,色号 from inserted
else
   update 化验室布组 set 客户 = t.客户 , 色号 = t.色号 from inserted t where 化验室布组.本厂编号 = t.本厂编号
go

--1、insert 对化验室纱组插入数据,然后查看化验室布组表的数据
insert into 化验室纱组 values(1 , 2 , 3 , 4)
insert into 化验室纱组 values(5 , 6 , 7 , 8)
go
select * from 化验室布组
/*
本厂编号        客户          色号          布类          
----------- ----------- ----------- ----------- 
1           2           3           NULL
5           6           7           NULL

(所影响的行数为 2 行)
*/

--2、update , 更改化验室纱组表中本厂编号=1的色号=6
update 化验室纱组 set 色号 = 6 where 本厂编号 = 1
go
select * from 化验室布组
/*
本厂编号        客户          色号          布类          
----------- ----------- ----------- ----------- 
1           2           6           NULL
5           6           7           NULL

(所影响的行数为 2 行)
*/

--3、delete 化验室纱组表中本厂编号=1的那条数据
delete from 化验室纱组 where 本厂编号 = 1
go
select * from 化验室布组
/*
本厂编号        客户          色号          布类          
----------- ----------- ----------- ----------- 
5           6           7           NULL

(所影响的行数为 1 行)
*/

drop table 化验室纱组 , 化验室布组

------解决方案--------------------
SQL code
drop table classname 
declare @TeacherID int 
declare @a char(50) 
declare @b char(50) 
declare @c char(50) 
declare @d char(50) 
declare @e char(50) 
set @TeacherID=1 

select @a=DRClass1, @b=DRClass2, @c=DRClass3, @d=DRClass4, @e=DRClass5 from Teacher Where TeacherID = @TeacherID 

create table classname(classname char(50)) 
insert into classname (classname) values (@a) 
if (@b is not null) 
begin 
insert into classname (classname) values (@b) 

if (@c is not null) 
begin 
insert into classname (classname) values (@c) 

if (@d is not null) 
begin 
insert into classname (classname) values (@d) 
if (@e is not