经常会遇见把存储过程的返回值放置到定义的临时表中,
现在先创建一个存储过程:
存储过程
1 CREATE PROC [dbo].[usp_GetOrderInfo] 2 @CustomerID nchar(5), 3 @StartDate datetime, 4 @EndDate datetime, 5 @rowsAffect int output 6 AS 7 BEGIN 8 SET NOCOUNT ON; 9 DECLARE @err int; 10 SELECT oo.CustomerID,oo.EmployeeID,oo.OrderDate,oo.ShipName FROM Orders oo where oo.OrderDate>=CONVERT(VARCHAR(10),@StartDate,120) 11 AND oo.OrderDate <=CONVERT(VARCHAR(10),@EndDate,120) AND oo.CustomerID =@CustomerID; 12 SET @rowsAffect =@@ROWCOUNT; 13 SET @err = @@ERROR; 14 RETURN @err; 15 END
现在我们调用刚刚创建的存储过程:
调用段
1 DECLARE @CustomerID NCHAR(5); 2 DECLARE @StartDate DATETIME; 3 DECLARE @EndDate DATETIME; 4 DECLARE @RowsAffect INT; 5 DECLARE @a int; 6 SET @CustomerID ='ALFKI'; 7 SET @StartDate='1900-01-01'; 8 SET @EndDate='2000-01-01'; 9 EXEC @a= GetOrderInfo @CustomerID,@StartDate,@EndDate,@RowsAffect OUTPUT;
此时可以发现,SQL 窗口下方结果集有2个窗口,一个是执行此存储过程返回的结果集,一个是存储过程的输出参数和返回值;
现在我们想把结果集放到临时表中:
第一步创建临时表:
创建临时表
1 CREATE TABLE #customers 2 (customerid nchar(5), 3 EmployeeID int, 4 OrderDate datetime, 5 ShipName nvarchar(40) 6 )
调用段:
调用段
1 DECLARE @CustomerID NCHAR(5); 2 DECLARE @StartDate DATETIME; 3 DECLARE @EndDate DATETIME; 4 DECLARE @RowsAffect INT; 5 DECLARE @a int; 6 SET @CustomerID ='ALFKI'; 7 SET @StartDate='1900-01-01'; 8 SET @EndDate='2000-01-01'; 9 INSERT INTO #customers(customerid,EmployeeID,OrderDate,ShipName) 10 EXEC @a= GetOrderInfo @CustomerID,@StartDate,@EndDate,@RowsAffect OUTPUT; 11 SELECT @rowsAffect ,@a
就这样就ok啦!
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏