在 SQL 中的同一行上打印整数变量和字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23565790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing integer variable and string on same line in SQL
提问by mohan111
Ok so I have searched for an answer to this on Technet, to no avail.
好的,所以我在 Technet 上搜索了此问题的答案,但无济于事。
I just want to print an integer variable concatenated with two String variables.
我只想打印一个与两个 String 变量连接的整数变量。
This is my code, that doesn't run:
这是我的代码,没有运行:
print 'There are ' + @Number + ' alias combinations did not match a record'
It seems like such a basic feature, I couldn't imagine that it is not possible in T-SQL. But if it isn't possible, please just say so. I can't seem to find a straight answer.
看起来像这样一个基本功能,我无法想象它在 T-SQL 中是不可能的。但如果不可能,请直说。我似乎找不到直接的答案。
回答by mohan111
declare @x INT = 1
PRINT 'There are ' + CAST(@x AS VARCHAR) + ' alias combinations did not match a record'
回答by Damien_The_Unbeliever
Numbers have higher precedencethan strings so of course the +
operators want to convert your strings into numbers before adding.
数字比字符串具有更高的优先级,因此+
运算符当然希望在添加之前将您的字符串转换为数字。
You could do:
你可以这样做:
print 'There are ' + CONVERT(varchar(10),@Number) +
' alias combinations did not match a record'
or use the (rather limited) formatting facilities of RAISERROR
:
或使用(相当有限的)格式化工具RAISERROR
:
RAISERROR('There are %i alias combinations did not match a record',10,1,@Number)
WITH NOWAIT
回答by BigBlue
You can't combine a character string and numeric string. You need to convert the number to a string using either CONVERT or CAST.
不能组合字符串和数字字符串。您需要使用 CONVERT 或 CAST 将数字转换为字符串。
For example:
例如:
print 'There are ' + cast(@Number as varchar) + ' alias combinations did not match a record'
or
或者
print 'There are ' + convert(varchar,@Number) + ' alias combinations did not match a record'
回答by Selim ?zbudak
Double check if you have set and initial valuefor int and decimal values to be printed.
仔细检查您是否为要打印的 int 和十进制值设置了初始值。
This sample is printing an empty line
此示例正在打印一个空行
declare @Number INT
print 'The number is : ' + CONVERT(VARCHAR, @Number)
And this sample is printing -> The number is : 1
这个样本正在打印 -> 数字是:1
declare @Number INT = 1
print 'The number is : ' + CONVERT(VARCHAR, @Number)
回答by BAdmin
You may try this one,
你可以试试这个
declare @Number INT = 5
print 'There are ' + CONVERT(VARCHAR, @Number) + ' alias combinations did not match a record'