Files
mssql-interactions-python/useful-sql-scripts/useful.sql
T

88 lines
4.0 KiB
Transact-SQL

------------------------------------------------------
-- 1. Solutions to common issues
------------------------------------------------------
-- Resolving the "Divide by zero" error (by example)
DECLARE @denominator INT
SET @denominator = 0
--SELECT 1 / 0
SELECT 1 / ISNULL(NULLIF(@denominator, 0), 1)
------------------------------------------------------
--Handling the error “The conversion of a char data type to a datetime data
--type resulted in an out-of-range datetime value”
--Change the default language to “us_english” for the given SQL Server login:
USE [master];
ALTER LOGIN "LOGIN_NAME" WITH DEFAULT_LANGUAGE = us_english;
--Best practice: Always use the ISO date format in your data applications/T-SQL scripts: YYYY-MM-DD
------------------------------------------------------
--Handling the error “Database [Database_Name] cannot be upgraded because it is read-only
--or has read-only files”
--Make sure that the user account on which the SQL Server instance database engine is
--running has full access to the database files.
------------------------------------------------------
-- 2. Basic String Functions
------------------------------------------------------
-- Returns @length characters from @expression starting from @start_index
SELECT SUBSTRING(@expression, @start_index, @length)
SELECT SUBSTRING('This is a Test', 1, 4)
-- Finds the given @pattern in the @string and replaces it with the @replacement_string
SELECT REPLACE(@string, @pattern, @replacement_string)
SELECT REPLACE('This is a Test', 'Test', 'New Test')
-- Returns the size of @string in terms of number of characters
SELECT LEN(@string)
SELECT LEN('This is a Test')
-- Returns the first @num_chars characters of the @string counting from the left
SELECT LEFT(@string, @num_chars)
SELECT LEFT('This is a Test',4)
-- Returns the first @num_chars characters of the @string counting from the right
SELECT RIGHT(@string, @num_chars)
SELECT RIGHT('This is a Test',4)
-- Removes the leading blank spaces
SELECT LTRIM(@expression)
SELECT LTRIM(' This is a Test')
-- Removes the trailing blank spaces
SELECT RTRIM(@expression)
SELECT RTRIM('This is a Test ')
------------------------------------------------------
-- 3. Performance-Related Tips
------------------------------------------------------
-- Avoiding locking when reading data (however, dirty reads are allowed)
-- You need to take into consideration, possible dirty-read issues when using this approach
SELECT [columnName]
FROM [tableName] WITH (NOLOCK)
------------------------------------------------------
-- Rebuild a specific index with using parameters
USE [DATABASE_NAME];
ALTER INDEX [INDEX_NAME] ON [SCHEMA.TABLE]
REBUILD WITH (FILLFACTOR=[FILL_FACTOR_VALUE_BETWEEN_0_100], ONLINE=[ON|OFF]);
-- Rebuild all indexes in a table with using parameters
USE [DATABASE_NAME];
ALTER INDEX ALL ON [SCHEMA.TABLE]
REBUILD WITH (FILLFACTOR=[FILL_FACTOR_VALUE_BETWEEN_0_100], ONLINE=[ON|OFF]);
------------------------------------------------------
-- Updating database tables without causing blocking
-- You need to take into consideration, possible dirty-read issues when using this approach
UPDATE [TABLE_NAME] WITH (READPAST)
SET
WHERE
------------------------------------------------------
-- 4. Maintenance-Related Tips
------------------------------------------------------
-- Truncating a data/log file
USE [DBName];
DBCC SHRINKFILE ([Data_Log_LogicalName],TRUNCATEONLY);
-- Renaming a Windows login
ALTER LOGIN "[Domain or Server Name]\[Windows Username]"
WITH NAME="[New Domain or New Server Name]\[Windows Username]";
-- Renaming a SQL Server login
ALTER LOGIN "[SQL Server Login Name]"
WITH NAME="[New SQL Server Login Name]";
-- Creating Logins for orphaned SQL Server users
USE [DBName];
EXEC sp_change_users_login 'Auto_Fix', '[UserName]', NULL, '[Password]';
-- Changing the Database Owner in a SQL Server Database (SQL Login)
USE [DBName];
EXEC sp_changedbowner '[SQL_Login_Name]';
-- Changing the Database Owner in a SQL Server Database (Windows Login)
USE [DBName];
EXEC sp_changedbowner '[DomainNameUserName]'