Querying SQL Server on remote host arises a Character Set Problem - sql-server

When calling to SQL SERVER on remote hosting with query below a problem occurs:
SELECT COLUMN_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '..ş'
Table Names, having Local Culture Alphabet do not return in query, although they are in schema. For example, tables with Turkish Chars like 'ı' 'ş' not seen in query result...

you have to set collation when creating or altering:
use master
ALTER DATABASE YourDataBase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE YourDataBase COLLATE Turkish_CI_AS
ALTER DATABASE YourDataBase SET MULTI_USER
this time no need to use N' in each query.

Related

How can I disable autogrowth in SQL Server wide

I have a database server that some databases with restricted users are in use in the database. I need to restrict users to can't change .MDF and .LDF autogrowth settings. Please guide me to restrict the users.
I think there is two way to get this access:
Disable autogrowth in databases
Limit the maximum size of MDF and LDF
But I couldn't find any option in Management Studio to do them server wide and also get access from users.
Thanks.
you can execute following ALTER DATABASE command which sets auto growth option to off for all databases using undocumented stored procedure sp_Msforeachdb
for single database (Parallel Data Warehouse instances only)
ALTER DATABASE [database_name] SET AUTOGROW = OFF
for all databases
EXEC sp_Msforeachdb "ALTER DATABASE [?] SET AUTOGROW = OFF"
Although this is not a server variable or instance settings, it might help you ease your task for updating all databases on the SQL Server instance
By excluding system databases and for all other databases, following T-SQL can be executed to get list of all database files and output commands prepared can be executed
select
'ALTER DATABASE [' + db_name(database_id) + '] MODIFY FILE ( NAME = N''' + name + ''', FILEGROWTH = 0)'
from sys.master_files
where database_id > 4
To prevent data files' autogrow property to be changed, I prepared below SQL Server DDL trigger once I used a DDL trigger for logging DROP table statements.
Following trigger will also prevent you to change this property, so if you need to update this property, you have to drop this trigger first.
CREATE TRIGGER prevent_filegrowth
ON ALL SERVER
FOR ALTER_DATABASE
AS
declare #SqlCommand nvarchar(max)
set #SqlCommand = ( SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)') );
if( isnull(charindex('FILEGROWTH', #SqlCommand), 0) > 0 )
begin
RAISERROR ('FILEGROWTH property cannot be altered', 16, 1)
ROLLBACK
end
GO
For more on DDL Triggers, please refer to Microsoft Docs

ALTER DATABASE [...] SET SINGLE_USER WITH NO_WAIT waits 20 seconds

On SQL Server 2012, when I am issuing a statement
ALTER DATABASE [...] SET SINGLE_USER WITH NO_WAIT
and someone else is using the database, I have to wait for approximately 20 seconds until the request fails, despite the NO_WAIT termination clause.
Is it possible to tell SQL Server to let the "ALTER DATABASE" request fail immediately? Preferrably also for older versions of SQL Server? (although it seems that "ALTER DATABASE [...] SET" is not supported in SQL Server 2005 and earlier).
Give this a try. It checks for active connections to the database before trying to put it into SINGLE_USER mode.
use master
GO
DECLARE #DBName SYSNAME = N'YourDbName'
IF NOT EXISTS (
SELECT sd.Name, sp.*
FROM sys.sysprocesses sp
JOIN sys.sysdatabases sd
ON sd.dbid = sp.dbid
WHERE sd.Name = #DBName
AND sp.spid <> ##SPID
)
BEGIN
ALTER DATABASE [...]
SET SINGLE_USER WITH NO_WAIT
END

Restore stored procedure

I have a database in SQL Server 2008 R2 and I created this stored procedure for restoring databases:
CREATE PROCEDURE [dbo].[usp_DBRestore]
#DBName nvarchar(60)
,#BackName nvarchar(120)
,#OutMessage nvarchar(4000) output
--,
--#DataName varchar(60),
--#DataFileName varchar(120),
--#LogName varchar(60),
--#LogFileName varchar(120)
AS
BEGIN TRY
USE [master]
ALTER DATABASE #DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE #DBName FROM
DISK = #BackName WITH
FILE = 1, NOUNLOAD,
REPLACE,
PASSWORD = 'TEST'
SET #OutMessage = 'OK';
ALTER DATABASE #DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE
END TRY
BEGIN CATCH
ALTER DATABASE #DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE
INSERT [dbo].[ErrorLog]
(
[UserName],
[ErrorNumber],
[ErrorSeverity],
[ErrorState],
[ErrorProcedure],
[ErrorLine],
[ErrorMessage]
)
VALUES(
CONVERT(sysname, CURRENT_USER),
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_PROCEDURE(),
ERROR_LINE(),
ERROR_MESSAGE()
)
END CATCH
When I execute code I see this error :
a USE database statement is not allowed in a procedure, function or
trigger.
How can I solve this error?
You cannot do this in that way - you basically have two options:
stick to a stored procedure, but in that case, you have to use dynamic SQL. Your stored procedure creates a string of SQL statements, which allows it to use USE master and it allows it to dynamically set the database name etc., and then it executes that SQL statement using sp_executesql #sqlRestoreStatement. If you want to check this out, you MUST be all means read (and understand) Erland Sommarskog's seminal article The Curse and Blessings of Dynamic SQL
you can use a regular SQL script, possibly with SQLCMD placeholders (if you have SQLCMD mode enabled in your SQL Server Management Studio) and execute the restore from a regular script (which you can put into your own template folder, for instance). In that case, you'd have something like:
:setvar dbname YourDatabaseNameHere
DECLARE #FileName NVARCHAR(255)
SET #FileName = N'D:\YourBackupDirectory\SomeDatabase.bak'
RESTORE DATABASE [$(dbname)]
FROM DISK = #FileName
WITH FILE = 1,
MOVE N'YourDatabase_Data' TO N'D:\MSSQL\Data\$(dbname).mdf',
MOVE N'YourDatbase_Log' TO N'D:\MSSQL\Data\$(dbname)_Log.ldf',
NOUNLOAD, REPLACE,
STATS = 2
GO
With this setup, you can easily use the SQL script as a template and restore any kind of database using it.
You don't need the USE statement. Best is to remove Use statement and create / Alter this sp on master database itself.
If you want to take a backup execute this SP from master DB. I can not see any other way out.
You can create a linked server and have that referenced in your stored procedure.
For example. LinkedServer.database.[dbo].StoredProcedure
Check out this
How to create the linked server for SQL Server 2008 where we have the database from 2000 and 2005

set collation automatically

My application's database mydb has different collation than tempdb. I get a lot of exceptions comparing string values between tempdb temporary tables and mydb persistent tables.
It was decided to create mydb with same collation as tempdb.
The collation must be set automatically using script. I've tried this:
DECLARE #SQLCollation sql_variant
SELECT #SQLCollation = DATABASEPROPERTYEX('tempdb', 'Collation')
ALTER DATABASE mydb COLLATE #SQLCollation -- doesn't work
ALTER DATABASE mydb COLLATE Latin1_General_CI_AS -- works, but doesn't suit me because I have to type in collation myself in this SQL
So how do I set the same collation for mydb as for tempdb?
You can't just change DB collation with ALTER DATABASE. This only changes system databases (object names etc). (Dalex's answer)
You have to follow the steps detailed in the answers to Changing SQL Server Database sorting.
Another option to use the COLLATE Database_Default to coerce collation without knowing what it is. See SQL Server error "Implicit conversion of because the collation of the value is unresolved due to a collation conflict." and SQL Server - is there a way to mass resolve collation conflicts
Changing collation of the db will not change the collation of already existing tables in the db.
Another option would be to specify the collation to use when you create your temp table.
create table #TempTable
(
Name varchar(10) collate database_default
)
Your comparisons will then work just fine asuming that all your tables character fields have the same collation as the database.
DECLARE #SQLCollation NVARCHAR(1000)
SELECT #SQLCollation = 'ALTER DATABASE MyDb COLLATE '+CAST(DATABASEPROPERTYEX('Tempdb', 'Collation') as NVARCHAR(1000))
exec (#sqlcollation)

How to remove Case Sensitive check in SQL Server 2008?

I just installed SQL Server 2008 and imported AdventureWorksDatabase (for SQL 2005, as for 2008 that didn't worked).
It is now enforcing case sensitivity when I searched for any tables, views etc
Hence Person.contact table when written throws an error of Invalid column name, but when Person.Contact is written it shows all rows.
Plus the intellisense too doesn't work great.
Case sensitivity is controlled by the collation the database uses - check this by querying the system catalog views:
select name, collation_name
from sys.databases
A collation name will be something like: Latin1_General_CI_AS
The _CI_ part is telling me here that it's a case insensitive collation. If you see a _CS_ then it's a case sensitive collation.
You can change a database's default collation using:
ALTER DATABASE AdventureWorks COLLATE .......
and pick any valid collation here - use one with a _CI_ to get a case-insensitive collation.
Trouble is: even if you change the collation on the database level, certain tables might still have individual column that had a specific collation defined when the table was created. You could also change all of these, but that's going to be a bigger undertaking. See this article for more info and a script to check and possibly change individual columns in your tables.
The reason the intellisense might not be working properly is that the case sensitivity of database objects per se is controlled by the server collation - which can again be different from any database default.
To find out what the server's collation is, use:
SELECT SERVERPROPERTY('Collation')
Changing the server's system collation is quite a messy process and requires you to use the original setup.exe as explained here.
The problem here is Case Sensitivity of the table name Contact. You should set collation_name of the Database AdventureWorks as Latin1_General_CI_AS
Check for collation_name:
SELECT name, collation_name
FROM sys.databases
WHERE name = 'AdventureWorks';
GO
If the collation_name is Latin1_General_BIN or Latin1_General_CS_AS change it to Latin1_General_CI_AS
ALTER DATABASE AdventureWorks
COLLATE Latin1_General_CI_AS ;
GO
If the Database has locked to do this action "The database could not be exclusively locked to perform the operation." . Alter the Database to Single User
ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE
and do
ALTER DATABASE AdventureWorks
COLLATE Latin1_General_CI_AS ;
GO
Revert back the Database to Multi User finally
ALTER DATABASE AdventureWorks SET MULTI_USER WITH ROLLBACK IMMEDIATE
Or
You can change the Collation in Database Properties.

Resources