We create a sample database, but in the creation we set our identities to specific values. To turn on Identity Insert we use
context.Database.ExecuteSqlCommand ( "SET IDENTITY_INSERT dbo.Tool ON;" );
we then call
context.SaveChanges ();
and then execute another sql command to turn Identity Insert off.
When we Add the entity is there a way to insert Identity Insert ON SQL command as text before the ADD and then to insert Identity Insert OFF SQL command as text after the Add.
I picture SaveChanges as generating the SQL text that will do the insert, this is why I am wondering if there is a way to insert SQL commands.
Related
I want to use bulk insert task in SSIS but the problem is that in the connection tab destination table is not allowing me to type in the tablename that does not exists. I will create a table using t-sql task.
My SSIS task will be like
Execute sql task -- > Bulk insert task
Execute SQL Task creates a table dbo.mytable and now Bulk insert task should use this newly created table but it does not allow me to give the option to type the newly created table (dbo.mytable) as destination table .
I have written a transaction for mySQL InnoDB engine.
It has an insert in a table with auto generate key, than another insert using that auto generate key which I got using LAST_INSERT_ID().
Now after this second insert I have several inserts which need a foreign key for auto generated key from last table in which I have inserted.
So what I did was made a variable and used it is all of them.
Now, I need that auto generated key value to be returned in my Java program so I can use it.
How do I do it?
My transaction is fairly large so here is what I am trying to do.
start transaction;
insert into a(value) values(123);
insert into b(aid,value) values((select LAST_INSERT_ID()),345);
SET #KEY = ( select LAST_INSERT_ID() ) ;
insert into c(val,fk) values(1,#KEY);
insert into c(val,fk) values(2,#KEY);
insert into c(val,fk) values(3,#KEY);
.....
insert into c(val,fk) values(10,#KEY);
Now I need the #KEY variable value to be returned back in my program.
Its Java program I am using J connector for MySQL (If it matters).
MySQL variables are session-scoped you can do the following anywhere you want as long as you're using the same connection :
SELECT #KEY;
For more information, the manual is your friend : https://dev.mysql.com/doc/refman/5.0/en/user-variables.html
When generating database scripts, I'm scripting data to be migrated to a different environment. Is there a setting in the script generation that I can enable to set IDENTITY_INSERT on/off automatically so I don't have to go through each table manually in the generated script and set it? I'm using SSMS and I'd like to do this via SSMS.
Here's what I am getting:
INSERT my_table (my_table_id, my_table_name) VALUES (1, 'val1');
INSERT my_table (my_table_id, my_table_name) VALUES (2, 'val2');
Here's what I want:
SET IDENTITY_INSERT my_table ON
INSERT my_table (my_table_id, my_table_name) VALUES (1, 'val1');
INSERT my_table (my_table_id, my_table_name) VALUES (2, 'val2');
SET IDENTITY_INSERT my_table OFF
I know this is an old question, but the accepted answer and the comments to the accepted answer aren't quite correct regarding SSMS.
When using the generate scripts task in Sql Server Management Studio (SSMS) to generate scripts with data, set identity_insert statements will be included for tables that have an identity column.
In the object explorer: Tasks -> Generate Scripts -> [All Tables or selected tables] -> Advanced -> [Schema with Data or Data]
If the table to script data from does not have a column with the identity property , it will not generate the set identity_insert statements.
If the table to script data from does have a column with the identity property , it will generate the set identity_insert statements.
Tested & Confirmed using SSMS 2008 & SSMS 2012
In the OP's situation, I'm guessing the origin table did not have the identity property set for my_table_id in the source table, but the identity property was set for my_table_id in the destination table.
To get the desired output, change the table to script data from to have my_table_id to have the identity property.
This article explains in depth the steps to do this (without using the designer in SSMS): Add or drop identity property for an existing SQL Server column - Greg Robidoux
Create a new column with the identity property
Transfer the data from the existing id column to the new column
Drop the existing id column.
Rename the new column to the original column name
You didn't say what tool you are using to generate your scripts, but if you have a tool like Red-Gate Data Compare, it will generate these statements for you, if you include the auto-increment field in the comparison. I'm not aware that SSMS has any such option.
If you have Visual Studio Premium or Ultimate edition, then you also have access to DBPro (Database Professional) that has a data compare and synchronization option. I believe this will generate the IDENTITY_INSERT statements for you as well.
I'm trying to insert rows into a table via a trigger or stored procedure without writing any data to the binary log. Is this possible? I know that for normal connections you can set SQL_LOG_BIN=0 to disable binary logging for the connection, but I haven't been able to get that to work for triggers. If there's a way to do this with a federated engine table, that would also be OK.
edit:
I can almost accomplish this via a stored procedure:
CREATE PROCEDURE nolog_insert(`id` INT, `data` blob)
BEGIN
SET SESSION SQL_LOG_BIN = 0;
INSERT INTO `table` (`id`, `data`) VALUES (id, data);
SET SESSION SQL_LOG_BIN = 1;
END
I insert a record by calling the procedure from the mysql prompt:
call nolog_insert(50, 'notlogged');
As expected, the record (50, 'notlogged') is inserted into the table, but is not written to the binary log.
However, I want to run this procedure from a trigger. When using a trigger as follows:
create trigger my_trigger before insert on blackhole_table for each row call nolog_insert(new.id, new.data);
The data is both inserted to the table and written to the binary log.
If you run statement based replication triggers are executed on both the master and the slave but not replicated. Perhaps that could solve your problem.
Other than that, it's not allowed to change sql_log_bin inside a transaction so I would say that there is no good way to have the effects of a trigger not replicated when using row based replication.
I would like to copy the data after insert from one table to another table in another db on the same instance of the server. I am using Microsoft SQL Server 2008 Enterprise.
I thought that the easiest way it will be using a trigger but I have problems with this.
This is my code:
CREATE TRIGGER [DB1].[dbo].[myTrigger]
ON [DB1].[dbo].[myTable]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [DB2].[dbo].[tempTable] (L_myTable_ID, L_Mode, C_Name)
SELECT (L_ID, L_Mode, C_Name) FROM INSERTED
END
Any suggestions?