Quantcast
Channel: CSharp Forum Latest Questions
Viewing all articles
Browse latest Browse all 32056

Function

$
0
0
I want to make sql function if any user issued any quantity that should be reduced from Balance column or if user returned any quantity that should also add into Balance Quantity.
And 
If User is entering first time qunatity into Total_qty column into ACTesting table then same quantity must show in Balance column


createtable ACTesting

(

id intidentity(1,1),

item nvarchar(max)  notnull,

Issued nvarchar(max)null,

Used nvarchar(max)null,

Returned nvarchar(max)null,

Balance as(dbo.Issued(issued)),

Total_qty nvarchar(max)

)

 

 

alterfunction dbo.Issued(@issued nvarchar)

returnsint

as

      Begin

      declare @Issued_Qty int=cast(@issued asint)

      declare @Amount23 int

      ifnotexists(select*from ACTesting)

       

                  begin

                        declare @Bal_Qty int=cast((Select Balance from

                         ACTesting where id =(selectmax(id)from 

                        ACTesting))ASint)                  

                        set @Amount23=@Bal_Qty-@issued

                        return @Amount23                               

                  End        

            else

                  Begin

                        declare @Total_Qty int=cast((Select Total_qty from

                        ACTesting where id =(selectmax(id)from

                        ACTesting))ASint)                  

                        set @Amount23=@Total_Qty

                        return @Amount23

                  End

                  return @Amount23             

      End


Viewing all articles
Browse latest Browse all 32056

Trending Articles