2017-05-10 32 views
-1

這工作:當我在撇號之前刪除一些空格時,爲什麼字符串連接失敗?

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 

這是爲什麼?

+4

打印出來的查詢。 。 。 'FROMjob as j'不是有效的SQL。 –

回答

2

因爲之前需要from之後的空格。

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

+0

Ohhhh當然....哈哈.... – loltospoon

相關問題