这次,在实际项目中,着实考虑了如何提升应用程序性能来使用存储过程的优势,因为程序对通信速度的要求,不得不写大量的存储过程来实现。下面结合一个例子,相信大家会更加清晰。
CREATE Procedure GetHighByTeam @tableName nvarchar(20),@startDate nvarchar(20),@endDate nvarchar(20),@teamNo nvarchar(1)
AS//功能:通过班次得到温度最高值
declare @sSql nvarchar(4000),@i int
set @sSql = 'select '
set @i = 1
while @i < 16
begin
set @sSql = @sSql + ltrim(convert(varchar(3), @i))+ ' AS Seq,tempDate,tempTime,p'+ltrim(convert(varchar(3), @i))+' AS Point From ' + @tableName
set @sSql = @sSql + ' Where p'+ltrim(convert(varchar(3),@i)) + ' = ( Select Max(p'+ltrim(convert(varchar(3),@i))+') From '+@tableName +' Where teamNo = ' + @teamNo + ' and (TempDate between ' + @startDate + ' And '+@endDate+'))'
set @sSql = @sSql + ' and teamNo =' + @teamNo
set @sSql = @sSql + ' Union Select '
set @i = @i+1
end
set @sSql = @sSql + ltrim(convert(varchar(3), @i)) + ' AS Seq ,tempDate,tempTime,p'+ltrim(convert(varchar(3), @i)) + ' AS Point From ' + @tableName
set @sSql = @sSql + ' Where p'+ltrim(convert(varchar(3), @i)) + '= (Select Max(p'+ltrim(convert(varchar(3), @i))+') From '+@tableName +' Where teamNo = ' + @teamNo + ' and (TempDate between ' + @startDate + ' And '+@endDate+'))'
set @sSql = @sSql +' and teamNo =' + @teamNo
EXECUTE sp_executesql @sSql
GO
AS//功能:通过班次得到温度最高值
declare @sSql nvarchar(4000),@i int
set @sSql = 'select '
set @i = 1
while @i < 16
begin
set @sSql = @sSql + ltrim(convert(varchar(3), @i))+ ' AS Seq,tempDate,tempTime,p'+ltrim(convert(varchar(3), @i))+' AS Point From ' + @tableName
set @sSql = @sSql + ' Where p'+ltrim(convert(varchar(3),@i)) + ' = ( Select Max(p'+ltrim(convert(varchar(3),@i))+') From '+@tableName +' Where teamNo = ' + @teamNo + ' and (TempDate between ' + @startDate + ' And '+@endDate+'))'
set @sSql = @sSql + ' and teamNo =' + @teamNo
set @sSql = @sSql + ' Union Select '
set @i = @i+1
end
set @sSql = @sSql + ltrim(convert(varchar(3), @i)) + ' AS Seq ,tempDate,tempTime,p'+ltrim(convert(varchar(3), @i)) + ' AS Point From ' + @tableName
set @sSql = @sSql + ' Where p'+ltrim(convert(varchar(3), @i)) + '= (Select Max(p'+ltrim(convert(varchar(3), @i))+') From '+@tableName +' Where teamNo = ' + @teamNo + ' and (TempDate between ' + @startDate + ' And '+@endDate+'))'
set @sSql = @sSql +' and teamNo =' + @teamNo
EXECUTE sp_executesql @sSql
GO
这个存储过程的实现大家可以不去考虑,只需要注意标红的地方,使用系统的存储过程sp_executesql 储过程的实现大家可以传递表名称,从而实现所需要的传递表名作为参数的需要。
具体在DELPHI中如何使用存储过程,请参看我在 DELPHI 部分写的《DELPHI存储过程调用》。
本文转自 august 51CTO博客,原文链接:http://blog.51cto.com/august/6904,如需转载请自行联系原作者