Conversion failed when converting the nvarchar value to int
sql-server, stored-procedures
Solution
This should work:
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
DECLARE @count int
SET @SQLString = N'SELECT @CountOUT = COUNT(*) FROM ' + @tablename;
SET @ParmDefinition = N'@CountOUT int OUTPUT';
EXECUTE sp_executesql @SQLString, @ParmDefinition, @CountOUT=@count OUTPUT;
SELECT @count;
Problem
Declare @count nvarchar(max) ``` set @count ='select COUNT(*) from '+ @tablename+'' if( @count =0 ) begin print 'fail' end else begin print 'success' end end ``` the @count variable is not getting the value 0. it shows the error as Conversion failed when converting the nvarchar value 'select COUNT(*) from tablename' to data type int.