我正在使用一个存储过程,我在其中发送@columnname
它的数据库类型,float
并且我还声明了 parameter float
。
当我将列名作为浮点数传递时,它会给出错误:
消息 8114,将数据类型 nvarchar 转换为浮点数时出错。
这是我的测试查询
Declare @column float
set @column = 'S_E1'
select Avg(@column) from TBL_SENSORS
当我将参数类型更改为varchar
then 它给了我这个错误:
消息 8117,操作数数据类型 varchar 对 avg 运算符无效。
Declare @column varchar
set @column = 'S_E1'
select Avg(@column) from TBL_SENSORS
我该如何解决这个问题?
更新 :
这是我的存储过程:
ALTER PROCEDURE [dbo].[getAvgColumn]
@ColumnName float,
@StartDate DateTime,
@EndDate DateTime,
@Start int,
@End int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @Skip int = 0
declare @Take int = 4
declare @count int = 0
set @count = (select count(@ColumnName) from TBL_SENSORS Where RECORD_TIMESTAMP Between @StartDate and @EndDate And ( @ColumnName Between @Start And @End ))
while(@Skip < @count)
Begin
select avg(@ColumnName)
from
(select
@ColumnName as cc,
row_number() over (order by RECORD_TIMESTAMP) as rn
from
TBL_SENSORS
Where
RECORD_TIMESTAMP Between @StartDate and @EndDate
And (@ColumnName Between @Start And @End)
) T
where
rn > @Skip and
rn <= @Skip + @Take
set @Skip = @Skip + @Take
end
END