I am trying to call a Sql sequence in Thingworx . i wrote a sql query service in a thing and tried executing it. But it shows the following error "Execute Query failed: com.microsoft.sqlserver.jdbc.SQLServerException: NEXT VALUE FOR function cannot be used if ROWCOUNT option has been set, or the query contains TOP or OFFSET."
how to resolve this?
Thanks in Advance
This is not a TWX issue. I'd suggest reading up on proper SQL query implementation.
If you tried executing your query directly in SQL Manager, you'd get the same error.
Instead of executing the sequence as an query separately, i added ' next value for "sequence name" ' in the insert command values itself. like, insert into tablename (id) values (next value for sequenceName) . This solved kind of solved the aforementioned issue.
But, I have another similar issue here.
When i execute a stored procedure in thingworx as SQL query am getting that same old error "NEXT VALUE FOR function cannot be used if ROWCOUNT option has been set, or the query contains TOP or OFFSET." i tried executing this specific procedure in sql directly, it works fine there. But while executing it in Thingworx am getting this error.
Can I get some help regarding this?
Our SQL statements sent to the database are wrapped in an sp_prepexec transaction. From the context of this sp_prepexec transaction the DB won't allow us to invoke any nextval lookups. Thus, we're unable to get the value of the next sequence using our DB connector inside ThingWorx.
As a workaround, we can create a stored procedure on the DB to get the current value. Then we can invoke this stored procedure from ThingWorx to return the current value of the sequence. We'll need to increment this appropriately if we need to use this in an insert statement, but it's probably best if we let the DB auto-increment inserts as needed:
--Stored Procedure on the DB:
CREATE PROCEDURE [current_seq_val]
SET NOCOUNT ON;
SELECT CAST(current_value AS int) FROM sys.sequences WHERE name = '<sequenceName>';
Used in a ThingWorx service (this gives the current value, not the next value and does not increment the sequence):