修改存储过程

时间:2017-10-30 09:25:23

标签: sql sql-server stored-procedures

我有一个存储过程,根据分类帐条目计算可用余额,如下所示。我想编辑并添加一个额外的参数;原始SP仅执行此查询:

SELECT SUM([amount value]) as [Bal available]  
FROM  [xxxxxxxxxxxx $Ledger Entries] 
WHERE [Entry No_] ='number_here'

但我希望它能实现:

SELECT SUM([amount value]) as [Bal available]  
FROM  [xxxxxxxxxxxx $Ledger Entries] 
WHERE [Transaction Type] ='TYPE1' and [Entry No_] ='number_here'` 

有什么建议吗?

ALTER PROCEDURE [dbo].[loadbal] 
    (
    @Company_Name varchar(100),
    @EntNo varchar(100)
    )
AS
BEGIN
DECLARE @SQL VARCHAR(4000)
DECLARE @TableName VARCHAR(4000)

SET @TableName='['+@Company_Name+' $Ledger Entries]'

SELECT @SQL = ' SELECT SUM([amount value]) as [Bal available]  
                FROM '+@TableName+' WHERE [Entry No_] ='+@EntNo

EXEC (@SQL)

END

1 个答案:

答案 0 :(得分:1)

只需更改您的Select声明如下:

SELECT @SQL = ' SELECT SUM([amount value]) as [Bal available]  FROM '+@TableName+' WHERE [Transaction Type] = ''TYPE1'' AND [Entry No_] ='+@EntNo

SQL Query将如下所示:

 SELECT SUM([amount value]) as [Bal available]  FROM [ $Ledger Entries] WHERE [Transaction Type] = 'TYPE1' AND [Entry No_] = <whatever u have passed here>