0

Every time I run the query I get the syntax error. The thing is when I run the lines where the error is supposedly occurring the code runs that section so I don't know why I'm getting the below error messages.

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

My code:

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
--@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
  • It seems you have one to many or missing one ')' -> @Company_Id + ')' and @tablename + ')'. Commented Oct 30, 2013 at 10:27

1 Answer 1

1

Looks like extra brackets at the end of each of these lines:

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 + ')')

There is a closing bracket in quotes, but no opening bracket to correspond to it.

Sign up to request clarification or add additional context in comments.

5 Comments

what should the syntax look like, ive tried adding brackets to the beginning of the code but an error still occurs
Try: EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id ) and EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename )
I now get this error Msg 7202, Level 11, State 2, Line 1 Could not find server 'dbo' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
So the above fixed the syntax error. Run: select * from sys.servers to see whether your servers are linked.
Yes I ran the query and my server comes up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.