我收到以下错误:
"Incorrect Syntax near '.'. Expected ID or Quoted_ID".
尝试创建以下过程时,

CREATE PROCEDURE [dbo].[SP_NAME]
@DBName varchar(max)
As
Begin

SELECT NBCGL_TRANSACTION.*, MRG_BSLA.GL_BSLA_CODE AS BSLA
FROM @DBName..NBCGL_TRANSACTION LEFT OUTER JOIN MRG_BSLA
     ON NBCGL_TRANSACTION.BUSINESS = MRG_BSLA.BSLA_CODE
WHERE NBCGL_TRANSACTION.TRANSACTION_TYPE = 2

END

最佳答案

您不能直接将变量用于数据库/架构/表/列名称。您需要使用动态SQL来实现您的目标:

CREATE PROCEDURE [dbo].[SP_NAME]
@DBName varchar(max)
As
Begin

declare @query nvarchar(max)
set @query = N'SELECT NBCGL_TRANSACTION.*, MRG_BSLA.GL_BSLA_CODE AS BSLA FROM '
+ @DBName
+ N'..NBCGL_TRANSACTION LEFT OUTER JOIN MRG_BSLA ON NBCGL_TRANSACTION.BUSINESS = MRG_BSLA.BSLA_CODE WHERE NBCGL_TRANSACTION.TRANSACTION_TYPE = 2'

exec sp_executesql @query

END

关于sql - SQL中的预期ID或Quoted_ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25540056/

10-11 17:32