I'm trying to detect and silently discard any duplicate INSERTs into a table, and if a dupe detected, return the ID (Primary Key) of the existing record. Otherwise, insert the record and return the new ID.
I can do this with either a RULE or a TRIGGER, however both have drawbacks. Here's an example of my RULE:
CREATE OR REPLACE RULE territory_products_ignore_duplicate_inserts AS
ON INSERT TO territory_products
WHERE (EXISTS ( SELECT 1
FROM territory_products tp
WHERE tp.territory_id = NEW.territory_id AND tp.product_id = NEW.product_id)) DO INSTEAD SELECT tp.id
FROM territory_products tp WHERE tp.territory_id = NEW.territory_id AND tp.product_id = NEW.product_id LIMIT 1;
Testing this in a SQL console or psql with an INSERT works fine. If there's a dupe, it'll return the first existing record's id and not do the INSERT. Otherwise, it will go ahead with the INSERT. However, in Rails, it fails and returns this error:
ERROR: cannot perform INSERT RETURNING on relation "territory_products"
HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.
Moving to a TRIGGER, I try this:
CREATE OR REPLACE Function territory_products_ignore_dups() Returns Trigger
As $$
Begin
If Exists (
Select id From territory_products tp
Where tp.territory_id = NEW.territory_id And tp.product_id = NEW.product_id
) Then
Return NULL;
End If;
Return NEW;
End;
$$ Language plpgsql;
Create Trigger territory_products_ignore_dups
Before Insert On territory_products
For Each Row
Execute Procedure territory_products_ignore_dups();
This also works fine, except that I can't get it to return the existing ID, because of the Return NULL (which is required to disallow the INSERT).
Can anyone resolve either of these issues, so I get the result I'm looking for? (e.g. silently discard the INSERT in case of a dupe and return the ID of the existing record. Or if the INSERT is successful, return the ID of the new record).
Add a unique index to your database:
# migration file
add_index : territory_products, [:territory_id, :product_id], unique: true
In your logic look for the existing record or create if doesn't exist. It will raise an exception in case of a race condition. Just search again for the record.
#logic when finding/creating record
begin
TerritoryProduct.where(territory_id: params[:territory_id], product_id: params[:product_id]).first_or_create!
rescue ActiveRecord::RecordNotUnique
# In case it already exists
TerritoryProduct.where(territory_id: params[:territory_id], product_id: params[:product_id]).first
end
Related
I have a giant mysql sql dump file. But I'm getting error when I try to import it because of foreign key checks. Somehow there is missing data, so I'm importing it with
SET SESSION FOREIGN_KEY_CHECKS=0;
and it works, but I'm looking for a solution for missing data.
So is there any automatic way to find and delete relation data with missing entries to get a clean database dump, or I have to go and write manuel SQL for every relation, write query to delete missing values ?
You can automate a delete statement like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS check_foreign $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `check_foreign`()
BEGIN
DECLARE finishing INTEGER DEFAULT 0;
DECLARE vstmt VARCHAR(4000);
DECLARE vtbname VARCHAR(50);
DECLARE vtbnameref VARCHAR(50);
DECLARE vtbcol VARCHAR(50);
DECLARE vtbcolref VARCHAR(50);
DECLARE cr_tables CURSOR FOR select a.table_name, a.referenced_table_name, a.column_name, a.referenced_column_name from information_schema.KEY_COLUMN_USAGE a where a.table_schema = 'protocol_manager' and a.REFERENCED_TABLE_NAME is not null order by a.table_name;
DECLARE CONTINUE HANDLER FOR not found SET finishing = 1;
OPEN cr_tables;
SET vstmt = '';
table_foreign_delete: loop
if finishing = 1 then
leave table_foreign_delete;
end if;
FETCH cr_tables INTO vtbname, vtbnameref, vtbcol, vtbcolref;
SET vstmt = CONCAT(vstmt, char(10), 'DELETE FROM ', vtbname, ' a WHERE NOT EXISTS (SELECT 1 FROM ', vtbnameref, ' b WHERE a.', vtbcol, ' = b.', vtbcolref, ');');
end loop table_foreign_delete;
select vstmt;
END$$
DELIMITER ;
You can even do deep search to find a way to execute it dynamicly. For example a temporary table with a trigger. You generate a delete statement, insert it into the temp table, trigger the insert that fires a another (func, proc) to execute the statement generated.
I want to create basically an update query on Open Office Base (the same way with Ms ACCESS).
Base does not typically use update queries (but see below). Instead, the easiest way to do an update command is to go to Tools -> SQL. Enter something similar to the following, then press Execute:
UPDATE "Table1" SET "Value" = 'BBB' WHERE ID = 0
The other way is to run the command with a macro. Here is an example using Basic:
Sub UpdateSQL
REM Run an SQL command on a table in LibreOffice Base
Context = CreateUnoService("com.sun.star.sdb.DatabaseContext")
databaseURLOrRegisteredName = "file:///C:/Users/JimStandard/Desktop/New Database.odb"
Db = Context.getByName(databaseURLOrRegisteredName )
Conn = Db.getConnection("","") 'username & password pair - HSQL default blank
Stmt = Conn.createStatement()
'strSQL = "INSERT INTO ""Table1"" (ID,""Value"") VALUES (3,'DDD')"
strSQL = "UPDATE ""Table1"" SET ""Value"" = 'CCC' WHERE ID = 0"
Stmt.executeUpdate(strSQL)
Conn.close()
End Sub
Note that the data can also be modified with a form or by editing the table directly.
Under some circumstances it is possible to create an update query. I couldn't get this to work with the default built-in HSQLDB 1.8 engine, but it worked with MYSQL.
In the Queries section, Create Query in SQL View
Click the toolbar button to Run SQL Command directly.
Enter a command like the following:
update mytable set mycolumn = 'This is some text.' where ID = 59;
Hit F5 to run the query.
It gives an error that The data content could not be loaded, but it still performs the update and changes the data. To get rid of the error, the command needs to return a value. For example, I created this stored procedure in MYSQL:
DELIMITER $$
CREATE PROCEDURE update_val
(
IN id_in INT,
IN newval_in VARCHAR(100)
)
BEGIN
UPDATE test_table SET value = newval_in WHERE id = id_in;
SELECT id, value FROM test_table WHERE id = id_in;
END
$$
DELIMITER ;
Then this query in LibreOffice Base modifies the data without giving any errors:
CALL update_val(2,'HHH')
See also:
https://forum.openoffice.org/en/forum/viewtopic.php?f=5&t=75763
https://forum.openoffice.org/en/forum/viewtopic.php?f=61&t=6655
https://ask.libreoffice.org/en/question/32700/how-to-create-an-update-query-in-base-sql/
Modifying table entries from LibreOffice Base, possible?
I'm using Firebird 2.x and I have made a stored procedure to insert a record if it doesn't exist and return its ID into a variable.
But when I execute, it turns out that the following error occurs:
Dynamic SQL Error. SQL error code = -104. Unexpected end of command - line 2, column 76.
Full source code of my SP following:
CREATE PROCEDURE INSERT_ADMIN_OFFICE
AS
DECLARE VARIABLE OFF_ID BIGINT;
DECLARE VARIABLE PER_ID BIGINT;
DECLARE VARIABLE EMP_ID BIGINT;
DECLARE VARIABLE AP_ID BIGINT;
BEGIN
IF (NOT EXISTS(SELECT * FROM OFFICE OFF WHERE OFF.DESCRIPTION LIKE '%Administrador%')) THEN
INSERT INTO OFFICE (DESCRIPTION) VALUES ('Administrador') RETURNING ID INTO :OFF_ID;
ELSE
SELECT OFF.ID FROM OFFICE OFF WHERE OFF.DESCRIPTION LIKE '%Administrador%' INTO :OFF_ID;
INSERT INTO PERSON (NAME, BIRTH_DATE, ADDRESS, DISTRICT, CITY, STATE) VALUES ('Intellitools Desenvolvimento de Software Ltda.', '01/01/2007', 'Rua Nunes Machado, 472 - Cj 503', 'Centro', 'Curitiba', 'PR') RETURNING ID INTO :PER_ID;
INSERT INTO USER_PASSPORT (PERSON_ID, USER_NAME, PWD, TYPE) VALUES (:PER_ID, 'intellitools', 123, 1);
INSERT INTO EMPLOYEE (OFFICE_ID, PERSON_ID) VALUES (:OFF_ID, :PER_ID) RETURNING ID INTO :EMP_ID;
INSERT INTO ACCESS_PROFILE (DESCRIPTION) VALUES ('Administrador Geral') RETURNING ID INTO :AP_ID;
INSERT INTO REL_EMPLOYEE_ACCESS_PROFILE (EMPLOYEE_ID, ACCESS_PROFILE_ID) VALUES (:EMP_ID, :AP_ID);
SUSPEND;
END
;
I notice that this error is because of the INTO on the INSERT but I can't find another way to do that.
I appreciate your help!
Just remove SUSPEND and your proc will execute like a charm. For a one time actions I would suggest EXECUTE BLOCK instead of creating stored procedure.
NOTE: This is based on my question here but this is subtly different. I have resolved the issue on the other question, this is specifically regarding child entities.
I have a data-structure such that:
SecurityPolicy 1<---* SecurityPolicyRule
Therefore, a SecurityPolicy can have 0, one or many SecurityPolicyRules.
I am using Julie Lerman's Entity Framework book to implement some degree of concurrency checking, TDD and POCO support.
I understand that each table should have a rowversion/timestamp field, which is marked as ConcurrencyMode==Fixed.
I have decided to implement the CUD in Stored Procedures. My UPDATE Sproc is as follows:
create PROCEDURE dbo.sp_M2_Core_UpdateSecurityPolicy
#ID int,
#Name nvarchar(256),
#Comment nvarchar(max)=null,
#timestamp timestamp
AS
declare #nameExists nvarchar(256)
select #nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=#Name and [ID]<>#id
if (not #nameExists is null)
begin
raiserror (N'Name is already in use: %s',
11,
1,
#Name)
end
else
begin
update M2_Core_SecurityPolicy
set [Name]=#Name,
[Comment]=#Comment
where id=#id and [timestamp]=#timestamp
IF ##ROWCOUNT>0
SELECT [Timestamp] AS newTimeStamp FROM M2_Core_SecurityPolicy WHERE id=#id
end
go
create PROCEDURE dbo.sp_M2_Core_UpdateSecurityPolicyRule
(
#id int,
#RoleName nvarchar(256),
#Rank int,
#CanReadExecute bit=null,
#CanWrite bit=null,
#CanDelete bit=null,
#CanExport bit=null,
#Timestamp timestamp
)
AS
declare #roleExists nvarchar(256)
declare #securityPolicyID int
select #roleExists= [RoleName] from vw_aspnet_Roles where [RoleName]=#RoleName
if (#roleExists is null)
begin
raiserror (N'Role is not defined: %s',
11,
1,
#roleName)
end
else
begin
select #securityPolicyID=[SecurityPolicyID] from M2_Core_SecurityPolicyRule where [id]=#id
-- move all other rules up in priority
IF (SELECT COUNT(*) FROM M2_Core_SecurityPolicyRule WHERE [ID]<>#ID AND [SecurityPolicyID]=#SecurityPolicyID AND [Rank]=#Rank) > 0
BEGIN
UPDATE M2_Core_SecurityPolicyRule
SET [Rank]=[Rank]+1
WHERE [Rank] >= #rank
AND [SecurityPolicyID]=#SecurityPolicyID
AND [ID]<>#ID
END
update M2_Core_SecurityPolicyRule
set [RoleName]=#RoleName,
[Rank]=#Rank,
[CanReadExecute]=#CanReadExecute,
[CanWrite]=#CanWrite,
[CanDelete]=#CanDelete,
[CanExport]=#CanExport
where id=#id and [timestamp]=#timestamp
IF ##ROWCOUNT>0
SELECT [Timestamp] AS newTimeStamp FROM M2_Core_SecurityPolicyRule WHERE id=#id
end
RETURN
go
I am testing this using some code that:
Creates a Security Policy
Adds a created Security Policy Rule to the Security Policy
Adds the Security Policy
Saves the updates
Adds 1 to the Rank of the Security Policy Rule
Saves the updates
The test is below:
[TestMethod()]
public void AddWithSecurityPolicyRuleChangeRankTest()
{
ICoreContext coreContext = new CoreEntities(_coreDbConnectionString);
CoreUnitOfWork coreUnitOfWork = new CoreUnitOfWork(coreContext);
SecurityPolicyRepository target = new SecurityPolicyRepository(coreUnitOfWork);
int originalCount = coreContext.SecurityPolicies.Count();
string securityPolicyName = "addwithsecuritypolicyrulechangeruletest";
int originalRank = 1;
SecurityPolicy entity = new SecurityPolicy()
{
Comment = null,
Name = securityPolicyName,
SecurityPolicyRules = new FixUpCollection<SecurityPolicyRule>()
};
entity.SecurityPolicyRules.Add(
new SecurityPolicyRule()
{
CanDelete = null,
CanExport = null,
CanReadExecute = null,
CanWrite = null,
Rank = originalRank,
RoleName = "User"
});
target.Add(entity);
coreUnitOfWork.Save();
entity.SecurityPolicyRules[0].Rank=originalRank+1;
coreUnitOfWork.Save(); // <-- exception thrown here
SecurityPolicy savedSecurityPolicy = target.GetAll().Single(q => q.Name.Equals(securityPolicyName, StringComparison.CurrentCultureIgnoreCase));
Assert.AreEqual(originalRank+1,savedSecurityPolicy.SecurityPolicyRules[0].Rank);
}
However, when I run this, it throws an exception at the highlighted line. The exception is:
System.Data.OptimisticConcurrencyException
was unhandled by user code
Message=Store update, insert, or
delete statement affected an
unexpected number of rows (0).
Entities may have been modified or
deleted since entities were loaded.
Refresh ObjectStateManager entries.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64
rowsAffected, UpdateCommand source)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager
stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager
entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions
options)
at System.Data.Objects.ObjectContext.SaveChanges()
at MIGTurbo2.Core.Data.CoreEntities.Save()
in
D:\dev\migturbo2.0\MIGTurbo2.Core\Data\Core.Context.cs:line
92
at MIGTurbo2.Repositories.CoreUnitOfWork.Save()
in
D:\dev\migturbo2.0\MIGTurbo2.Repositories\CoreUnitOfWork.cs:line
26
at MIGTurbo2.Core.Tests.IntegrationTests.SecurityPolicyRepositoryTest.AddWithSecurityPolicyRuleChangeRankTest()
in
D:\dev\migturbo2.0\MIGTurbo2.Core.Tests\IntegrationTests\SecurityPolicyRepositoryTest.cs:line
524 InnerException:
And sure enough, no data has changed. ie. The [Rank] is still 1 from the first update (therefore, the INSERT). However, running it through SQL Profiler and Ayende's EF Profiler, no calls to the database are even made to make the UPDATE. So the relevance of the timestamp/rowversion is surely ... irrelevant?
What could be causing this? I don't want to have to Refresh the DB on every Save! This ONLY happens when updating the child entity. Changing the parent SecurityPolicy works fine.
Update 1:
When running the INSERT code, the SecurityPolicyRule is created fine. This issue is specific to UPDATE and DELETE operations.
Update 2:
I've figured out that if I set the state of the entity, it does get submitted to the database. The EF is obvioulsy picking up my State property (using the mechanism explained in Julie's book) fine now. So my test code now looks like this when updating the item:
entity.SecurityPolicyRules[0].Rank=originalRank+1;;
entity.SecurityPolicyRules[0].State = IWW.Elements.State.Modified;
However, despite the call and parameters sent to the database being present and correct, it is still not working. Interestingly, as shown in the screenie below, the last line is displayed AFTER the exception. Though I'm sure that this is only down to the Entity Framework Profile "flushing" its monitors.
Is the Stored Procedure of the child entity returning correct data?
I have a data-structure such that:
SecurityPolicy 1<---* SecurityPolicyRule
Therefore, a SecurityPolicy can have 0, one or many SecurityPolicyRules.
I am using Julie Lerman's Entity Framework book to implement some degree of concurrency checking, TDD and POCO support.
I understand that each table should have a rowversion/timestamp field, which is marked as ConcurrencyMode==Fixed.
I have decided to implement the CUD in Stored Procedures. My UPDATE Sproc is as follows:
create PROCEDURE dbo.sp_M2_Core_UpdateSecurityPolicy
#ID int,
#Name nvarchar(256),
#Comment nvarchar(max)=null,
#timestamp timestamp
AS
declare #nameExists nvarchar(256)
select #nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=#Name and [ID]<>#id
if (not #nameExists is null)
begin
raiserror (N'Name is already in use: %s',
11,
1,
#Name)
end
else
begin
update M2_Core_SecurityPolicy
set [Name]=#Name,
[Comment]=#Comment
where id=#id and [timestamp]=#timestamp
IF ##ROWCOUNT>0
SELECT [Timestamp] AS newTimeStamp FROM M2_Core_SecurityPolicy WHERE id=#id
end
go
create PROCEDURE dbo.sp_M2_Core_UpdateSecurityPolicyRule
(
#id int,
#RoleName nvarchar(256),
#Rank int,
#CanReadExecute bit=null,
#CanWrite bit=null,
#CanDelete bit=null,
#CanExport bit=null,
#Timestamp timestamp
)
AS
declare #roleExists nvarchar(256)
declare #securityPolicyID int
select #roleExists= [RoleName] from vw_aspnet_Roles where [RoleName]=#RoleName
if (#roleExists is null)
begin
raiserror (N'Role is not defined: %s',
11,
1,
#roleName)
end
else
begin
select #securityPolicyID=[SecurityPolicyID] from M2_Core_SecurityPolicyRule where [id]=#id
-- move all other rules up in priority
IF (SELECT COUNT(*) FROM M2_Core_SecurityPolicyRule WHERE [ID]<>#ID AND [SecurityPolicyID]=#SecurityPolicyID AND [Rank]=#Rank) > 0
BEGIN
UPDATE M2_Core_SecurityPolicyRule
SET [Rank]=[Rank]+1
WHERE [Rank] >= #rank
AND [SecurityPolicyID]=#SecurityPolicyID
AND [ID]<>#ID
END
update M2_Core_SecurityPolicyRule
set [RoleName]=#RoleName,
[Rank]=#Rank,
[CanReadExecute]=#CanReadExecute,
[CanWrite]=#CanWrite,
[CanDelete]=#CanDelete,
[CanExport]=#CanExport
where id=#id and [timestamp]=#timestamp
IF ##ROWCOUNT>0
SELECT [Timestamp] AS newTimeStamp FROM M2_Core_SecurityPolicyRule WHERE id=#id
end
RETURN
go
I am testing this using some code that:
Creates a Security Policy
Adds a created Security Policy Rule to the Security Policy
Adds the Security Policy
Saves the updates
Adds 1 to the Rank of the Security Policy Rule
Saves the updates
The test is below:
[TestMethod()]
public void AddWithSecurityPolicyRuleChangeRankTest()
{
ICoreContext coreContext = new CoreEntities(_coreDbConnectionString);
CoreUnitOfWork coreUnitOfWork = new CoreUnitOfWork(coreContext);
SecurityPolicyRepository target = new SecurityPolicyRepository(coreUnitOfWork);
int originalCount = coreContext.SecurityPolicies.Count();
string securityPolicyName = "addwithsecuritypolicyrulechangeruletest";
int originalRank = 1;
SecurityPolicy entity = new SecurityPolicy()
{
Comment = null,
Name = securityPolicyName,
SecurityPolicyRules = new FixUpCollection<SecurityPolicyRule>()
};
entity.SecurityPolicyRules.Add(
new SecurityPolicyRule()
{
CanDelete = null,
CanExport = null,
CanReadExecute = null,
CanWrite = null,
Rank = originalRank,
RoleName = "User"
});
target.Add(entity);
coreUnitOfWork.Save();
entity.SecurityPolicyRules[0].Rank=originalRank+1;
coreUnitOfWork.Save(); // <-- exception thrown here
SecurityPolicy savedSecurityPolicy = target.GetAll().Single(q => q.Name.Equals(securityPolicyName, StringComparison.CurrentCultureIgnoreCase));
Assert.AreEqual(originalRank+1,savedSecurityPolicy.SecurityPolicyRules[0].Rank);
}
However, when I run this, it throws an exception at the highlighted line. The exception is:
System.Data.OptimisticConcurrencyException
was unhandled by user code
Message=Store update, insert, or
delete statement affected an
unexpected number of rows (0).
Entities may have been modified or
deleted since entities were loaded.
Refresh ObjectStateManager entries.
Source=System.Data.Entity
StackTrace:
at System.Data.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64
rowsAffected, UpdateCommand source)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager
stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager
entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions
options)
at System.Data.Objects.ObjectContext.SaveChanges()
at MIGTurbo2.Core.Data.CoreEntities.Save()
in
D:\dev\migturbo2.0\MIGTurbo2.Core\Data\Core.Context.cs:line
92
at MIGTurbo2.Repositories.CoreUnitOfWork.Save()
in
D:\dev\migturbo2.0\MIGTurbo2.Repositories\CoreUnitOfWork.cs:line
26
at MIGTurbo2.Core.Tests.IntegrationTests.SecurityPolicyRepositoryTest.AddWithSecurityPolicyRuleChangeRankTest()
in
D:\dev\migturbo2.0\MIGTurbo2.Core.Tests\IntegrationTests\SecurityPolicyRepositoryTest.cs:line
524 InnerException:
And sure enough, no data has changed. ie. The [Rank] is still 1 from the first update (therefore, the INSERT). However, running it through SQL Profiler and Ayende's EF Profiler, no calls to the database are even made to make the UPDATE. So the relevance of the timestamp/rowversion is surely ... irrelevant?
What could be causing this? I don't want to have to Refresh the DB on every Save!
Update 1
Having run the SQL that should execute:
declare #t timestamp
select #t=[timestamp] from M2_Core_SecurityPolicyRule where ID=1
exec [sp_M2_Core_UpdateSecurityPolicyRule] #id=1, #roleName='User',#Rank=2,#Timestamp=#t
It works fine. There is something inside EF occurring that is blocking the call
Update 2
By breaking through the code, I find that the following occurs:
The item is created (obviously, Timestamp is null)
The item is added (Timestamp still null)
The changes are saved (this issues the INSERT)
The [Timestamp] field is then NOT UPDATED from the DB
Therefore, the subsequent UPDATE fails, as [Timestamp] IS NULL
So why would the [Timestamp] field not be updated?
Generally speaking, this is because the timestamp of the entity in the objectstatemanager no longer matches what is in the DB.
Call
coreContext.Refresh(RefreshOptions.StoreWins (or .ClientWins depending on what you want),entity);
to sync the entity and the DB before calling the save.
For a good post explaining optimistic concurrency see
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/457f2196-dd21-4188-8185-2561b954c54b
or
http://msdn.microsoft.com/en-us/library/bb738618.aspx
It seems that there I might have misunderstood Julie Lerman's book or there is a slight change required to how she implements her Stored Procedures.
I have changed the Model and Stored Procedures such that the SProcs return the Timestamp and the Model picks it up. This therefore means that the [Timestamp] field will not be null.
So the INSERT SProc now looks like:
create PROCEDURE dbo.sp_M2_Core_InsertSecurityPolicy
#Name nvarchar(256),
#Comment nvarchar(max)=null
AS
declare #nameExists nvarchar(256)
declare #id int
select #nameExists= [Name] from M2_Core_SecurityPolicy where [Name]=#Name
if (not #nameExists is null)
begin
raiserror (N'Name is already in use: %s',
11,
1,
#Name)
end
else
begin
INSERT INTO M2_Core_SecurityPolicy
([Name],Comment)
values
(#Name,#Comment)
IF ##ROWCOUNT > 0
BEGIN
SET #id=SCOPE_IDENTITY()
SELECT #id as ID,[Timestamp] FROM M2_Core_SecurityPolicy WHERE ID=#id
END
end
go
and the Mapping is changed so that it picks up the "new" field: