Skip to content

Instantly share code, notes, and snippets.

@umair-me
umair-me / tsql_query_in_transaction.sql
Last active May 4, 2018 10:52
Run query in transaction with error logs
BEGIN TRANSACTION;
BEGIN TRY
-- ADD SQL QUERIES HERE
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
@umair-me
umair-me / rebuild_indexes.sql
Created August 28, 2015 09:59
Rebuild indexes sql server
-- Re Index
EXEC sp_MSforeachtable "dbcc dbreindex('?', '')"
-- Update Stats
--EXEC sp_updatestats
-- or
EXEC sp_MSforeachtable "UPDATE STATISTICS ? WITH FULLSCAN"
@umair-me
umair-me / check_table_fragmentation.sql
Last active June 29, 2020 16:46
Check SQL table index fragmentation
SELECT OBJECT_NAME(ind.OBJECT_ID) AS TableName,
ind.name AS IndexName,
indexstats.index_type_desc AS IndexType,
indexstats.avg_fragmentation_in_percent ,
CONCAT('ALTER INDEX [', ind.name, '] ON [', OBJECT_NAME(ind.OBJECT_ID), '] REBUILD')
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats
INNER JOIN sys.indexes ind
ON ind.object_id = indexstats.object_id
AND ind.index_id = indexstats.index_id
WHERE indexstats.avg_fragmentation_in_percent > 30
@umair-me
umair-me / gist:8616eaf810256e849906
Created July 8, 2015 16:22
Forward domains using web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="https://www.domainname.com$S$Q" exactDestination="true" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
@umair-me
umair-me / new_gist_file
Created June 18, 2014 08:38
This method will return a string with spaces inserted before the capitals (excluding the first character).
/// <summary>
/// This method will return a string with spaces inserted before the capitals (excluding the first character).
/// So, "ThisIsAString" will become "This Is A String" (hopefully)
/// </summary>
/// <param name="s">The string that you want to be nicely formatted</param>
/// <returns></returns>
public static string InsertSpaceBeforeCapitals(string s)
{
char[] tmpTitleArr = s.ToCharArray();
Tap on the Windows-key to go to the start screen interface if you are not already there.
Type gpedit.msc and select the first result from the list.
This opens the Local Group Policy Editor.
Navigate to the following folder: Local Computer Policy > Computer Configuration > Administrative Templates > Windows Components > SkyDrive
Locate "Prevent the usage of SkyDrive for file storage" and double-click on the entry.
Switch its state from "not configured" to "enabled" and click ok.
@umair-me
umair-me / gitignore fix
Last active August 29, 2015 14:01
Remove commited ignored files .gitignore
Try this:
git rm -r --cached .
git add .
git commit -am "Remove ignored files"
Remove all items from index.
git rm -r -f --cached .
Rebuild index.
@umair-me
umair-me / select_by_datetime.sql
Last active November 3, 2015 11:22
Get records between two 2 dates
SELECT *
FROM TABLE
where datetime >= Convert(datetime, '2014-01-17 00:00:01', 120)
AND datetime <= Convert(datetime, '2014-01-17 23:59:01', 120)
@umair-me
umair-me / close_all_connections.sql
Last active January 19, 2016 16:07
Close all connections SQL server database
USE master
GO
ALTER DATABASE YourDatabaseName
SET OFFLINE WITH ROLLBACK IMMEDIATE
GO
/*Simple table grid style:*/
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}