Impresión de variable entera y cadena en la misma línea en SQL


Ok así que he buscado una respuesta a esto en Technet, sin éxito.

Solo quiero imprimir una variable entera concatenada con dos variables de cadena.

Este es mi código, que no se ejecuta:

print 'There are ' + @Number + ' alias combinations did not match a record'

Parece una característica tan básica, no podía imaginar que no es posible en T-SQL. Pero si no es posible, por favor solo dilo. No puedo encontrar una respuesta directa.

Author: Joel Coehoorn, 2014-05-09

5 answers

declare @x INT = 1 

PRINT 'There are ' + CAST(@x AS VARCHAR) + ' alias combinations did not match a record'
 71
Author: mohan111,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-09 13:27:58

Los números tienen mayor precedencia que las cadenas, por lo que, por supuesto, los operadores + quieren convertir sus cadenas en números antes de agregar.

Usted podría hacer:

print 'There are ' + CONVERT(varchar(10),@Number) +
      ' alias combinations did not match a record'

O utilizar las (bastante limitadas) facilidades de formato de RAISERROR:

RAISERROR('There are %i alias combinations did not match a record',10,1,@Number)
WITH NOWAIT
 4
Author: Damien_The_Unbeliever,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-09 13:27:57

No se puede combinar una cadena de caracteres y una cadena numérica. Necesita convertir el número a una cadena usando CONVERT o CAST.

Por ejemplo:

print 'There are ' + cast(@Number as varchar) + ' alias combinations did not match a record'

O

print 'There are ' + convert(varchar,@Number) + ' alias combinations did not match a record'
 2
Author: BigBlue,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-11 11:12:25

Puedes probar este,

declare @Number INT = 5                            
print 'There are ' + CONVERT(VARCHAR, @Number) + ' alias combinations did not match a record'
 0
Author: BAdmin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-09 13:28:09

Compruebe si ha establecido y el valor inicial para que se impriman los valores int y decimales.

Este ejemplo está imprimiendo una línea vacía

declare @Number INT
print 'The number is : ' + CONVERT(VARCHAR, @Number)

Y esta muestra está imprimiendo - > El número es: 1

declare @Number INT = 1
print 'The number is : ' + CONVERT(VARCHAR, @Number)
 0
Author: Selim Özbudak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-02 15:56:39