SQL Server 2016 - Invalid object name 'hibernate_sequence' - sql-server

I have an image backup that I restore to the MS SQL server 2016.
I have an entity that declares its id like that:
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#XmlID
#XmlElement
#XmlJavaTypeAdapter(IntToStringXmlAdapter.class)
private Integer id;
when I save the entity I receive:
Hibernate: select next_val as id_val from hibernate_sequence with (updlock, rowlock) 2018-02-28 22:05:41.935
ERROR 18152 --- [nio-8080-exec-6] o.hibernate.id.enhanced.TableStructure : could not read a hi value com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'hibernate_sequence'.
......
2018-02-28 22:05:41.942 WARN 18152 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 208, SQLState: S0002
2018-02-28 22:05:41.942 ERROR 18152 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : Invalid object name 'hibernate_sequence'.
I have created by hand the sequence to the SQL server and I make sure that it exist through the SSMS.
CREATE SEQUENCE hibernate_sequence
AS INTEGER
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99
NO CYCLE;
Despite of this I continue to the receive the previous error.
Any ideas what I am doing wrong?
Thank you in advance

Following points to check:
What dialect you are using?
What hibernate version you are using?
Version 5 changed the GenerationType.AUTO behavior
Set "hibernate.hbm2ddl.auto" to update and see what it creates in the database
Avoid GenerationType.AUTO. Set it explicit to GenerationType.IDENTITY or GenerationType.SEQUENCE depending on what you want or your DB supports.
Check if you have the latest SQL Server JDBC driver. I had issues with it migrating from hibertnate 4.3 to 5.0
In hibernate 5 set hibernate.id.new_generator_mappings to false

It seems you are upgrading SqlServer version. I faced it when i was upgrading SqlServer 2012 to SqlSever 2017.
Caused by : talk between driver (jtds or sqlserver ) and SqlServer 2017+ no more consider table "dbo.hibernate_sequence" as work-around. It needs specifically sequence with name hibernate_sequence.
solution : delete table named as dbo.hibernate_sequence and create sequence
Example :
USE [your_database_name]
GO
CREATE SEQUENCE [dbo].[hibernate_sequence]
AS [bigint]
START WITH 10000000
INCREMENT BY 1
MINVALUE -9223372036854775808
MAXVALUE 9223372036854775807
CACHE
GO
SqlServer Studio screenshot

You really need to look at the documentation for this. The query you have here is totally wrong. And why are you trying to use query hints on a sequence?
The correct syntax would look this.
select next value FOR hibernate_sequence as next_val
Here is the documentation on this. https://learn.microsoft.com/en-us/sql/t-sql/functions/next-value-for-transact-sql

I was also getting the same error in spring boot project.
In my case the error com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'sys.sequences' is showing because I was using sql server 2008 version which doesn't have sys.sequences object.
I switched to sql server 2017 and it is working fine.

I had this problem and in my case i had to change the dialect from SQLServerDialect to SQLServer2012Dialect. The first has no implementation for sequences.

Related

INSERT failure with SQLAlchemy ORM / SQL Server 2017 - Invalid Object Name

Firstly, I have seen many answers which is specific to the Invalid Object Name error working with SQL Server, but None of them seem to solve my problem. I don't have much idea on SQL Server dialect, but here is my current setup required on the project.
SQL Server 2017
SQLAlchemy (pyodbc+mssql)
Python 3.9
I'm trying to insert a database row, using the SQLAlchemy ORM, but it fails to resolve the schema and table, giving me the error of type
(pyodbc.ProgrammingError) ('42S02', "[42S02] [Microsoft][ODBC Driver
17 for SQL Server][SQL Server]Invalid object name 'agent.dbo.tasks'.
(208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]Statement(s) could not be prepared. (8180)")
I'm creating a session with the following code.
engine = create_engine(connection_string, pool_size=10, max_overflow=5,
pool_use_lifo=False, pool_pre_ping=True, echo=True, echo_pool=True)
db_session_manager = sessionmaker()
db_session_manager.configure(bind=engine)
session = db_session_manager()
I have a task object defined like
class Task(BaseModel):
__tablename__ = "tasks"
__table_args__ = {"schema": "agent.dbo"}
# .. field defs
I'm trying to fill the object fields and then do the insert like the usual
task = Task()
task.tid = 1
...
session.add(task)
session.commit()
But this fails, with the error mentioned before. I tried to execute a direct query like
session.execute("SELECT * FROM agent.dbo.tasks")
And it returned a result set.
The connection string is a URL object, which prints like
mssql+pyodbc://task-user:******#CRD4E0050L/agent?driver=ODBC+Driver+17+for+SQL+Server
I tried to use the SQL Server Management Studio to insert manually and check, there it shown me a sql dialect with [] as field separators
like
INSERT INTO [dbo].[tasks]
([tid]..
, but SQLAlchemy on echo did not show that, instead it used the one I see in MySQL like
INSERT INTO agent.dbo.tasks (onbase_handle_id,..
What is that I'm doing wrong ? I thought SQLAlchemy if configured with a supported dialect, should work fine (I use it on MySQL quite well). Am I missing any configuration ? Any help appreciated.

why 12c with extended varchar2 limit still converting sql server varchar(max) to long

Oracle version 12.1.0.2
max_string_size=extended
I am using sql server ODBC to connect to sql server database via Oracle gateway to sql server, the connection is working fine and i am able to access sql server tables.
However, as per Oracle documentation starting 12c and with extended limit on varchar2 data type the conversion of sqlserver varchar(max) to oracle Long will only happen if the length of sql server data is more than 32k.
My sql server table has few columns defined as varchar(max) in and all of those i see getting converted to LONG when i try to describe the table over dblink.
I need to load the data from sql server to oracle and the above problem is making it very difficult as more than one long columns can not be copied over dblink.
Any help will be deeply appreciated.
I created a view on the SQL server side that uses substr(column,1,4000) to fit within the old Oracle max 4000 character length. This worked quite well with Oracle 11.
I am in the process of migrating to a new Oracle 18 instance that uses character set AL32UTF8 instead of WE8MSWIN1252. The exact same SQL is now getting:
ORA-28500: connection from ORACLE to a non-Oracle system returned this message:
[Microsoft][ODBC Driver Manager] Program type out of range {HY003}
ORA-02063: preceding 2 lines from CEAV195
Fortunately I don't have a tight deadline for working this out.
Comment: I am now getting
[Error] Execution (8: 17): ORA-00997: illegal use of LONG datatype
despite using the following in the view on the SQL Server side:
cast(substring(cr.response,1,2000) as varchar(2000)) response
As I said earlier, this worked perfectly fine with Oracle 11 and the WE8MSWIN1252 character set.
I hit the same issue and found this solution elsewhere
set serverout on
DECLARE
l_cursor BINARY_INTEGER;
l_id VARCHAR2(60);
l_temp VARCHAR2(250);
l_notes VARCHAR2(32767);
BEGIN
l_cursor := DBMS_HS_PASSTHROUGH.open_cursor#remotedb;
DBMS_HS_PASSTHROUGH.parse#remotedb(
l_cursor,
'select "RecId","Notes" from "MySqlServerTable"'
);
LOOP
DBMS_HS_PASSTHROUGH.get_value#remotedb(l_cursor, 1, l_id);
DBMS_HS_PASSTHROUGH.get_value#remotedb(l_cursor, 2, l_notes);
DBMS_OUTPUT.put_line(l_id || ' ' || l_notes);
END LOOP;
exception
when others then
DBMS_HS_PASSTHROUGH.close_cursor#remotedb(l_cursor);
raise;
END;
/

TDS Error on Azure Entity Framework SQL Calls after Windows 10 April 2018 Update [duplicate]

This question already has answers here:
SqlClient error after updating Windows - "The incoming tabular data stream (TDS) remote procedure call > (RPC) protocol stream is incorrect"
(5 answers)
Closed 4 years ago.
I have the following C# code that executes against a SQL Server hosted in Azure.
protected IQueryable<T> FilterUpdatedRows<T>(IQueryable<T> query, DateTime lastSyncTimestamp) where T: class, ITimestamp
{
return query.Where(x => x.InsertTimestamp >= lastSyncTimestamp ||
(x.UpdateTimestamp.HasValue && x.UpdateTimestamp >= lastSyncTimestamp));
}
The Generic (T) are Entity Framework context classes that implement and interface that forces the InsertTimestamp and UpdateTimestamp to be defined.
This code has worked fine until I installed the April 2018 Update of Windows 10. Post update I get the following error ...
SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 2 (""): Data type 0x00 is unknown.
I confirmed that the parameters causing the issue are the InsertTimestamp and UpdateTimestamp fields.
This error only happens when I am targeting my copy of the SQL database on Azure. Exporting the database to a local database works without issue. Team members who have not upgraded to the April 2018 update have no issue when hitting the Azure version.
I ran a SQL Trace on my local version and captured the executing SQL statement. Running that statement through SSMS against the Azure version works without issue.
Here is the SQL statement captured by the trace ...
exec sp_executesql N'SELECT
[Extent1].[CrossingId] AS [CrossingId],
... Misc other fields ...
[Extent1].[CrossingInstanceId] AS [CrossingInstanceId]
FROM (SELECT
[V_SYNC_CrossingsSync].[CrossingId] AS [CrossingId],
... Misc other fields ...
[V_SYNC_CrossingsSync].[CrossingInstanceId] AS [CrossingInstanceId]
FROM [dbo].[V_SYNC_CrossingsSync] AS [V_SYNC_CrossingsSync]) AS [Extent1]
WHERE ([Extent1].[InsertTimestamp] >= #p__linq__0) OR (([Extent1].[UpdateTimestamp] IS NOT NULL) AND ([Extent1].[UpdateTimestamp] >= #p__linq__1))',N'#p__linq__0 datetime2(7),#p__linq__1 datetime2(7)',#p__linq__0='1980-01-01 00:00:00',#p__linq__1='1980-01-01 00:00:00'
Any thoughts on what may be going on?
Thanks
Temp Workaround:
Per ChainbridgeTech change MultipleActiveResultSets from TRUE to FALSE in my connection string, and the error stops.
This has been reported and is being worked on. They still need a repro and I'm still working on isolating shareable data that I can make public:
https://github.com/Microsoft/dotnet/issues/749

"The column cannot be modified because it is an identity, rowversion or a system column" - but it isn't

I am getting this error:
The column cannot be modified because it is an identity, rowversion or
a system column. [Column name = BatchClosed]
But [BatchClosed] is a nullable bit column and identity is false.
I am using Sql Server Compact Edition and the table is used in merge replication.
There are system columns ( _sysIG, _sysCG, _sysCD, _sysP1, _sysMC, _sysMCS, _sysSR) and a rowguid for the purpose of replication in the table.
The table is not marked as download-only in the publication.
The table is filtered though, and the BatchClosed field is used as a part of that filter:
WHERE surveyorid = convert(int, HOST_NAME()) AND BatchClosed = 0
When I test it in Management Studio connected to the Sql Server CE database with this sql I get the same error
UPDATE tblBatch SET BatchClosed = 0 WHERE BatchClosed = 1 AND SurveyID = 160;
Interestingly, this sql would not actually do an update because there are no records with BatchClosed = 1. (I assume that's just something to do with the way Sql Server CE works)
NB the test sql will work in Sql Server 2008 R2 but not on the Sql Server CE version after synchronization
EDIT
If I try to update any column in that table I get the same error message - as if all columns are system columns, not just the one in the filter
EDIT 2
I checked my installation and noted that the server tools had an older installation date while the x64 version was at SP1:
So I un-installed the x64 components, then downloaded and installed the server tools. It now looks like this:
I immediately lost my web synchronization. It took me a painful day of working through various dead ends before I found out how to get that back. (Solution here: Configuring Web Synchronization for Merge Replication to Sql Server CE)
Result? Still get the same error. :-(
I can both delete and insert in the table in question, and also update like this:
-- Script Date: 05-07-2014 09:26 - ErikEJ.SqlCeScripting version 3.5.2.39
UPDATE [tblBatch]
SET [SamplePercentage] = 0
WHERE BatchId = 2;
GO
I think you cannot update any other columns, as they are either system controlled (PK or rowguid) or participate in join filters in the publication. But to do updates, you can do a DELETE followed by an INSERT.

SQL Server 2005 and Datanucleus problem (ntext query)

Started getting errors with SQL Server 2005
This is the first time i've tested our app with Datanucleus 2.x (last test was made with DN 1.x)
I use Eclipse RCP.
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The ntext data type cannot be selected as DISTINCT because it is not comparable.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPrepared
Statement.java:390)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java
:283)
at org.datanucleus.store.rdbms.SQLController.executeStatementQuery(SQLController.java:463)
at org.datanucleus.store.rdbms.query.JDOQLQuery.performExecuteInternal(JDOQLQuery.java:755)
... 5 more
As the first line mentions, you are trying to do a DISTINCT on a result set that has ntext type fields inside it .. this is not possible..
have a look at Is there any way to DISTINCT or group by a text (or ntext) in SQL Server 2005? for possible solution. (cast the field to nvarchar(max))

Resources