Query to track all the indexes with fragmentation in a database.
SELECT
OBJECT_NAME(object_id) AS TablelName
,(SELECT name FROM sys.indexes WHERE object_id = a.object_id and index_id = a.index_id) IndexName
,avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(‘<<DatabaseName>>’), NULL, NULL, NULL, NULL) a
WHERE avg_fragmentation_in_percent > 40
AND index_type_desc IN(‘CLUSTERED INDEX’, ‘NONCLUSTERED INDEX’)
ORDER BY avg_fragmentation_in_percent DESC
The <<DatabaseName>> needs to be filled with the database of your choice.
Categories: Joins, Query, SQL Queries, SQL Server, SQL Server, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: clustered index, fragmentation, index, non clustered index, SQL Server, Transact SQL
There was a time when we all wished SQL Server supports compound operators just like any other programming languages. Wouldn’t it be nice to have a shorter syntax for assigning the result of an arithmetic operator? Well, SQL server 2008 introduced Compound operators that allow you to perform several arithmetic operations using an operand along with equal to (=) operand.
For example:
Declare @var1 int
Set @var1 = 150
Set @var1 += 100
Read more…
Categories: Operators, SQL Queries, SQL Server, SQL Server, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: Compound Operators, SQL Server, Transact SQL, tsql
SQL server usually selects the best query plan required to execute a T-SQL query. However, there might be cases where the plans selected are not the best. In such situations, you can use the query hints to suggest the best possible plan/method of executing a query. The Query hints can be applied on all operators in a query. These hints apply to SELECT, DELETE, INSERT, UPDATE and MERGE. You can use the Query hints in the OPTION clause after the query.
Few commonly used Query hints with examples are as below.
Read more…
Categories: Joins, Query, Query Hints, SQL Queries, SQL Server, T-SQL, T-SQL, T-SQL Tags: CONCAT UNION, HASH UNION, Join Hints, Maxdop, MERGE UNION, OPTIMIZE FOR, Query Hints, SQL Server, Table Hints
Everyone reading this must have come across these datatypes, but have you ever thought what are the differences and advantages between these two?
The obvious difference that comes to our mind is related to the length. Yes! You are right. The varchar supports variable length and Char supports fixed length. This is the main advantage varchar datatype. for example, let us consider varchar(10) and char(10) . To store “GEEKEPISODE”, both the datatypes will use the complete length allocated and to store “GEEK”, CHAR datatype will use the complete 10 characters allocated, but VARCHAR will use only 4.
From the above examples it might seem that VARCHAR is better than CHAR all the time. But remember, CHAR was included as a separate datatype because it has certain advantages over VARCHAR.
VARCHAR uses additional space to store the length of the string stored in it and to mark the end of the string. Whereas, CHAR datatype doesn’t use this additional space. This certainly is the advantage of CHAR datatype.
Summary:
Read more…
Categories: DataTypes, DataTypes, SQL Queries, SQL Server, Stored Procedures, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: Advantage, CHAR, Datatype, Difference, SQL Server, VARCHAR
I often create batch files to execute sql scripts ( specially SPs) and then schedule it using SQL agent job/Task Scheduler. In this post , I will show you how to create a batch file with sql commands.
First of all, The command that is used to execute SQL queries from commnd prompt is “SQLCMD“. The general syntax of this command is available in the link. I will go with a simple one as below
sqlcmd –E –S Servername –Q Query -o “Output Path”
here
-E = Trusted connection (windows authentication)
-S = Server Name
-Q = CommandLineQuery
-o = Output file path
a simple command on the commnad prompt can be
C:\Users\geekEpisodes> sqlcmd -E -S “Local-SRV01″ -Q “Select top 10 * from MyTestDatabase..[ForecastHistory]” -o “C:\New Folder\Output.txt”
All the commands that you execute in the command prompt can be executed from batch/command files. to create a batch file with SQL commands follow the steps below
Read more…
Categories: SQL Queries, SQL Server, SQL Server, Stored Procedures, String Functions, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: command prompt, sql queries, SQL Server, sqlcmd, T-SQL
November 16th, 2010
admin
You can use the string functions to manipulate the string values in the result set. String functions are used with both char and nchar data types. The list below contains the set of string functions available in SQL Server 2005.
Read more…
Categories: Functions, Functions and DMVs, SQL Queries, SQL Server, String Functions, T-SQL, T-SQL, T-SQL Tags: functions, SQL Server, string, string functions, T-SQL, Transact SQL
It is often necessary to change the database to single user mode, especially if you are a DBA. A simple example would be to change the collation settings or any DB settings. The single user will allow only one user ( usually DBA) to access the database. Hence it will be easy to make changes without the worry of deadlocks or any other type of contention for DB and also without affecting the users.
It is very easy to change the database to Single user mode; in fact, it is just an execution of the script away. Use the script below to change the mode.
ALTER DATABASE <<Database Name>> SET SINGLE_USER WITH NO_WAIT
Read more…
Categories: Alter, alter, Procedures, SQL Queries, SQL Server, SQL Server, Stored Procedures, System Stored Procs, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: dba, multi user, server, single user, sp_dboption, SQL, SQL Server, T-SQL
Many times , it may be necessary to remove Not Null constraint used for a column in a table. It is usually necessary to overcome unexpected integrity constraints ( rarely occurs, if designed well) against a table. We can remove the Not Null constaint by using ALTER TABLE …. ALTER COLUMN … option.
Read more…
Categories: Alter, alter, alter, SQL Queries, SQL Queries, SQL Server, SQL Server, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: Alter, alter column, alter table, not null, null, SQL Server
Although these functions are not commonly used, these are really powerful in searching a pattern and returning the location of text/ string/ pattern. By understanding the functionality of both the string functions, we will be able to understand the difference as well.
PATINDEX()
The function searches for a pattern in a string / expression and returns the starting position of the first occurrence of the pattern. It works similar to the LIKE operator in any query.
Read more…
Categories: Functions and DMVs, SQL Queries, SQL Server, String Functions, T-SQL, T-SQL, T-SQL, Tips & Tricks Tags: charindex, functions, patindex, query, SQL Server, string functions, Transact SQL