个人收藏存储过程入门

CREATE PROCEDURE CustOrderHist_HYS @CustomerID nchar(5)
AS
SELECT ProductName,Total=sum(Quantity)
FROM Products P, [Order Details] OD, Orders O, Customers C
WHERE C.CustomerID = @CustomerID
AND C.CustomerID = O.CustomerID AND O.OrderID = OD.OrderID AND OD.ProductID = P.ProductID
GROUP BY ProductName
exec CustOrderHist_HYS BOTTM
CREATE PROCEDURE HUAYONGSHENG @customerId char(5)
as
select productName ,total
select OrderID,total=sum(Quantity)
from dbo.[Order Details]
group by OrderID

create proc HUA @Id int
as
select Quantity
from dbo.[Order Details]
where OrderID=@Id
group by OrderID
drop proc hua
exec HUA 10249
create proc yong @id int , @pid int
as
begin
insert into dbo.[Order Details](orderId,productId) values(@id,@pid)
end
drop proc yong
exec yong 100,100

======================================================================
create procedure proc_Sex
@manCount int output, --输出参数 男生总人数
@womenCount int output, --输出参数 女生总人数
@count int output, --输出参数 男女生总人数
@sex nvarchar(4) --输入参数 男或女
as
select  @count=count(*) from StuInfo
print '@count='+convert(varchar(10),@count)

if(@sex='女')
begin
select  @womenCount=count(*) from StuInfo where stuSex='女'
set @manCount=@count-@womenCount
                     -- print '@manCount='+convert(varchar(10),@manCount)
        end
else
begin
select  @manCount=count(*) from StuInfo where stuSex='男'
set @womenCount=@count-@manCount
                    --  print '@womenCount='+convert(varchar(10),@womenCount)
end
go

drop proc proc_Sex

exec proc_Sex  0,0,0, '女'

程序代码如下:

public int[] CallProcedure(String stuSex){
    this.getConnDB();
    int[] count = new int[3];

    try {
CallableStatement csmt=conn.prepareCall("{call proc_Sex(?,?,?,?)}");

//注册OUT类型输出参数
csmt.registerOutParameter(1,Types.INTEGER);//男生总人数 输出参数
csmt.registerOutParameter(2, Types.INTEGER);//女生总人数 输出参数
csmt.registerOutParameter(3, Types.INTEGER);//男妇生总人数 输出参数

csmt.setString(4, stuSex);//设置存储过程输入参数
csmt.execute();

//得到输出参数的值

count[0] =csmt.getInt(1);
count[1]=csmt.getInt(2);
count[2]=csmt.getInt(3);

} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
    this.closeDB();
    }
return count;
    }
  









你可能感兴趣的:(C++,c,C#,Go)