Community discussion forum

Error converting data type varchar to numeric.

  • 6 months ago

    hi

    i m writting following code

    declare @i as numeric(18,0)
    declare @sql as varchar(8000)

    set @i =4

    set @sql ='select top '+@i+' * from dbo.tbl_ls_feed_data'
    exec(@sql)


    i mgetting error 

     to Error converting data type varchar to numeric.

    how to cast @i in this case

     

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 6 months ago

    Because @sql is varchar, @i must also be varchar when constructing your dynamic sql string.

    The following should resolve your problem



    declare @i as numeric(18,0)
    declare @sql as varchar(8000)

    set @i =4

    set @sql ='select top '+ CAST(@i AS VARCHAR) + ' * from dbo.tbl_ls_feed_data'
    exec(@sql)

  • 6 months ago

     thanks lot

    i did it  same way 

Post a reply

Enter your message below

Sign in or Join us (it's free).