当我在撇号之前删除一些空格时,为什么字符串连接失败?

时间:2017-05-10 17:52:27

标签: sql-server tsql linqpad

这有效:

DECLARE @query as nvarchar(200);
SET @query = 'SELECT j.job FROM ' + '' + 'job as j';

EXEC sp_executesql @query

...但这不起作用(输出错误为Incorrect syntax near the keyword 'as', pointing to the first line):

DECLARE @query as nvarchar(200);
SET @query = 'SELECT j.job FROM' + '' + 'job as j'; -- removed the white space after the word FROM

EXEC sp_executesql @query

为什么会这样?

1 个答案:

答案 0 :(得分:2)

因为在from之前job之后需要一个空格。

select 'SELECT j.job FROM ' + '' + 'job as j';

返回:SELECT j.job FROM job as j

select 'SELECT j.job FROM' + '' + 'job as j';

返回:SELECT j.job FROMjob as j