Conversion failed when converting the nvarchar value to INT

  • Not sure, why the below code is throwing error. Any suggestion please?

    Declare @specificDB nvarchar(max) = 'AdventureWorksDW2012'

    ,@tablename nvarchar(max) = 'DimAccount'

    declare @localtab INT

    SET @localtab = (SELECT Convert(INT,('select count(*) from ' + @specificDB+'.'+'INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = ''BASE TABLE'' AND Table_name = ' + @tablename)))

    Print @localtab

    Print @localtab

    ----

    Msg 245, Level 16, State 1, Line 8

    Conversion failed when converting the nvarchar value 'select count(*) from AdventureWorksDW2012.INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = 'BASE TABLE' AND Table_name = DimAccount' to data type int.

    Thanks.

  • You're assigning a string to an integer variable.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Yes, I got the issue and fixed it. 🙂

    Thanks.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply