如何避免在sql中编写重复的代码

时间:2015-03-14 17:04:29

标签: sql-server stored-procedures

我有一个存储过程,其中有几个if else语句,其中一半需要执行相同的大代码块。

这个大代码块包括使用传递给存储过程的各种参数调用各种内联TVF。

如下所示,当我需要执行这个大代码块时,还有一些条件。什么结构可以帮助我避免这种代码重复而没有太多开销,或者可能只有if else帮助才能处理它?<​​/ p>

所有这些代码都在一个存储过程中,并且调用内联TVF使用在存储过程中计算的一些参数和值,所以我怀疑创建一个函数是否有帮助。

样本流程如下: enter image description here

示例代码如下: 注意:如果else语句可能在逻辑上有点混乱,因为这是我被困住的地方

if exists(select * from dbo.ResultsStored where Hashedkey = HashBytes('MD5', @StringConcat))
begin

if exists(SELECT  * FROM    ResultsStored RS CROSS APPLY 
( SELECT    MAX(ModifiedAt) AS ModifiedAt FROM Patients pt  WHERE    pt.RowId = RS.RowId) p
WHERE   p.ModifiedAt > rs.ModifiedAt )
begin

print 'begin exists'
if @firstname is not null
begin
 SELECT @constVal = FunctionWeight FROM   dbo.FunctionWeights WHERE  FunctionWeights.FunctionId = 1;
 INSERT INTO #Temp2 (RowNumber,ValFromUser,ColumnName,ValFromFunc,FuncWeight,percentage) 
  SELECT RowNumber,@firstname,'firstname',PercentMatch,@constVal,PercentMatch * @constVal FROM   dbo.MatchFirstName(@firstname)
END 

 if @Middlename is not null
  BEGIN
      SELECT @constVal = FunctionWeight FROM   dbo.FunctionWeights WHERE  FunctionWeights.FunctionId = 2;
  INSERT INTO #Temp2 (RowNumber,ValFromUser,ColumnName,ValFromFunc,FuncWeight,percentage)
      SELECT RowNumber, @MiddleName,'Middlename',PercentMatch, @constVal,PercentMatch * @constVal FROM   dbo.MatchMiddleName(@MiddleName)
END 


   /* and calling goes on */  --until this is the large piece of code that needs to be repeated
select * from #Temp2
print 'updating results table'
Update dbo.ResultsStored set FinalSearchSeral = @searchNumber,  ModifiedAt = getDate(), PercentMatch  =  t.Perc from (select SUM(Percentage)/@cnt as Perc from #Temp2 GROUP BY RowNumber ) t  where HashedKey = HASHBYTES('MD5', @StringConcat)
select * from dbo.ResultsStored where Hashedkey = HashBytes('MD5', @StringConcat) order by SearchSerial desc, PercentMatch desc
end

 else if exists(select * from dbo.ResultsStored join dbo.Patients on Patients.rowId = ResultsStored.rowId where ResultsStored.Hashedkey = HashBytes('MD5', @StringConcat) and Patients.ModifiedAt <= ResultsStored.ModifiedAt  )
 begin
 print 'select only'
 select * from dbo.ResultsStored where HashedKey = HASHBYTES('MD5', @StringConcat) order by SearchSerial desc, PercentMatch desc
 end
 end
 else if not exists(select * from dbo.ResultsStored where Hashedkey = HashBytes('MD5', @StringConcat))
begin
print 'insert record';
select * from #Temp2
Insert into dbo.ResultsStored( searchSerial,FinalSearchSeral, StringSearched, RowId,PercentMatch, HashedKey) 
select @searchNumber, @searchNumber, dbo.encrypt(@StringConcat), RowNumber, (SUM(Percentage)/@cnt) as Percentage , HASHBYTES('MD5', @StringConcat)
FROM #Temp2 GROUP BY RowNumber 
select * from dbo.ResultsStored 
end

1 个答案:

答案 0 :(得分:0)

如果代码相同,那么你可以编写类似的东西

IF (Time > Y OR @ColumnA = @X) BEGIN ... END

在您的示例中,您可以执行以下操作:

IF (@firstname IS NOT NULL) BEGIN SET @name = @firstname; SET @fId = 1;  END;
    IF (@Middlename IS NOT NULL) BEGIN SET @name = @Middlename; SET @fId =2;  END;
    IF (
        IF EXISTS ( SELECT
                        *
                    FROM
                        ResultsStored RS
                        CROSS APPLY (
                                      SELECT
                                        MAX(ModifiedAt) AS ModifiedAt
                                      FROM
                                        Patients pt
                                      WHERE
                                        pt.RowId = RS.RowId
                                    ) p
                    WHERE
                        p.ModifiedAt > RS.ModifiedAt )

                        SELECT
                            @constVal = FunctionWeight
                        FROM
                            dbo.FunctionWeights
                        WHERE
                            FunctionWeights.FunctionId = @fId;
                        INSERT  INTO #Temp2
                                ( RowNumber ,
                                  ValFromUser ,
                                  ColumnName ,
                                  ValFromFunc ,
                                  FuncWeight ,
                                  percentage
                                )
                                SELECT
                                    RowNumber ,
                                    @firstname ,
                                    'firstname' ,
                                    PercentMatch ,
                                    @constVal ,
                                    PercentMatch * @constVal
                                FROM
                                    dbo.MatchFirstName(@name)