Update referenced aggregates using SQL Server fails with ´Cannot insert ... when IDENTITY_INSERT is set to OFF´ - sql-server

We are using latest Spring Data JDBC milestone (1.1.0.M3) together with SQL Server.
Updating referenced aggregates (not the aggregate root itself) - fails with:
com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'mytable' when IDENTITY_INSERT i set to OFF
Updating the aggregate root itself works OK.
Any ideas or suggestions? We are locked to using SQL Server.
Note that the above works with H2 in-mem DB.

Spring Data JDBC doesn't support MS-SqlServer yet.
The currently blocking problem is that it doesn't allow insertion of IDs in columns that are declared as IDENTITY.
There is an issue for that: https://jira.spring.io/browse/DATAJDBC-278
You probably don't need the id on the referenced entity though. If you remove it the problem should go away.

Related

Delete Fails using ODBC in SSIS

I changed an Execute SQL Task targeting my default, local SQL Server instance from OLE DB to ODBC. The SQL is
delete from tablename
The ODBC version works fine when the table has records but fails when the table is empty.
I thought about using the result set of another SQL Task to populate a variable but that would not work. Reading this SO article Delete statement fails when called from SSIS identified the source of the problem (ODBC version), but it still didn't provide the answer. Other web articles suggested no workaround.
Are there any other methods or approaches to conditionally call the delete only if the table is not empty?.
It occurred to me to just use T-SQL to solve the problem. I used a T-SQL conditional statement which works fine. The ODBC SQL Task succeeds whether or not the table is empty.
if 0 < (select count(1) from tablename)
delete from tablename

SSDT/Visual Studio Database Project publishing DACPAC referenced DML table trigger SQL71501

I'm unable to publish SQL DML triggers inside of a VS Database/SSDT project. Everything else seems to publish updates/creations fine. However, my triggers aren't ever generated in the script. I can update/create stored procedures, table valued functions (TVFs), contracts, queues and services just fine using [dbo].[ElementName] references.
MSSQL Version: SQL Server 2014 RS2
i.e. these all work fine as their respective type (contract, service, stored procedure, etc.) and update/create based on the source/destination.
Contract:
CREATE CONTRACT [//Web/Queue/BasicContract]
(
[//Web/Queue/RequestMessage] SENT BY INITIATOR,
[//Web/Queue/ReplyMessage] SENT BY TARGET
)
Stored Procedure [$(SystemDb) is set as a SQLCMD variable]:
CREATE PROCEDURE [dbo].[z_Dist_Tans_Error]
AS
BEGIN
DECLARE #RecordCount INT
SELECT #RecordCount = COUNT(*) FROM [$(SystemDb)].dbo.bcerr
END;
However, I get a reference error with this trigger SQL code. Error is commented in-line:
CREATE TRIGGER [z_Trg_MatlAvailable_Delete]
ON [dbo].[Matlavailable] --SQL71501: Trigger: [dbo].[z_Trg_MatlAvailable_Delete] has an unresolved reference to object [dbo].[Matlavailable]
FOR DELETE
AS
BEGIN
END
I can add the SQLCMD variable into the trigger creation query. This fixes the compile errors. But, when publishing it doesn't seem to detect the trigger doesn't exist in the destination database so it's never created in the script.
CREATE TRIGGER [z_Trg_MatlAvailable_Delete]
ON [$(SystemDb)].[dbo].[Matlavailable] --Error Resolved but never published
FOR DELETE
AS
BEGIN
END
If I add the table's SQL definition inside of my project I can resolve those reference issues and it actually publishes the triggers. However, I'm attaching this DML trigger to our ERP system's database. I have no control over how that database's table will evolve over time and I don't want my projects "definition" of that table to override the existing one.
It seems like I can reference DACPAC tables fine in queries but not attaching triggers (and probably more). Is there some way to create triggers using [$(VariableName)].[Schema].[TableName] that publish with VS Database Projects or a way to reference DACPAC tables to create triggers using just [Schema].[TableName] ?

SQL Server addressing a table different than requested then firing "object name xxx invalid"

From the Object Explorer of SQL Server Management Studio I renamed a table from nameA to nameB. When I query SELECT * from nameB I receive the error
object name 'nameA' not valid
This happens both from Management Studio and an external application. SELECT * from nameA gives the same error.
Restarting the service didn't help. Table name in sysobjects is correct. Is there maybe another place where SQL Server looks up table names? Thanks, it's getting me quite crazy :S
I'm using SQL Server 11.0.2100.60
Solved with a workaround:
I generated scripts for table schema and data, dropped the table and run scripts to recreate table schema and data. NOTE: I needed to manually modify the generated scripts because of mistakes in smalldatetime fields which made the generated INSERTs fail.
So it seems that dropping tables resets the "denormalized" SQL Server internal catalog which was left inconsistent somewhen. Thanks MS! ;)

Restoring SQL Server 2014 database in SQL Server 2005

I am trying to restore a SQL Server 2014 database in SQL Server 2005. I am doing by generating the complete script (with schema and data) from SQL Server 2014.
I get an error saying
'INSERT failed because the following SET options have incorrect settings: 'ANSI_PADDING'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or query notifications and/or xml data type methods.'
and
'Cannot insert explicit value for identity column in table 'T_GASAdminNote' when IDENTITY_INSERT is set to OFF.'
I have searched for solution but have not been able to rectify the problem. I have tried the solution given in this link, but it didn't help me.
Can anyone suggest me some solutions for restoring the database.
Update: sorry - misread your question...
The first error means just that - for some operation you're trying to do, your SET ANSI_PADDING setting is wrong. Read the official documentation on ANSI_PADDING on MSDN to learn more about this setting. Check what your setting is now - obviously, if that error occurs, you need the other setting for that operation you're attempting to do.
The second error means that your table T_GASAdminNote has an identity column, and you're trying to insert values into that column, but without first enabling this by using SET IDENTITY_INSERT T_GASAdminNote ON (and don't forget to disable the option after you're done!)

Why is my subform not including the Foreign Key from the parent form in the INSERT operation in MS Access 2007?

The problem
When I insert a new record in a multiple item subform, it throws an error as it hasn't included the required foreign key from the parent form. The foreign key isn't allowed to be null, and it's trying to insert a null value.
A bit more information
This process worked fine previously, but I have since upgraded my backend from ACE to SQL Server 2005. It now throws this error:
Run-time error '3146':
ODBC--call failed.
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert the value NULL into
column 'EngagementLetterID_FK', table 'ClientDatabase.dbo.Services'; column does
not allow nulls. INSERT fails. (#515) [Microsoft][ODBC SQL Server Driver][SQL
Server]The statement has been terminated. (#3621)
As quick comment, the subform is for Service records, and the parent form is for EngagementLetter records. One EL has many Services.
I've checked the table set up, and it seems fine, but I would say the table is operating as it should - it shouldn't allow a null value. Neither do I think SQL Server should be responsible for identifying the correct FK - the frontend should do this. However, my front end was working fine before with the ACE backend. So I'm having trouble finding out what the culprit is, and I can't figure out how to do more advanced Access debugging (e.g. beforeUpdate, tell me what the SQL INSERT query will be).
I've checked some of my other parent-sub forms, and they are behaving fine.
Any ideas?
One other detail that I forgot to mention - I had done some dramatic front end rebuilding at the same time. With the subform, I had not correctly linked EngagementLetterID_FK with EngagementLetterID from the parent form. The result of this is that a new Service record has not had the EngagementLetterID_FK specified.
So now that I have corrected the link, it's working fine.

Resources