热门文章
 
sql求两字符串相似度的自定
在 Access 中使用“存
ASP调用带参数存储过程
ASP与存储过程
实用的存储过程1
实用的存储过程2
SQL Server编写存储
SQL Server编写存储
在ASP存储过程的使用方法实
asp使用存储过程初步技巧
 推荐文章
 
asp使用存储过程初步技巧
ASP调用带参数存储过程
sql求两字符串相似度的自定
在 Access 中使用“存
ASP与存储过程
实用的存储过程1
实用的存储过程2
SQL Server编写存储
数据分页的存储过程
在ASP存储过程的使用方法实
存储过程入门到精通
asp调用orcle存储过程
存储过程介绍 和它在asp中
ASP和MSSQL存储过程的
系统存储过程sp_depen
asp:SQL存贮过程样板
 
你现在的位置:您现在的位置是: 中国ASP>>数据库>>存储过程
SQL语句和存储过程 查询语句的流程控制

作者信息

Author : 菜菜

Team : FreeXploiT

Date : 2005-07-06

Site : http://blog.csdn.net/freexploit

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个变量,实际上应该只提供一个变量就可以了,www.aspxuexi.com,asp学习网。

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

写好的存储过程如下


create PROCEDURE Pr_GetClass
@TeacherID int
as

Declare @a char(50), @b char(50), @c char(50), @d char(50), @e char(50)
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
go



2 连表查询

我有三个表
KJ表
KJID
TeacherID
..........................................................


Teacher表

TeacherID
TeacherName
CollageID
.........................................................

Collage表

CollageID
CollageName

我想写一个SQL语句,查询所有的KJ,根据KJ的TeacherID查到TeacherName,同时根据TeacherID查到Teacher,Teacher的CollageID查到Collage,最后生成的数据集里KJ的属性里除了本身的KJName以外,还想加上TeacherName,CollageName。


语句如下

SELECT T1.KJName, T2.TeacherName, T3.CollageName
FROM KJ T1
LEFT JOIN Teacher T2 ON T2.TeacherID=T1.TeacherID
LEFT JOIN Collage T3 ON T3.CollageID=T2.CollageID



相关信息:

数据库查询语言sql(2)
数据库查询语言SQL(1)
sql求两字符串相似度的自定义函数
在 Access 中使用“存储过程”
ASP调用带参数存储过程
ASP与存储过程
实用的存储过程1
实用的存储过程2
SQL Server编写存储过程小工具

 

中国ASP技术 ASP.ORG.CN 版权所有 2004-2008