这有效:
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
为什么会这样?
答案 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