Error converting data type varchar to numeric.

sql server India
  • 11 years 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

     

  • 11 years 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)

  • 11 years ago

     thanks lot

    i did it  same way 

Post a reply

Enter your message below

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon