当前页面: 开发资料首页 → JSP 专题 → 求一过程调用语句,SqlServer2000的
求一过程调用语句,SqlServer2000的
摘要: 求一过程调用语句,SqlServer2000的
求一SQL语句
有两个表:
product表 productid商品编号 ,productname商品名称
sale表 saleid报价记录编号,productid报价商品编号(与product表关联),seller报价商店名称,price价格
选出商品编号,商品名称,最高和最低报价,最高和最地报价商店名称
最好用过程调用写
小第水平次,想了好长时间也没想出来
谢谢了
sql如下:
--第一次选出商品编号,商品名称,最高和最低报价
select a.productid,productname,max(price) max_p,min(price) min_p
from x_product a,x_sale b
where a.productid=b.productid
group by a.productid,productname
into temp aa;
create index aa_idx1 on aa(productid); --如果记录数很多,应该建立该索引,记录数不大的话,就不必建了
--第二次选出最高价报价商店
select aa.*,b.seller from aa,x_sale b
where aa.productid=b.productid
and aa.max_p=b.price
into temp bb;
--最后选出全部内容
create index bb_idx1 on bb(productid); --如果记录数很多,应该建立该索引,记录数不大的话,就不必建了
select bb.productid,bb.productname,max_p,bb.seller,min_p,b.seller
from bb,x_sale b
where bb.productid=b.productid
and bb.min_p=b.price;
以上是取数的sql,如果用过程,见下一回帖
drop procedure get_xx;
create procedure get_xx(product_id integer)
returns integer,char(10),decimal,char(10),decimal,char(10);
define product_name char(10);
define max_p decimal;
define max_seller char(10);
define min_p decimal;
define min_seller char(10);
select productname,max(price) max_p,min(price) min_p
into product_name,max_p,min_p
from x_product a,x_sale b
where a.productid=b.productid and a.productid=product_id
group by productname;
select seller into max_seller from x_sale
where x_sale.productid=product_id and x_sale.price=max_p;
select seller into min_seller from x_sale
where x_sale.productid=product_id and x_sale.price=min_p;
return product_id,product_name,max_p,max_seller,min_p,min_seller;
end procedure;
执行语句如下:
execute procedure get_xx(1); --其中1是商品编号