简体   繁体   English

SQL Server 2008 R2数据库名称不能用字符串替换吗?

[英]SQL Server 2008 R2 database name cannot be replaced by a string?

I am trying to run a SQL Server 2008 R2 query in Management Studio on Win7. 我正在尝试在Win7上的Management Studio中运行SQL Server 2008 R2查询。

I need to access 50+ databases with similar names. 我需要访问50多个具有相似名称的数据库。

But, I got error for a database name : 但是,数据库名称出现错误:

DECLARE @name1 VARCHAR(16)
SET @name1 = 'HisName_' 
DECLARE @whole_name VARCHAR(18)

DECLARE @a_person VARCHAR(2)
DECLARE @a_curosr CURSOR 
SET @a_curosr = CURSOR FAST_FORWARD 
FOR
   SELECT personName FROM @person_names -- @person_names  is a table that hold person names

OPEN @a_curosr
FETCH NEXT FROM @a_curosr INTO @a_person    

WHILE @@Fetch_Status=0
BEGIN
    SET @whole_name = @name1 + @a_person 

    INSERT INTO [dbo].[mytable]

       SELECT a.person_id
       FROM [@full_name].[dbo].[myOldTable] as a  -- error here, @full_name is Invalid object name

    FETCH NEXT FROM @a_curosr INTO @a_person
END

I have created the table with name as [@full_name].[dbo].[myOldTable] hre , @full_name is a string , eg HisName_ + a two-letter string. 我创建了名为[@full_name].[dbo].[myOldTable] hre@full_name是字符串,例如HisName_ +两个字母的字符串。

Why the database name cannot be replaced as a string so that I can access 50+ databases in a loop ? 为什么不能将数据库名称替换为字符串,以便我可以循环访问50多个数据库?

Thanks 谢谢

I've tried to write your script with a dynamic sql statement. 我试图用动态sql语句编写脚本。 Please pay attention on the commented section (the sp_executeSQL part). 请注意注释部分(sp_executeSQL部分)。 Here is: 这是:

DECLARE @name1 VARCHAR(16)
SET @name1 = 'HisName_' 
DECLARE @whole_name VARCHAR(18)

DECLARE @person_names TABLE (personName varchar(2))

INSERT INTO @person_names (personName) VALUES ('AA')
INSERT INTO @person_names (personName) VALUES ('MD')
INSERT INTO @person_names (personName) VALUES ('AS')

DECLARE @sqlString AS nvarchar(MAX);

DECLARE @a_person VARCHAR(2)
DECLARE @a_curosr CURSOR 
SET @a_curosr = CURSOR FAST_FORWARD 
FOR
   SELECT personName FROM @person_names -- @person_names  is a table that hold person names

OPEN @a_curosr
FETCH NEXT FROM @a_curosr INTO @a_person    

WHILE @@Fetch_Status=0
BEGIN
    SET @whole_name = @name1 + @a_person 

    SET @sqlString = 'INSERT INTO [dbo].[mytable]
                        SELECT a.person_id
                        FROM [' + @whole_name + '].[dbo].[myOldTable] as a'

    SELECT @sqlString -- execute the statement on the next line instead
    --EXEC sp_executeSql @sqlString;

    FETCH NEXT FROM @a_curosr INTO @a_person
END

Hope this helps. 希望这可以帮助。

The problem is that SQL Server will not expand variables into named identifiers. 问题在于,SQL Server不会将变量扩展为命名标识符。 So [@full_name] is seeking a database by the name of "@full_name", which doesn't exist. 因此,[@ full_name]正在使用名称“ @full_name”查找数据库,该名称不存在。

There are two approaches here. 这里有两种方法。

One, use an SP to iterate all DBs in the instance: sp_msforeachdb 一种,使用SP迭代实例中的所有数据库: sp_msforeachdb

Two, use Dynamic SQL and modify it in your loop for execution: See answer here 二,使用动态SQL并在循环中对其进行修改以执行: 请参见此处的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM