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

MSSQL
Microsoft SQLServer的版本区别及选择
在SQL Server数据库中为标识(IDENTITY)列插入显式值
访问和更改关系数据,使用MSSQL外联接
一个查看MSSQLServer数据库空间使用情况的存储过程 SpaceUsed
SQL语句去掉重复记录,获取重复记录
复习一下sql server的差异备份
SQL中object_id函数的用法
SQL Server日期计算
找回SQL企业管理器里的SQL连接的密码的方法
mssql数据库系统崩溃后的一般处理步骤与方法
海量数据库的查询优化及分页算法方案
SQL Server连接中三个常见的错误分析
在程序中压缩sql server2000的数据库备份文件的代码
MS SQL SERVER 数据库日志压缩方法与代码
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
将Session值储存于SQL Server中

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 48 ::
收藏到网摘: 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后面重新做一些申明的,而不是放在开头整个存储过程的变量定义。