Error converting data type varchar to numeric.

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

     

  • 12 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)

  • 12 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.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”