Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 calling function in an SP query?

Author  Topic 

adbasanta
Posting Yak Master

120 Posts

Posted - 2012-07-04 : 02:45:52
God day!

I just want to ask on how can I call the function Ive made to get the balance of a certain account..Here is the function Ive created:


USE [Pizza_Financial]
GO
/****** Object: UserDefinedFunction [dbo].[get_accntbalance] Script Date: 07/04/2012 11:24:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[get_accntbalance](@accntid as bigint)
RETURNS Money
WITH EXECUTE AS CALLER

AS
BEGIN
declare @debitbalance as money
declare @creditbalance as money
declare @remainingbalance as money

set @debitbalance=(select sum(debit_amt)
from tbl_account_transactions_detail
where debit_accnt_ID=@accntid)

set @creditbalance=(select sum(credit_amt)
from tbl_account_transactions_detail
where credit_accnt_ID=@accntid)

set @remainingbalance=@debitbalance-@creditbalance
RETURN(@remainingbalance);
END;

Here is my stored procedures to get all accounts, I am thinking to create a variable in the select statement like currentbalance and call the function but I cant make it running.

USE [Pizza_Financial]
GO
/****** Object: StoredProcedure [dbo].[Show_All_Accnts_byType] Script Date: 07/04/2012 11:23:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Show_All_Accnts_byType]
@accntcategoryID as int,
@accntypeid as bigint,
@isaccnt_current as bit

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

if(@accntypeid<>'') and (@accntcategoryID<>'')
begin
SELECT * FROM tbl_accountlist al

INNER JOIN tbl_accnt_type at
ON at.accnttypename=(select at.accnttypename where at.accnttypeid=al.AccountType)

WHERE (al.Accntstatus=1 and al.AccountCategoryID=@accntcategoryID and al.AccountType= @accntypeid and al.IsCurrent=@isaccnt_current and al.IsSubAccnt=0)
ORDER BY AccntNo ASC
end
END

Thank you for helping!

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-07-04 : 03:37:27
use outer apply


SELECT *
, Credit.Credit,Debit.Debit

FROM tbl_accountlist al

INNER JOIN tbl_accnt_type at
ON at.accnttypename=(select at.accnttypename where at.accnttypeid=al.AccountType)

outer apply
(
select sum(credit_amt) as Credit
from tbl_account_transactions_detail
where credit_accnt_ID= XXXXX --put idAccount from tbl_accountlist ; al.idAccount
)CREDIT

outer apply
(
select sum(debit_amt) as debit
from tbl_account_transactions_detail
where debit_accnt_ID= XXXXX --put idAccount ;al.idAccount
)DEBIT

WHERE (al.Accntstatus=1 and al.AccountCategoryID=@accntcategoryID and al.AccountType= @accntypeid and al.IsCurrent=@isaccnt_current and al.IsSubAccnt=0)
Go to Top of Page

adbasanta
Posting Yak Master

120 Posts

Posted - 2012-07-04 : 04:49:11
Thank you stepson. Ive made some modification on the query..Can I add currentbalance in a result? The formula is currentbalance=Debit-Credit.

Here is the modified code so far.


USE Pizza_Financial
GO
SELECT *

FROM tbl_accountlist al

INNER JOIN tbl_accnt_type at
ON at.accnttypename=(select at.accnttypename where at.accnttypeid=al.AccountType)

outer apply
(
select sum(credit_amt) as Credit
from tbl_account_transactions_detail
where credit_accnt_ID=al.AccountID
)CREDIT

outer apply
(
select sum(debit_amt) as Debit
from tbl_account_transactions_detail
where debit_accnt_ID=al.AccountID
)DEBIT


thank you!
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-07-04 : 04:54:43
yes you can add

select *,
coalesce(DEBIT.Debit,0)- coalesce(CREDIT.Credit,0) as currentbalance

from .... rest of select

S
Go to Top of Page

adbasanta
Posting Yak Master

120 Posts

Posted - 2012-07-04 : 05:20:31
quote:
Originally posted by stepson

yes you can add

select *,
coalesce(DEBIT.Debit,0)- coalesce(CREDIT.Credit,0) as currentbalance

from .... rest of select

S




Thank you stepson.This works..God Bless!
Go to Top of Page

stepson
Aged Yak Warrior

545 Posts

Posted - 2012-07-04 : 05:50:51
w welcome

S
Go to Top of Page
   

- Advertisement -