The JPA LOCK TABLE statement causes an exception for SQL Server - sql-server

In order to prevent concurrent updates to a table by different threads, we protect it with
em.createNativeQuery("LOCK TABLE mytable IN EXCLUSIVE MODE").executeUpdate();
where em is a JPA EntityManager.
This is working correctly in Postgres but not in SQL Server (and I think in Oracle it may not be working either).
Is there something to enable in SQL Server so that the above JPA statement does not give a " SQL Error: 156, SQLState: S0001, Incorrect syntax near the keyword 'TABLE'" error?

Related

Setting up Azure SQL data sync's sync group

I just tried setting up Azure SQL data sync's sync group using sample Azure SQL database. I am syncing dbo.BuildVersion table of the sample database.
I get the following error on member database.
Database provisioning failed with the exception "Incorrect syntax near the keyword 'NOT'.Inner exception: SqlException ID: 39f49622-6a56-4a44-8e55-2a646f99a584, Error Code: -2146232060 - SqlError Number:156, Message: SQL error with code 156 For more information, provide tracing ID ‘679953bc-7dac-4490-89e9-ea6d145d0442’ to customer support."
How should I resolve this issue?
Thanks
I am able to resolve this issue by creating empty table in the member database first and then running the sync.
Does Sync service not create table when table does not exist in the member database?
It does create it, however, under certain circumstances it fails to create it do to some issue with the schema itself. Therefore, the workaround, as you figured out, is to create the table manually.

SQL Server 2016 Stored Procedure Syntax Error

I recently restored a SQL Server 2008 database into an instance of SQL Server 2016. Most of the functionality seems to work fine, but many of my stored procedures that include updates to an application database table called SYS_USER fails with the following error:
Msg 102, Level 15, State 1, Procedure SYS_USERupdate, Line 35 [Batch Start Line 0]
Incorrect Syntax near '#errorNumber'
The database does not have a stored procedure called SYS_Userupdate and none of the procedures' code includes the term #errorNumber. I attempted to run the SQL query from one of the failing procedures directly in SQL Server Management Studio and received the same error message. Here is the SQL query that is failing:
UPDATE SYS_USER
SET SYS_USER_LGF_DT = GETDATE()
WHERE SYS_USER_ID = #SYS_USER_ID
I plugged in a valid value for the #SYS_USER_ID variable. Similar queries in related to other tables run without an issue. All of the stored procedures work on a SQL Server 2008 instance with no errors. Also, the database compatibility_level to 100, which should be acceptable for SQL Server 2016.
This happens when you're not running on a correct version of SQL Server, or if the compatibility level of the database isn't set sufficiently. So change it to 130
To check compatibility level:
select compatibility_level
from sys.databases
where name = '<database name>'
To alter compatibility level:
alter database <database-name>
set compatibility level = 130 -- SQL Server 2016
Compatibility levels list for all SQL versions: ALTER DATABASE (Transact-SQL) Compatibility Level
Reposting the answer since it was proposed in a comment to the question. The solution to the problem was that there were triggers on the affected tables that I did not know were there. Syntax errors on these triggers were causing insert and update queries on the tables to fail.
Credit to Jeroen Mostert https://stackoverflow.com/users/4137916/jeroen-mostert for helping with this!

SQL Server 2008 R2 Not able to resolve Merge conflict

I am running a merge replication. During the merge process there is a conflict. If I choose the winner, the conflict just comes back. If I choose a loser, I get an error.
TITLE: Microsoft Replication Conflict Viewer
Microsoft Replication Conflict Viewer encountered an error executing the following query:
set DATEFORMAT mdy if (select OBJECTPROPERTY(OBJECT_ID('[tABLE]'),'TableHasIdentity')) =1
[SQL INSERT STATEMENT]
ADDITIONAL INFORMATION:
The insert failed. It conflicted with an identity range check constraint in database [Database], replicated table [TABLE]. , column [TABLE]. If the identity column is automatically managed by replication, update the range as follows: for the Publisher, execute sp_adjustpublisheridentityrange; for the Subscriber, run the Distribution Agent or the Merge Agent.
The statement has been terminated. (Microsoft SQL Server, Error: 548)
I have run sp_adjustpublisheridentityrange to fix this, but it does not resolve the issue or change the range on the table.
Any ideas?
I found orphaned records in the subscriber. Once deleted, the conflicts resolved themselves.

SQL Job error: The query processor could not produce a query plan

Running this SQL from a SQL Server Agent Job:
delete p
from sometable p
join sometable_deletes src on src.primarykeyid = p.primarykeyid
But I get this error:
Error: 8624, Severity: 16, State: 116.
Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services.
The SQL runs fine directly in SQL Server Management Studio. Also, from a SQL Server Agent Job, I can run a similarly structured delete statement fine with different tables. But there is something wrong with this particular statement.
How can I figure out what the actual issue is here? Or get more error info?
I have no idea why, but adding this to the top of the SQL in the SQL Server Agent Job resolved the issue for me:
set QUOTED_IDENTIFIER ON

sybase: fail to drop default of a column

We want to change a column type from smallint to decimal. The 'alter table' sql statement passed in a UAT db server but failed in a production server. DBA suggests it could be related to the 'default' constraint of the column but that is all the help we got.
I am thinking of dropping the default off first before altering the column type.
I tried this sql statement:
alter table MyTable alter MyColumn drop default
But I got this error message:
Error: Incorrect syntax near the keyword 'alter'.
SQLState: ZZZZZ
ErrorCode: 156
What is wrong with this syntax? The target DB is 15.5
I used this as syntax reference
I'm going to guess that you may be using Sybase ASE, given the version number you listed, and the fact that the Sybase IQ syntax gave you an error message.
For SAP Sybase ASE you would drop the default using the sp_unbindefault command.
sp_unbindefault 'MyTable.MyColumn'
Documentation:
Adaptive Server Enterprise 15.5 > Reference Manual: Procedures > System Procedures

Resources