How can i change sql server collation? - sql-server

I read a heavy data from csv file to sql server table,but i cant encode that data with this encode code:
Encoding.GetEncoding(1256)
I read this link and notice this:
https://msdn.microsoft.com/en-us/library/ms186356.aspx
The code 1256 is Arabic Encode,and i want to change my database collation with under solution:
Right click on my database
choose option
change collation
But when click ok i get this error:
TITLE: Microsoft SQL Server Management Studio
Alter failed for Database 'behzad'. (Microsoft.SqlServer.Smo)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=12.0.2000.8+((SQL14_RTM).140220-1752)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Alter+Database&LinkId=20476
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
The database could not be exclusively locked to perform the operation.
ALTER DATABASE failed. The default collation of database 'behzad' cannot be set to Arabic_100_CI_AS_KS. (Microsoft SQL Server, Error: 5030)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=12.00.2000&EvtSrc=MSSQLServer&EvtID=5030&LinkId=20476
BUTTONS:
OK

You are getting this error because you have some active connections to your database.
Alter database statement needs to obtain schema lock on the database, but if there are any active connections it will fail hence the error you are getting.
Alternatively you can do the following:
Use master
GO
ALTER DATABASE [behzad]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
USE [behzad];
GO
USE master;
GO
ALTER DATABASE [behzad] COLLATE Arabic_100_CI_AS_KS
GO
ALTER DATABASE [behzad]
SET MULTI_USER;
GO

Related

SQL Server Database Stuck Single User

I have a SQL Server 2016 on a Windows Server 2016.
The database is stuck in single user mode.
I try to do this :
ALTER DATABASE MyDatabase
SET MULTI_USER;
But it says that the database is in use.
I tried this to find the spID :
exec sp_who
And I found the spid 57 is using the database,
Tried this to kill the spID
KILL 57
But it says : Process ID 57 is not an active process ID.
I am really stuck!
I can't even rename or delete the database.
I tried all of these but, didn't work :
SQL Server 2008 R2 Stuck in Single User Mode
Any idea please ?
It means that the DB is in use, it's set to single user mode, and you're not that single user. A common cause of that is that Object Explorer in SSMS is connected to the DB. Close everything that's connected to the server (even restart the SQL Server service if you need to), and try again. At worst, don't use SSMS. Just connect with SQLCMD, so you know that nothing else is connected.
I found the solution,
I restarted the sql server service, re-execute the query exec sp_who and found another spID and could kill it this time.
Thanks
From the Official docs you can try changing it a little bit by removing the read-only part
ALTER DATABASE [database_name]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [database_name]
SET MULTI_USER;
GO
Docs : https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-set-options?view=sql-server-ver15#b-setting-the-database-to-read_only

Could not import package. Warning SQL72012: The object exists in the target

I exported my Azure database using Tasks > Export Data-tier Application in to a .bacpac file. Recently when I tried to import it into my local database server (Tasks > Import Data-tier Application), I encountered this error:
Could not import package.
Warning SQL72012: The object [MyDatabase_Data] exists in the target, but it will not be dropped even though you selected the 'Generate drop statements for objects that are in the target database but that are not in the source' check box.
Warning SQL72012: The object [MyDatabase_Log] exists in the target, but it will not be dropped even though you selected the 'Generate drop statements for objects that are in the target database but that are not in the source' check box.
Error SQL72014: .Net SqlClient Data Provider: Msg 12824, Level 16, State 1, Line 5 The sp_configure value 'contained database authentication' must be set to 1 in order to alter a contained database. You may need to use RECONFIGURE to set the value_in_use.
Error SQL72045: Script execution error. The executed script:
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET CONTAINMENT = PARTIAL
WITH ROLLBACK IMMEDIATE;
END
Error SQL72014: .Net SqlClient Data Provider: Msg 5069, Level 16, State 1, Line 5 ALTER DATABASE statement failed.
Error SQL72045: Script execution error. The executed script:
IF EXISTS (SELECT 1
FROM [master].[dbo].[sysdatabases]
WHERE [name] = N'$(DatabaseName)')
BEGIN
ALTER DATABASE [$(DatabaseName)]
SET CONTAINMENT = PARTIAL
WITH ROLLBACK IMMEDIATE;
END
(Microsoft.SqlServer.Dac)
I followed the advice on other posts and tried to run this on SQL Azure database:
sp_configure 'contained database authentication', 1;
GO
RECONFIGURE;
GO
However, it says
Could not find stored procedure 'sp_configure'.
I understand the equivalent statement in Azure is:
https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-scoped-configuration-transact-sql?view=sql-server-2017
What is the equivalent statement to "sp_configure 'contained database authentication', 1;"?
The solution is to execute this against the master database of your local/on-premise SQL Server:
sp_configure 'contained database authentication', 1;
GO
RECONFIGURE;
GO
Thank you to David Browne - Microsoft and Alberto Morillo for the quick solution.
I had the same issue and it got fixed by importing the bacpac via command prompt. In link Import Bacpac go to SQLPackage section and run the command provided there.
sqlpackage.exe /a:import /tcs:"Data Source=<serverName>.database.windows.net;Initial Catalog=<migratedDatabase>;User Id=<userId>;Password=<password>" /sf:AdventureWorks2008R2.bacpac /p:DatabaseEdition=Premium /p:DatabaseServiceObjective=P6
The solution that worked for me was to directly copy DB from azure to my localhost.
Create a new database in your local DB server.
Right-click on a new database, Tasks -> Import data.
Provide data for Azure db:
Provide data for your localhost:
Click Next. Check the first checkbox to select all.
On the next form choose -> Run immediately
Wait until the process is completed.
Using the Azure portal to Import a bacpac:
The Collation chosen in the first step of the Azure SQL Import wizard, is important!
It needs to match the Collation in the source DB that was used to create the bacpac.
The sqlpackage and SSMS methods do NOT need this step.

SQL Server Hangs when creating view

In SQL Server 2014 when creating a new view SQL Server management studio hangs for about 10 minutes then comes up with the error...
failed to retrieve data for this request. An exception occurred while
executing a transact-SQL statement or batch. Lock request timeout
exceeded
We can then create and save a new view as normal. Any ideas why this is happening and how to solve it ?
Try unlocking the database before creating your view:
First see if there are any exiting connections:
SELECT request_session_id
FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID('[dbname]')
If there are any open sessions, kill them using:
kill 52
Then use this to unlock the database:
USE [dbname];
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Now you can create the view.
After that, don't forget to change the database to multi user mode:
ALTER DATABASE [dbname]
SET MULTI_USER

ALTER DATABASE returning Major Error 0x80040E14, Minor Error 25501

I am attempting to recreate a SQL Server Compact database from a script. I started off creating it like this:
CREATE DATABASE [MyDatabase]
GO
and that seemed to work. The next commands in the script are these:
ALTER DATABASE [MyDatabase] SET ANSI_NULL_DEFAULT OFF
GO
and about two dozen similar commands. I have tried a representative sample of these and they all return the error:
Major Error 0x80040E14, Minor Error 25501
ALTER DATABASE [MyDatabase] SET ANSI_NULLS OFF
There was an error parsing the query. [ Token line number = 1,Token line offset = 7,Token in error = DATABASE ]
Does anyone know what the matter is?
I'm using Microsoft SQL Server Management Studio 2008 R2 on Windows XP.
If you check here - http://msdn.microsoft.com/en-US/library/ms174454(v=sql.90).aspx, I don't think you can do alter database in SQL Server CE.

Drop SQL Server Database

I am creating a SQL Server database programmatically during a conversion. If the conversion code fails I want to delete/drop the database. If I use the shortcut menu for the database in SQL Server Management Studio 2005 the "delete" option is disabled. DROP DATABASE command also fails with message "Cannot drop database "XYZ" because it is currently in use."
I have shutdown and restarted the SQL Server and the database will not drop.
Any direction?
A new search found the following script that worked:
ALTER DATABASE [dbname]
SET SINGLE_USER --or RESTRICTED_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [dbname];
GO
Must have been some open transaction that was stopping the drop. Problem solved.
How long does your conversation takes.. Maybe you should consider using transactions, if that's an option. Just roll the transaction back when your conversation fails.

Resources