Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:15:07+00:00 2026-06-04T05:15:07+00:00

I am using one stored procedure in which I send @columnname whose type in

  • 0

I am using one stored procedure in which I send @columnname whose type in database is float and I also declare parameter float.
When I pass column name as float then it gives the error:

Msg 8114, error converting data type nvarchar to float.

This is my test query

Declare @column float
set @column = 'S_E1'
select Avg(@column) from TBL_SENSORS

And when I change parameter type to varchar then it gives me this error:

Msg 8117, Operand data type varchar is invalid for avg operator.

Declare @column varchar
set @column = 'S_E1'
select Avg(@column) from TBL_SENSORS

How can I solve this?

UPDATE :

This is my stored procedure :

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:15:08+00:00Added an answer on June 4, 2026 at 5:15 am

    You need dynamic sql.

    DECLARE @SQL NVARCHAR(4000)
    Declare @column varchar 
    set @column = 'S_E1' 
    
    SET @SQL = 'select Avg(' + quotename(@column) + ') from TBL_SENSORS'
    
    EXEC sp_executesql @SQL
    

    Update: applied both suggestions for quotename and sp_executesql

    New procedure: you can use dynamic SQL to place data into a temp table with a known schema. Refer to column based on alias: ColumnToAvg. Instead of TBL_SENSORS use #TempSensors.

    Note: in production you’ll want to check if #TempSensors exists and drop if it does.

    SET @SQL = 'Select RECORD_TIMESTAMP, '
        + quotename(@ColumnName)   
        + ' as ColumnToAvg 
           INTO #TempSensors
           from TBL_SENSORS
           Where RECORD_TIMESTAMP Between @StartDate and @EndDate 
           And @ColumnName Between @Start And @End'
    
    EXECUTE sp_executesql @sqlCommand
        , N'@StartDate datetime, @EndDate datetime, @Start int, @End int'
        , @StartDate , @EndDate, @Start, @End
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Stored Procedure (SP) in which I pass in one value. In
I'm calling a stored procedure which does some updates/inserts/deletes (any one of these at
I'm inserting a row using an Oracle stored procedure which is configured to use
I am using oracle 11g and have written a stored procedure which stores values
I'm overloading a vb.net search procedure which queries a SQL database. One of the
I do a call to an oracle stored procedure using JDBCTemplate which has an
Using a stored procedure i have a fairly complex SQL statement which returns a
I have a stored procedure, PROC , which receives some parameters. If one of
I have inherited a 1000-line stored procedure which produces XML by using FOR XML
I have created one stored procedure which runs on 5000 users in tbluser table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.