将参数传递给存储过程时出错

时间:2015-05-21 10:22:18

标签: sql-server-2008 stored-procedures

我有以下存储过程:

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"

我很确定这与传递变量的方式有关,但我真的很难理解发生了什么或如何纠正?

1 个答案:

答案 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