当一个参数为null时,不会打印Sql字符串 [英] Sql string not printing when one parameter is null
问题描述
这是我的代码:
Here is my code:
SET @MyParameterEntry = 'My first parameter is: ' + @P1 + ' My second parameter is: ' + @P2 + 'My third parameter is: ' + @P3
Print @MyParameterEntry
当我的P3参数是NULL,我的其他参数都没有显示 - 甚至我的我的第一个参数是:verbiage。
这还没有发生,我通常能够成功传入null或空白参数,仍然有我的其他verbiage打印。如果我将P3参数默认为特定字符串,则会显示整个@MyParameterEntry变量。
导致此问题的原因以及如何解决它以便我的整个@MyParameterEntry显示无论我的P3参数何时为空?
我尝试过:
Google搜索,MSDN,与其他有效的代码进行比较。
When my P3 parameter is NULL, none of my other parameters display - not even my "My first parameter is: " verbiage.
This has not been happening and I am usually able to successfully pass in null or blank parameters and still have my other verbiage print. If I default my P3 parameter to a specific string, the entire @MyParameterEntry variable displays.
What is causing this and how can I resolve it so my entire @MyParameterEntry displays regardless of when my P3 parameter is null?
What I have tried:
Google search, MSDN, compared to other code that works.
推荐答案
添加
Add
isnull(@P3,'')
因此传递空字符串而不是空值。对所有参数都可能更安全。
So blank string so passed rather than a null value. Might be safer to do it to all parameters.
您好,
请试试这个: -
在SQL Server中使用Concat
声明@ P1 varchar(10),@ P2 varchar(10),@ P3 VARCHAR(10),@ MyParameterEntry VARCHAR(120)
SET @ P1 ='W'
SET @ P2 ='E'
SET @ P3 = NULL
SET @MyParameterEntry = CONCAT('我的第一个参数是:',@ P1,'我的第二个参数是:',@ P2,'我的第三个参数是:',@ P3)
打印@MyParameterEntry
Hi,
Please try this:-
Use Concat in SQL Server
Declare @P1 varchar(10),@P2 varchar(10),@P3 VARCHAR(10),@MyParameterEntry VARCHAR(120)
SET @P1='W'
SET @P2='E'
SET @P3 =NULL
SET @MyParameterEntry = CONCAT( 'My first parameter is: ' , @P1 , ' My second parameter is: ' , @P2 , 'My third parameter is: ' , @P3)
Print @MyParameterEntry
这篇关于当一个参数为null时,不会打印Sql字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!