問題描述
當你添加一個新的 job_type
到 sys.sp_cdc_add_job @job_type
,
(屬于 nvarchar(20)
類型)
When you add pass a new job_type
to sys.sp_cdc_add_job @job_type
,
(which is of type nvarchar(20)
)
您可以將參數傳遞為
N'
清理'- 清理
使用前一種使用 N'
的語法將參數傳遞給存儲過程有什么原因或好處嗎?
Are there any reasons or benefits to use the former syntax using N'
to pass the argument to stored procedures?
推薦答案
僅當字符串包含 unicode 字符時
Only if the string contains unicode characters
字符串在傳遞到存儲過程時隱式轉換為 nvarchar.
The string is implictly converted to nvarchar when the passed into the stored proc.
但是,在 SP 執行之前,它是一個沒有 N 前綴的文字 varchar(字符串常量").因此,如果您使用日語名稱,則需要N"以使其成為unicode 常量".請參閱 BOL 中的constants",其中詳細介紹了常量...
However, before SP execution, it's a literal varchar ("character string constant") without N prefix. So, if you Japanese names, you'll need "N" to make it a "unicode constant". See "constants" in BOL which explains more about, er, constants...
受 Andomar 啟發的測試...
A test of this inspired by Andomar...
CREATE PROCEDURE dbo.ZanziBar
@KungFoo nvarchar(1)
AS
SET NOCOUNT ON
SELECT @KungFoo
GO
EXEC dbo.ZanziBar '?'
EXEC dbo.ZanziBar N'?'
這篇關于TSQL:在字符串中顯式指定 NVARCHAR 有什么好處?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!