'sp_executesql'附近的语法不正确

OMG*_*ies 8 sql t-sql sql-server-2005 dynamic-sql

我不明白为什么以下是给我错误的.我认为它与注释掉的部分有关,但@SQL是nvarchar(4000).

BEGIN
  sp_executesql N'SELECT ''td'''
  --sp_executesql @SQL, N'@StartDate DateTime, @EndDate DateTime, @End2 DateTime, @Program varchar(4)', @StartDate, @EndDate, @End2, @Program
END
Run Code Online (Sandbox Code Playgroud)

Sam*_*ron 15

这就是为什么:

-- This works just fine:
BEGIN
  -- You must have an exec before your sp_executesql or it will not work in a block
  exec sp_executesql N'SELECT ''td'''
END

当你在一个区块中时,你不能只在没有exec的情况下调用存储过程.