Archive

Archive for the ‘Tips & Tricks’ Category

Transact SQL ( T-SQL) to get all indexes in a database of SQL Server

June 22, 2018 Leave a comment

Below query can be used to get all indexes including heaps from an SQL server database. “IndexKeyOrder” specifies the order of the column within the index.

USE MyDatabase;

GO;

SELECT Coalesce(IX.name, ) AS IndexName

, OBJ.name AS ObjectName

,SCHEMA_NAME(OBJ.schema_id) AS SchemaName

, OBJ.type AS ObjectType –U= TABLES , V= VIEWS

, CASE WHEN OBJ.is_ms_shipped = 0x0 THEN 1 ELSE 0 END AS SystemObjects

, IX.index_id AS IndexID —- 1= CLUSTERED , 0 = HEAP, 2+ = NONCLUSTERED

, IX.type_desc as IndexType

, COL.name AS ColumnName

, IX_COL.is_included_column AS IsIncludedColumn

, IX_COL.key_ordinal AS IndexKeyOrder — 0 is for included columns

, IX.is_disabled AS IsDisabledFlag

FROM

sys.indexes AS IX

INNER JOIN sys.objects AS OBJ

ON IX.object_id = OBJ.object_id

AND OBJ.type IN (‘U’,‘V’) –U= TABLES , V= VIEWS

LEFT OUTER JOIN sys.index_columns AS IX_COL

ON IX.object_id = IX_COL.object_id

AND IX.index_id = IX_COL.index_id

LEFT OUTER JOIN sys.columns AS COL

ON IX_COL.object_id = COL.object_id

AND IX_COL.column_id = COL.column_id

ORDER BY SchemaName

, ObjectName

, IndexName

, IndexKeyOrder

;

Advertisements

What are SQL Query Hints?

July 5, 2014 Leave a comment

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…

 

Compound Operators in SQL Server

July 5, 2011 1 comment

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…

 

 

Understanding MERGE Transact SQL Statement

July 5, 2011 1 comment

By using “Merge”, you can perform insert, delete and update on a table in a single SQL statement. This helps in synchronizing the tables as well.

However while using merge there are several points that you need to keep in mind. Few

  • In a “Merge” statement, a “When Matched” clause with a search condition cannot appear after a ‘When Matched’ clause with no search condition.
  • A “Merge” statement must be terminated by a semi-colon (;).
  • An action of type ‘DELETE’ is not allowed in the ‘WHEN NOT MATCHED’ clause of a MERGE statement.
  • At least one of the three MATCHED clauses must be specified, but they can be specified in any order. A variable cannot be updated more than once in the same MATCHED clause.
  • Any insert, update, or delete actions specified on the target table by the MERGE statement are limited by any constraints defined on it, including any cascading referential integrity constraints.

Few examples of merge are as given below.

Read more…

 

 

Executing the script in batch file using “sqlcmd” command

April 25, 2011 1 comment

 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 written as
 
 
.

Search for a pattern in sql server using patindex() and Charindex()

October 8, 2010 2 comments

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.

The general syntax is

PATINDEX ( ‘%pattern%’ , expression )

You might have already noticed the ‘%’ symbols used in the pattern. These are important, as I mentioned earlier, use the same syntax as used for searching by  LIKE operator.

Continue Reading …

Reindex the database with high fragmentation using “DBCC DBREINDEX”

October 8, 2010 Leave a comment

DBCC is one of the most commonly used commands by DBAs across the globe. Today I will introduce a simple way to reindex a specific or all indexes in a table using DBCC.

The command used for this is

DBCC DBREINDEX

General Sytax

DBCC DBREINDEX

(

table_name

[ , index_name [ , fillfactor ] ]

)

The best feature of this command is that,

Continue Reading …