当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQL语句和存储过程 查询语句的流程控制

MSSQL
用SQL创建数据库
无法用TCP/IP协议连接远端SQL Server数据库问题
SQL Server中各个系统表的作用
SQL中的五种数据类型
使用Sql server进行分布式查询
使用sql server中的全文索引
T-SQL 查询中使用的函数之系统函数
SQL 的一些核心语句
缩小SQL Server数据库日志
怎样配置SQL Server发送电子邮件
无数据库日志文件恢复数据库方法两则(1)
无数据库日志文件恢复数据库方法两则(2)
恢复SQL Server系统数据库
利用事务日志来恢复Update、Delete误操作引起的数据丢失
问:如何将 XML 文件导入 SQL Server 2000?
关于SQL Server中几个未公布的访问注册表的扩展存储过程
SQL Server安全规划全攻略
SQL注入漏洞全接触--入门篇(一)
SQL注入漏洞全接触--入门篇(二)
SQL注入漏洞全接触--进阶篇(一)

MSSQL 中的 SQL语句和存储过程 查询语句的流程控制


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 74 ::
收藏到网摘: n/a

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 null)
begin
insert into classname (classname) values (@e)
end
end
end
end

select * from classname

以上这些SQL语句能不能转成一个存储过程?我自己试了下
ALTER PROCEDURE Pr_GetClass

@TeacherID int,
@a char(50),
@b char(50),
@c char(50),
@d char(50),
@e char(50)
as

select @a=DRClass1, @b=DRClass2, @c=DRClass3, @d=DRClass4, @e=DRClass5 from Teacher Where TeacherID = @TeacherID
DROP TABLE classname
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 null)
begin
insert into classname (classname) values (@e)
end
end
end
end

select * from classname
但是这样的话,这个存储过程就有6个变量,实际上应该只提供一个变量就可以了

主要的问题就是自己没搞清楚 @a,@b,@C,@d 等是临时变量,是放在as后面重新做一些申明的,而不是放在开头整个存储过程的变量定义。