2013-10-30 141 views
0

每次運行查詢時,都會出現語法錯誤。問題是,當我運行錯誤應該發生的代碼運行該部分的行,所以我不知道爲什麼我會得到下面的錯誤消息。SQL語法錯誤,語法不正確?

Msg 102, Level 15, State 1, Line 1 
Incorrect syntax near ')'. 
Msg 102, Level 15, State 1, Line 1 
Incorrect syntax near ')'. 

我的代碼:

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

declare @Company_Id 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' + @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 ' + @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]_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; 
+0

看來你有一對多或缺少一個'')'' - >'@Company_Id +')''和'@tablename +')''。 –

回答

1

貌似多餘的括號在每個行的末尾:

EXEC('INSERT' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id + ')') 

EXEC('INSERT ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ')') 

有一個右括號中的報價,但沒有打開支架對應於它。

+0

語法應該是什麼樣子,我試圖在代碼的開頭添加括號,但仍然出現錯誤 – user2520671

+1

Try: 'EXEC('INSERT INTO'+ @Destination_Database_Name +'.dbo。'+ @tablename +'SELECT * FROM '+ @Source_Database_Name +' .dbo。 '+ @tablename + '其中' + @Source_Database_Name +' .dbo。 '+ @tablename +' .CompanyID ='+ @Company_Id)' 和 'EXEC( 'INSERT INTO'+ @Destination_Database_Name +'.dbo。'+ @tablename +'SELECT * FROM'+ @Source_Database_Name +'.dbo。'+ @tablename)' – Free2Rhyme2k

+0

我現在收到此錯誤消息7202,Level 11,State 2 ,第1行 在sys.servers中找不到服務器'dbo'。驗證是否指定了正確的服務器名稱。如有必要,請執行存儲過程sp_addlinkedserver以將服務器添加到sys.servers。 – user2520671