I am trying to execute an SQL statement with Begin - End and Cursor in between using Execute SQL processor in Apache Nifi. But this is throwing an error telling "Unable to execute SQL select query". How can I execute transact SQL statements in nifi? Which processor is best suited for this?
Below is the SQL code snippet that I am trying to execute,
DECLARE @Cursor CURSOR
DECLARE @stlsn binary(10), @endlsn binary(10), @sequal binary(10), @op char, @upm varbinary(128), @rn numeric, @tg char(10), @ti char(10), @ln char(10), @dv char(10), @cid int
BEGIN
SET @Cursor = CURSOR FOR
SELECT * FROM cdc.dbo_SampleDB_CT
OPEN @Cursor
FETCH NEXT FROM @Cursor INTO @stlsn, @endlsn, @sequal, @op, @upm, @rn, @tg, @ti, @ln, @dv, @cid
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @op, @rn, @tg, @ti, @ln, @dv
IF @op= 1 Execute ('DELETE FROM dbo.NewDB WHERE RNo = '+@rn)
IF @op= 2 Execute ('INSERT INTO dbo.NewDB VALUES ('+@rn+','''+@tg+''','''+@ti+''','''+@ln+''','''+@dv+''')')
FETCH NEXT FROM @Cursor INTO @stlsn, @endlsn, @sequal, @op, @upm, @rn, @tg, @ti, @ln, @dv, @cid
END;
CLOSE @Cursor ;
DEALLOCATE @Cursor;
END;
This doesn't need to be dynamic at all. You can just do a joined DELETE
and an INSERT...SELECT...
.
You haven't specified your column names so I'm guessing a bit here.
DELETE FROM n
FROM dbo.NewDB n
JOIN cdc.dbo_SampleDB_CT c ON c.RNo = n.RNo
WHERE c.__$operation = 1;
INSERT INTO dbo.NewDB
SELECT c.RNo,c.tg,c.ti,c.ln,c.dv
FROM cdc.dbo_SampleDB_CT c
WHERE c.__$operation = 2;
I note that you haven't dealt with CDC update rows.