可以"插入OPENQUERY"在交易中使用?

时间:2015-02-04 22:08:40

标签: mysql sql sql-server transactions openquery

我正在尝试在SQL Server中运行事务,我的代码将把数据插入到MySQL服务器中。

我在try catch块中开始一个事务,然后从那里提交/回滚。

问题在于我一直收到此错误

(1 row(s) affected)
OLE DB provider "MSDASQL" for linked server "MySQLSERVE1" returned message "[MySQL][ODBC 5.2(a) Driver]Optional feature not supported".

(0 row(s) affected)
Msg 3930, Level 16, State 1, Procedure xxx Line 132
The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.

这是我的代码,问题是什么?是否无法在交易中使用OPENQUERY

BEGIN TRY
    BEGIN TRANSACTION

        CREATE TABLE #finalList (
            Attemtps INT,
            CAMPAIGNNAME VARCHAR(255),
            FINISHCODE VARCHAR(255),
            CALLDATE DATETIME,
            AGENTID CHAR(60),
            Notes VARCHAR(8000),
            phone CHAR(10),
            MERCHANTAccount CHAR(20),
            CAMPID INT,
            user_id INT NULL,
            call_code_id INT NULL,
            result_code_id INT NULL,
            mid_id INT NULL,
            account_id INT NULL
        );

        CREATE INDEX mid ON #finalList(MERCHANTAccount);
        CREATE INDEX agent_id ON #finalList(AGENTID);
        CREATE INDEX FINISHCODE ON #finalList(FINISHCODE);
        CREATE INDEX CAMPAIGNNAME ON #finalList(CAMPAIGNNAME);

        DECLARE @campName varchar(255);
        DECLARE @sqlCommand varchar(max);
        DECLARE @sqlCommandMySQL nvarchar(max);
        DECLARE @LastRun datetime;
        DECLARE @OPENQUERY nvarchar(max);
        DECLARE @OPENQUERYFINAL nvarchar(max);
        DECLARE @NOW datetime;

        DECLARE MY_CURSOR CURSOR 
          LOCAL STATIC READ_ONLY FORWARD_ONLY
        FOR 
        SELECT LTRIM(RTRIM(CallListName)) AS CallListName
        FROM [SMSQL1].[MyDB].[dbo].[Campaigns]
        WHERE dialer_campaign = 1 AND i3Server ='I3New' AND ClientID = 111 AND (EndDate IS NULL OR EndDate >= getdate() - 7);

        OPEN MY_CURSOR
        FETCH NEXT FROM MY_CURSOR INTO @campName
        WHILE @@FETCH_STATUS = 0
        BEGIN 

            SET @OPENQUERY = 'SELECT @LastRun = lr FROM OPENQUERY(MySQLSERVE1,''';
            SET @sqlCommandMySQL = 'SELECT CAST(IFNULL(MAX(lastRun), DATE_SUB(NOW(), INTERVAL 7 DAY) ) AS DATETIME ) AS lr
                                    FROM guardian_dynamo.runtimes_i3
                                    WHERE CampaignListName = "'+@campName+'" '')';

            SET @OPENQUERYFINAL = @OPENQUERY + @sqlCommandMySQL;

            EXEC sp_executesql @OPENQUERYFINAL, N'@LastRun datetime OUTPUT', @LastRun output
            SET @NOW = GETDATE();
            --  --
            SET @sqlCommand = ' INSERT INTO #finalList(Attemtps, CAMPAIGNNAME, FINISHCODE, CALLDATE, AGENTID, Notes, PHONE, MERCHANTAccount)
                                SELECT ATTEMPTS, CAMPAIGNNAME, FINISHCODE, CAST(CALLDATE AS DATETIME) AS CALLDATE, AGENTID,Notes, PHONE, MERCHANTAccount 
                                FROM [I3_IC4].[dbo].['+ @campName +']
                                WHERE CALLDATE > '''+CAST(@LastRun AS varchar(60))+''' AND CALLDATE <= '''+CAST(@NOW AS varchar(60))+''' AND ISNULL(status, ''C'') IN (''U'', ''E'', ''A'', ''F'')   ';
            EXEC (@sqlCommand);

            INSERT INTO OPENQUERY(MySQLSERVE1, 'SELECT LastRun, CampaignListName
                                            FROM mysqlDB.runtimes_i3
                                            LIMIT 1') VALUES(@NOW, @campName);

            FETCH NEXT FROM MY_CURSOR INTO @campName
        END
        CLOSE MY_CURSOR
        DEALLOCATE MY_CURSOR;

        -- fix the attempts
        UPDATE #finalList
        SET Attemtps = Attemtps +1
        WHERE Attemtps > 0;

        -- Fix The Notes
        UPDATE #finalList
        SET user_id = 1310
        WHERE AGENTID = '';

        -- Fix The Notes
        UPDATE #finalList
        SET RDINotes = NULL
        WHERE RDINotes = '';

        -- UPDATE THE MIDS
        UPDATE f
        SET account_id = m.account_id,
            mid_id = m.mid_id
        FROM #finalList AS f
        INNER JOIN OPENQUERY(MySQLSERVE1, 'SELECT m.account_id, m.mid, m.id AS mid_id
                                        FROM mysqlDB.account_mids AS m
                                        INNER JOIN mysqlDB.accounts AS a ON a.account_id = m.account_id
                                        WHERE a.client_id = 3') AS m ON m.mid = f.MERCHANTAccount;

        -- UPDATE THE MIDS
        UPDATE f
        SET user_id = su.user_id
        FROM #finalList AS f
        INNER JOIN OPENQUERY(MySQLSERVE1, 'SELECT login_user AS agentid, user_id
                                        FROM mysqlDB.users') AS su ON su.agentid = f.AGENTID;

        -- UPDATE CALL CODES
        UPDATE f
        SET call_code_id = cc.call_code_id
        FROM #finalList AS f
        INNER JOIN OPENQUERY(MySQLSERVE1, 'SELECT call_code_title, call_code_id
                                        FROM mysqlDB.call_codes WHERE client_id = 3') AS cc ON cc.call_code_title = f.CAMPAIGNNAME;

        -- UPDATE CALL CODES
        UPDATE f
        SET result_code_id = rc.result_code_id
        FROM #finalList AS f
        INNER JOIN OPENQUERY(MySQLSERVE1, 'SELECT result_code_title, result_code_id
                                        FROM mysqlDB.result_codes') AS rc ON rc.result_code_title = f.FINISHCODE;

        -- SELECT * FROM #finalList where account_id IS NULL OR result_code_id IS NULL OR call_code_id IS NULL OR user_id IS NULL;
        DROP TABLE #finalList;
    COMMIT TRANSACTION
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
    BEGIN
        DROP TABLE #finalList;
        ROLLBACK TRANSACTION 
    END
END CATCH;

0 个答案:

没有答案