adding lock option to Create Table statement in Sql Server 2005 - sql

I am busy creating tables in Sql Server from a Sybase database. In the Sybase database when the tables are created an option 'lock allpages' was used, how can I replicate this when creating the tables in Sql Server 2005.

In SQL Server you cannot specify a lock option for the table in CREATE TABLE. You can at most disable row level and page level locking by adding the WITH ALLOW_ROW_LOCKS = OFF or WITH ALLOW_PAGE_LOCKS_OFF. The equivalent of locking the entire table in SQL Server is to use a lock hint WITH (TABLOCK) when running queries and updates on the table, but that is not recommended.
My recommendation would be to just ignore this option when transferring the tables from Sybase to SQL Server.

What do you want to achieve with this "lock allpages" option? Is the database you're working on up and running productively? If not, in SQL Server, you can restrict access to the entire database to a single user:
ALTER DATABASE YourDatabaseName SET SINGLE_USER
and that way you're sure no one else if going to come in your way and fiddle around until you're totally done :-)
Set it back to "normal" usage with:
ALTER DATABASE YourDatabaseName SET MULTI_USER
Marc

Related

Alter Memory Optimized SQL Server 2014 Table

Can I alter my memory optimized table? Like adding column or changing data types etc. If yes, how to do it?
I am using SQL Server 2014
Thanks
According to Altering Memory-Optimized Tables (SQL Server 2014):
Performing ALTER operations on memory-optimized tables is not
supported. This includes such operations as changing the bucket_count,
adding or removing an index, and adding or removing a column. This
topic provides guidelines on how to update memory-optimized tables.
Updating the definition of a memory-optimized table requires you to create a new table with the updated table definition, copy the data to the new table, and start using the new table.
But it will be possible with SQL Server 2016:
In SQL Server 2016 Community Technology Preview 2 (CTP2) you can
perform ALTER operations on memory-optimized tables by using the ALTER
TABLE statement. The database application can continue to run, and any
operation that is accessing the table is blocked until the alteration
process is completed.
In the previous release of SQL Server, you had to manually complete
several steps to update memory-optimized tables.

Sql Merge Locking the Entire Target Table

I have used SQL Merge for CRUD operations in a table that holds records more than 3 million and due to merge locks the table this would gives time out errors on other selects and updates in the production environment.
Site has been accessed by multiple users at a given time and same table could be updated by different set of users at the same time.
so based on my scenario is using SQL merge is efficient or do i have to use INSERT/UPDATE/DELETE statements?
is there are any way that improve merge by only locking the data record instead of locking the entire table?
This is caused by a very stupid default setting in sql server.
For some reason sql server blocks all readers on a table while a writer is using that table. You can correct this oversight from microsoft like this :
ALTER DATABASE yourdatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE yourdatabase SET READ_COMMITTED_SNAPSHOT ON with NO_WAIT
ALTER DATABASE yourdatabase SET MULTI_USER
There is an excellent article about this here http://www.brentozar.com/archive/2013/01/implementing-snapshot-or-read-committed-snapshot-isolation-in-sql-server-a-guide/#comment-2220427

MSSQL Import/Export/Copy IDENTITY_INSERT problems

Using MS SQL Server Management Studio 2008.
I have a db (say ip 10.16.17.10 and called db1) and a second one (say ip 10.16.17.25 called db2).
I am trying to copy one table (and its contents) from db1 into db2.
I have the database on both (but empty in db2).
The problem is no matter how I copy/export/import, no matter what options I set in MS SQL Server Management Studio 2008 when I click 'table'->'Design' (on db2) it ALWAYS says 'Identity Spefication: NO' even tho the db1 table has it on.
From db1 I go to 'Tasks'->'export'->'source/db' and 'destination/db'->'Edit Mapping'->'Enable identity Insert' and click it on.
But no joy. ALWAYS exports without it.
I try similar thing from IMPORT on db2. Similar thing if I use COPY.
I have read MANY of the STACKOVERFLOW articles on this, they all suggest setting IDENTITY_INSERT setting to ON but when I do run below:
SET IDENTITY_INSERT [dbo].[mytable] ON
The table either doesn't exist yet or has already copied WITHOUT the identity setting on so see the error:
does not have the identity property. Cannot perform SET operation.
I have tried setting it as a property (under database properties) for db2 but when I copy/import/export never works.
Would appreciate any help here as lots of StackOverflow articles so far all seem to be having an easier time than me.
I am planning on doing this for another 50 or so tables in this database so am hoping to find a way which doesnt involve running scripts for each table.
thanks
The process of using the Export Data Wizard to copy the data from one table to another will NOT replicate all aspects of the schema (like identity and auto-increment). If you want to replicate the schema, script out your table into a create statement, change the name to db2, and create it. Then you should be able to run the export/import wizard with the identity insert option on and insert into your new table that replicates the schema of your old table.
Ended up sorting this out using MS SQL Management Studio.
Thanks to #kevin for the help regarding Import Data and Export Data. Schemas are NOT transferred across however they are the best means to transport the data once schema is up.
Found best way to MASS import/export db table schemas using below (Saved SQL create scripts to file):
Tasks->Generate Scripts->All Tables To File->with Identity on
Ran 200kb SQL file on db2 for schema.
Then ran Import Data from db1 to db2.
Done, all Identity_Inserts maintained.
thanks for help
According to the Error message I think your table does not have an IDENTITY column. Make sure that [dbo].[mytable] does have an IDENTITY column before you executing SET IDENTITY_INSERT.
SET IDENTITY_INSERT [dbo].[mytable] ON
DEMO1 (Trying to set identity ON when there is NO identity column)
--Error
'Table 'T' does not have the identity property. Cannot perform SET operation.: SET IDENTITY_INSERT T ON'
DEMO2 (Trying to set identity ON when there is identity column)
--No Errors
Follow following Steps :
From db1 I go to 'Tasks'->'export'->'source/db' and 'destination/db'->'Edit Mapping'->'Enable identity Insert' and Edit SQL - > You will able to see query structure of Table.
IN the query for eg. ID int NOT NULL, do the next step ID int NOT NULL IDENTITY(1,1)
Then proceed.
I bet it will work.

When is the temporary database used?

I've been told that RDBMS ( SQL Server in this case ) make use of the temporary database to perform its internal job, for instance when a SELECT count( column ) FROM foo query is performed.
What kind of queries / statements trigger the use of the temporary database?
background:
We are currently about to change the collation on our application database, but we have been told there might be problems if that database make use of the temporary database, because they will have different collation. The rationale is the temporary database is already being used by other applications.
So we want to identify what kind of queries may trigger temp db usage and see if they'll have any problem.
I've found this about when is the db used:
http://msdn.microsoft.com/en-us/library/ms190768.aspx

Data difference between two database

I am working on SQL Server 2005. I have took the backup and restore the database. I just wanted to know is there any way I can check whether the data between these two database are diffent. or any script that I can run or check [ could be table by table] to see if any new row added or modified.
EDITED
For example - I have to check the data difference from Database 1 Table_User to Database 2 Table_User.
Thanks
You can use the built-in command line tool TableDiff.exe:
http://msdn.microsoft.com/en-us/library/ms162843(SQL.90).aspx
It performs row by row comparisons along with column-level comparisons. Here's an article that walks through how to use it:
http://www.databasejournal.com/features/mssql/article.php/3594926/SQL-Server-2005-TableDiff-Utility.htm
And another (with a link to a GUI that you can download):
http://weblogs.sqlteam.com/mladenp/archive/2007/03/03/60125.aspx
This would be on a table by table basis - but it's free and already installed for you since you have SQL Server 2005 installed. Hope this helps!
you can use sql compare and sql data compare.(red-gate.com)
or you can use sql delta.(sqldelta.com)
I think you want to track what are changes in your restored database comparing to that before it was restored. If so, I hope this may be helpful.
ALTER DATABASE yourdatabasename SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
GO
USE yourdatabasename;
GO
ALTER TABLE Person.Person ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON);
GO