locked
Sqlserver 2012 Return Table as out parameter with other out parameters RRS feed

  • Question

  • Hi Friends,

    how I can return table from sqlserver stored procedure as out parameter along with another out parameter. please find below my stored procedure . or if possible how I can get multipul value from  @Erroritem  nvarchar(20) out,  @errorqty int out

    thank you

     GO
    ALTER  PROCEDURE [dbo].[Quotation_to_invoice]
    (
        @TableVar dbo.inventory_table READONLY, @mstoreid int, @mcityid int,@mregionid int, @order_type nvarchar(20),@terms nvarchar(20),@invoice_sequence nvarchar(20) out,@quotation_no nvarchar(20)
    ,  @Erroritem  nvarchar(20) out,  @errorqty int out 
    )
    AS
    BEGIN
     
    DECLARE	@SNO	int	;
    DECLARE @FR	nvarchar(40);
    DECLARE @PartNO	nvarchar(60);
    DECLARE @itemName	nvarchar(100);
    DECLARE @Brand	nvarchar(60);
    DECLARE @Origion	nvarchar(60);
    DECLARE @Location	nvarchar(60);
    DECLARE @Qty	nvarchar(10);
    DECLARE @Price	nvarchar(10);
    DECLARE @Discount	nvarchar(10);
    DECLARE @Total	nvarchar(20);
    Declare @mseq int;
    Declare @tbalance int;
    Declare @mainbalance int;
    declare @tbalance_counter int;
     DECLARE @errortab table (errorpartno nvarchar(20), qty int)
        SET NOCOUNT ON;
    	    
    DECLARE mycursor CURSOR FAST_FORWARD
    FOR SELECT  sno,fr,partno,itemname,brand,origion,location,qty,price,discount,total  FROM @TableVar 
            
     select @invoice_sequence= NEXT VALUE  FOR invoice_Sequence
      SET @tbalance_counter=0
    OPEN mycursor FETCH NEXT FROM myCursor INTO @SNO,  @FR,  @PartNO,  @itemName,  @Brand,  @Origion,	  @Location,  @Qty	,  @Price,  @Discount	 ,  @Total	 ;
    WHILE (@@FETCH_STATUS = 0 )
        BEGIN     
    		   
    	if	 @order_type='Quotation'  and @terms ='Cash' 
    	begin
    		select  @mainbalance=sum(QTY-USED_balance) from   inv_balance 
          	                    where part_number=@partno and STOREID=@mstoreid and CITYID=@mcityid
    		               	                     and REGIONID=@mregionid  and  (qty-USED_balance) >0 
    			   IF @qty > @mainbalance 
    			 Begin
    			 set @Erroritem =@partno
    			 set @errorqty=@mainbalance 
    			 insert into @errortab values(@partno,@mainbalance )
    			 end
    			 ELSE
    			 begin
    			                                  
    			 IF (@mainbalance >= @qty and @mainbalance is not null)
    			 Begin
    			 --insert into quotation2(qty,itemname) values(@mainbalance,@itemname)
    	WHILE (@tbalance_counter < =@QTY and @mainbalance >= @qty)
    	begin
    	  insert into quotation2(qty,itemname) values(@mainbalance,@itemname)
    	select    @mseq = min(seqno)   from   inv_balance 
                                             where  part_number=@PartNO  and STOREID=@mstoreid
                                                 and CITYID=@mcityid and REGIONID=@mregionid  and  (qty-USED_balance) >0 
    											
    	 select   @tbalance =(qty-USED_balance)    from    inv_balance 
                                             where part_number=@PartNO and STOREID=@mstoreid
                                                 and CITYID=@mcityid and REGIONID=@mregionid and  seqno=@mseq
    											  
    		if ( @tbalance>=@Qty OR @tbalance<=@Qty)
    		   Begin        
    		  -- insert into quotation2(itemname) values('100')
    				   					                   
    		update inv_balance  set USED_BALANCE = USED_BALANCE + @QTY
    			   where  part_number=@PartNO  and STOREID=@mstoreid
                          and CITYID=@mcityid  and REGIONID=@mregionid  and seqno=@mseq
    					   
    					   set @tbalance_counter = @tbalance_counter + @tbalance
    			end
    		   
    		   
    		   	   end
    			 	end
    								               
    			End
    		---- mainbalance								
     insert into inv_trx(TRX_TYPE,TRX_DATE,PART_NUMBER,ItemName,brand, origion,location,
    		 STOREID,DISCOUNT_AMOUNT, CITYID,REGIONID,QTY,SELLING_PRICE,COST_PRICE,REF_NO,BATCHNO,invoice_no,FR,Quotation_no)
                 	VALUES('S',getdate(),@partno,@itemName,@Brand,@origion,@location,
    				 @mstoreid, @Discount,  @mcityid ,@mregionid, 
    							 @qty, @price,@price, NULL,null ,@invoice_sequence,@FR,@quotation_no) 
    						  end   ---@mainbalance
    			
    				
    					FETCH NEXT FROM myCursor INTO 
    	 	@SNO,  @FR,  @PartNO,  @itemName,  @Brand,  @Origion,	  @Location,  @Qty	,  @Price,  @Discount	 ,  @Total	 ;
    					
     end
    		
    		----main while loop					 
    CLOSE myCursor ;
    DEALLOCATE myCursor;
    	--update quotation set Acknowledge_flag='Y' where Quotation_no=@quotation_no
    	 
    	 		 
    	 end
        


    asad

    Saturday, January 25, 2014 6:34 AM

Answers

  • how I can return table from sqlserver stored procedure as out parameter along with another out parameter. please find below my stored procedure . or if possible how I can get multipul value from  @Erroritem  nvarchar(20) out,  @errorqty int out

    TVPs are read-only so it is not possible to return the table as an output parameter.  Scalar parameters (e.g. @Erroritem and @errorquantity) can only store a single value so that is not an option either.

    If you cannot return a result set, another option is an XML output parameter.  Below is a code example that shows one method to return the contents of the table varable as XML.

    ALTER PROCEDURE dbo.Quotation_to_invoice
          @TableVar dbo.inventory_table READONLY
    	, @mstoreid int
    	, @mcityid int
    	, @mregionid int
    	, @order_type nvarchar(20)
    	, @terms nvarchar(20)
    	, @invoice_sequence nvarchar(20) out
    	, @quotation_no nvarchar(20)
    	, @errorxml xml OUT
    AS
    DECLARE @errortab table (errorpartno nvarchar(20), qty int);
    DECLARE 
    	  @Erroritem  nvarchar(20)
    	,  @errorqty int;
    
    --stored procedure code here
    
    SET @errorxml = (
    	SELECT
    		  errorpartno
    		, qty
    	FROM @errortab AS errortab
    	FOR XML PATH, ROOT('errors'), TYPE
    	);
    
    GO


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com


    • Edited by Dan GuzmanMVP Saturday, January 25, 2014 2:02 PM spelling
    • Proposed as answer by Elvis Long Tuesday, January 28, 2014 6:28 AM
    • Marked as answer by Elvis Long Monday, February 3, 2014 1:33 AM
    Saturday, January 25, 2014 2:01 PM

All replies

  • Please see the example for OUTPUT parameter.

    OUTPUT SSMS

    Without your DDL,DML and Sample data, we would not be able to help with actual code. But, you can refer the link which would definitely help you.

    Saturday, January 25, 2014 7:06 AM
  • why not return them as a resultset? Whats your front end language? how do you want to use the resultset values?


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Saturday, January 25, 2014 7:13 AM
  • I am using vb.net 2008, furthermore , returned records I want to use as not sufficient qty all  those records will be displayed in Datagridview as red records.

       As I used  "select * from table"   but could not succeeded , because there are other output parameters are available.


    asad

    Saturday, January 25, 2014 11:51 AM
  • Sorry I didnt understand your explanation. 

    What does the below mean?

    I want to use as not sufficient qty all  those records will be displayed


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Saturday, January 25, 2014 11:53 AM
  • how I can return table from sqlserver stored procedure as out parameter along with another out parameter. please find below my stored procedure . or if possible how I can get multipul value from  @Erroritem  nvarchar(20) out,  @errorqty int out

    TVPs are read-only so it is not possible to return the table as an output parameter.  Scalar parameters (e.g. @Erroritem and @errorquantity) can only store a single value so that is not an option either.

    If you cannot return a result set, another option is an XML output parameter.  Below is a code example that shows one method to return the contents of the table varable as XML.

    ALTER PROCEDURE dbo.Quotation_to_invoice
          @TableVar dbo.inventory_table READONLY
    	, @mstoreid int
    	, @mcityid int
    	, @mregionid int
    	, @order_type nvarchar(20)
    	, @terms nvarchar(20)
    	, @invoice_sequence nvarchar(20) out
    	, @quotation_no nvarchar(20)
    	, @errorxml xml OUT
    AS
    DECLARE @errortab table (errorpartno nvarchar(20), qty int);
    DECLARE 
    	  @Erroritem  nvarchar(20)
    	,  @errorqty int;
    
    --stored procedure code here
    
    SET @errorxml = (
    	SELECT
    		  errorpartno
    		, qty
    	FROM @errortab AS errortab
    	FOR XML PATH, ROOT('errors'), TYPE
    	);
    
    GO


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com


    • Edited by Dan GuzmanMVP Saturday, January 25, 2014 2:02 PM spelling
    • Proposed as answer by Elvis Long Tuesday, January 28, 2014 6:28 AM
    • Marked as answer by Elvis Long Monday, February 3, 2014 1:33 AM
    Saturday, January 25, 2014 2:01 PM