2013-10-30 89 views
0

我在解決這個問題時遇到了問題,我已經完成了從sys.servers中選擇*來檢查我的服務器是否已被列出。在sys.servers中找不到服務器'dbo'。

我也從sys.servers做了查詢選擇名稱,並列出了我的服務器名稱。

這裏是我的代碼

declare @Source_Database_Name AS varchar(255) = 'dbo.Production2' 
declare @Destination_Database_Name AS varchar(255) = 'c365online_script1' 

declare @Company_Id AS int = 1 --declare a companyid 

CREATE TABLE #CompanyID1 (ID bigint) 

INSERT INTO #CompanyID1(ID) 
VALUES('1') 

--FIRST CURSOR LOOP THROUGH THIS TABLE 
CREATE TABLE #TableList (
processorder int, 
tablename NVARCHAR(100) 
) 

INSERT INTO #TableList (processorder, tablename) 
VALUES 
(1, 'tCompany'); 

DECLARE @firstLoop BIT 
--SET @firstLoop = true 
DECLARE @Counter INT -- counting variable 

----------- Cursor specific code starts here ------------ 
-- company cursor 
declare copyCompanyDataCursor CURSOR fast_forward FOR 
SELECT ID from #CompanyID1; 

open copyCompanyDataCursor 
fetch next from copyCompanyDataCursor into @Company_Id; 

WHILE @@FETCH_STATUS = 0 
    BEGIN 

     declare @processorder int; 
     declare @tablename varchar(500); 
     -- table cursor 

     declare copyTableDataCursor CURSOR fast_forward FOR 
     SELECT processorder,tablename from #TableList order by processorder; 

     open copyTableDataCursor 
     fetch next from copyTableDataCursor into @processorder, @tablename; 

     WHILE @@FETCH_STATUS = 0 
     BEGIN 
      SET IDENTITY_INSERT [c365online_script1].[dbo].[tCompany] ON 

      -- Does the table have a companyID column? if statement checking for company id 
      IF EXISTS(SELECT * FROM Production2.INFORMATION_SCHEMA.COLUMNS 
       WHERE COLUMN_NAME='CompanyID' and TABLE_NAME='tProperty') 
        BEGIN 
        declare @debug varchar(max) 
          EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id) 
        END 
          ELSE 
        BEGIN 
          Print 'No' 
        END 
      -- if yes then copy data based on companyID in cursor 



      -- if no check if this is the first time through company loop and copy all data 
      -- if @firstloop company exists look at information schema 

        -- insert into c365online_script1.dbo.tCompany(selec 
    EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename) 

        -- company logic 


     SET IDENTITY_INSERT [c365online_script1].[dbo].[tCompany] OFF 

      FETCH NEXT FROM copyTableDataCursor into @processorder,@tablename; 
     END 

     close copyTableDataCursor; 

     Deallocate copyTableDataCursor; 

--INSERT INTO c365online_script1.dbo.tCompany 
--SELECT * 
--FROM production2.tCompany 
--WHERE ISNULL(CompanyID, 0) = 0 -- copy all data where id is equal to zero 
[email protected]Destination_Database_Name 

--  
     --EXEC(INSERT + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id + ')')  
     --SET @firstLoop = false; 
     FETCH NEXT FROM copyCompanyDataCursor into @Company_Id; 
    END 

CLOSE copyCompanyDataCursor; 
DEALLOCATE copyCompanyDataCursor; 

回答

1

代碼的第一行是

declare @Source_Database_Name AS varchar(255) = 'dbo.Production2' 

是您的數據庫實際上dbo.Production2

如果是這樣的話,您在使用該名稱時將需要方括號。即參考它作爲[dbo.Production2]

這是一個相當混亂的數據庫名稱dbo也是默認架構。因此,一般來說,您希望看到後的數據庫名稱爲。之前沒有。

+0

是的我的數據庫名稱是Production2當我做了[dbo.Production2] SQL說無效列名 – user2520671

+1

@ user2520671 - 你還沒有回答Martin問的問題。 Martin問你的數據庫是否叫做'dbo.Production2' - 你回答說'是的,它被稱爲'Production2'「 - 你沒有意識到這兩個字符串是不一樣的嗎? –

+0

數據庫的名稱只是Production2 – user2520671