sql server 存储过程如何调用存储过程
sqlserver2008中已存在一个存储过程P1,现在要通过存储过程P2在存储过程P1产生的数据中进行进一步的数据筛选。...
sql server 2008中已存在一个存储过程 P1,现在要通过存储过程P2在存储过程P1产生的数据中进行进一步的数据筛选。
展开
3个回答
展开全部
第一种方法: 使用output参数
USE AdventureWorks; GO
IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL DROP PROCEDURE Production.usp_GetList; GO
CREATE PROCEDURE Production.usp_GetList @product varchar(40) , @maxprice money
, @compareprice money OUTPUT , @listprice money OUT AS
SELECT p.name AS Product, p.ListPrice AS 'List Price' FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.name LIKE @product AND p.ListPrice < @maxprice; -- Populate the output variable @listprice. SET @listprice = (SELECT MAX(p.ListPrice) FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
WHERE s.name LIKE @product AND p.ListPrice < @maxprice); -- Populate the output variable @compareprice. SET @compareprice = @maxprice; GO
另一个存储过程调用的时候:
Create Proc Test as
DECLARE @compareprice money, @cost money
EXECUTE Production.usp_GetList '%Bikes%', 700, @compareprice OUT, @cost OUTPUT
IF @cost <= @compareprice BEGIN
PRINT 'These products can be purchased for less than $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.' END ELSE
PRINT 'The prices for all products in this category exceed $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二种方法:创建一个临时表
create proc GetUserName as begin
select 'UserName' end
Create table #tempTable (userName nvarchar(50)) insert into #tempTable(userName) exec GetUserName
select #tempTable
--用完之后要把临时表清空 drop table #tempTable
--需要注意的是,这种方法不能嵌套。例如:
procedure a begin ...
insert #table exec b end
procedure b begin ...
insert #table exec c select * from #table end
procedure c begin ...
select * from sometable end
--这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
--会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。
USE AdventureWorks; GO
IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL DROP PROCEDURE Production.usp_GetList; GO
CREATE PROCEDURE Production.usp_GetList @product varchar(40) , @maxprice money
, @compareprice money OUTPUT , @listprice money OUT AS
SELECT p.name AS Product, p.ListPrice AS 'List Price' FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.name LIKE @product AND p.ListPrice < @maxprice; -- Populate the output variable @listprice. SET @listprice = (SELECT MAX(p.ListPrice) FROM Production.Product p
JOIN Production.ProductSubcategory s
ON p.ProductSubcategoryID = s.ProductSubcategoryID
WHERE s.name LIKE @product AND p.ListPrice < @maxprice); -- Populate the output variable @compareprice. SET @compareprice = @maxprice; GO
另一个存储过程调用的时候:
Create Proc Test as
DECLARE @compareprice money, @cost money
EXECUTE Production.usp_GetList '%Bikes%', 700, @compareprice OUT, @cost OUTPUT
IF @cost <= @compareprice BEGIN
PRINT 'These products can be purchased for less than $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.' END ELSE
PRINT 'The prices for all products in this category exceed $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二种方法:创建一个临时表
create proc GetUserName as begin
select 'UserName' end
Create table #tempTable (userName nvarchar(50)) insert into #tempTable(userName) exec GetUserName
select #tempTable
--用完之后要把临时表清空 drop table #tempTable
--需要注意的是,这种方法不能嵌套。例如:
procedure a begin ...
insert #table exec b end
procedure b begin ...
insert #table exec c select * from #table end
procedure c begin ...
select * from sometable end
--这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
--会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。
展开全部
SqlConnection con = new SqlConnection("连接字符串");
SqlCommand cmd = new SqlCommand("SelectManStudent", con);
SqlParameter parm = new SqlParameter("@Sex", SqlDbType.NVarChar);
parm.Value = '男';//此处传性别
cmd.Parameters.Add(parm);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
//ds就是查出来的结果集
SqlCommand cmd = new SqlCommand("SelectManStudent", con);
SqlParameter parm = new SqlParameter("@Sex", SqlDbType.NVarChar);
parm.Value = '男';//此处传性别
cmd.Parameters.Add(parm);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
//ds就是查出来的结果集
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sda.Fill(ds);
con.Close();
//ds就是查出来的结果集
con.Close();
//ds就是查出来的结果集
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询