我有以下存储过程:
SET QUOTED_IDENTIFIER OFF;
GO
ALTER PROCEDURE [transactiondb] @dbname1 NVARCHAR(128)
AS
DECLARE @query VARCHAR(1000);
SET @query = "SELECT TOP 10*
FROM @dbname1.dbo.transaction";
EXEC (@query);
GO
执行存储过程时,出现以下错误:
必须声明标量变量" @ dbname1"
我很确定这与传递变量的方式有关,但我真的很难理解发生了什么或如何纠正?
答案 0 :(得分:1)
试试这个。它对我有用,
Alter PROCEDURE [transactiondb] @dbname1 NVARCHAR(128)
AS
DECLARE @query VARCHAR(1000);
SET @query = 'SELECT TOP 10 *
FROM ' +@dbname1+'.dbo.transaction';
EXEC (@query);
Go