TSQL PRINT Statement Translation Request

When I try to print a line with int and nvarchar on the same line, I get an error. Here is the whole code.

 DECLARE @COUNT INT, @CONST INT SET @COUNT = 0 SET @CONST = 12 WHILE(@COUNT<12) BEGIN SET @COUNT = @COUNT + 1 PRINT @COUNT + N' times ' + @CONST + N' is ' + @COUNT*@CONST END 

The error I am getting is:

Msg 245, Level 16, State 1, Line 8 Conversion error when converting nvarchar 'times' value to int data type.

I can type 'int' a nvarchar just to find it myself, but when I combine them, I get this error. Am I combining them wrong?

I am using Microsoft SQL Management Studio

+6
source share
1 answer

You must CAST your variables as nvarchar first:

 DECLARE @COUNT INT, @CONST INT SET @COUNT = 0 SET @CONST = 12 WHILE(@COUNT<12) BEGIN SET @COUNT = @COUNT + 1 PRINT CAST(@COUNT as nvarchar) + N' times ' + CAST(@CONST as nvarchar) + N' is ' + CAST(@COUNT*@CONST as nvarchar) END 
+9
source

Source: https://habr.com/ru/post/893036/


All Articles