How to Submit a T-SQL script

132 Views Asked by At

I'm new to SQL Server and was trying some SQL statements for my database.

I want to enable identity insert with the following statement:

SET IDENTITY_INSERT tableName ON

That works fine. After that I inserted an entry but if I close the script the changes are lost.

I also tried

 EXEC sp_MSforeachtable @command1="PRINT '?'; SET IDENTITY_INSERT ? ON",
                        @whereand = ' AND EXISTS (SELECT 1 FROM sys.columns WHERE object_id = o.id AND is_identity = 1) and o.type = ''U'''

to enable identity insert for all tables, but that did not work either.

My expectation was that I run that script once and it will change the DB for all time but if I try to insert some entries with identity column, it doesn't insert the entry.

0

There are 0 best solutions below