SQL SERVER – The Self Join

A self-join is joining a table to itself. This is done by using table name aliases to give each instance of the table a separate name. Self Join is very useful when you want to compare values in a column to other values in the same column. Self join and its example is very common question in the interview. Let us take a look to example

» Read more

SQL Server – SQL Scripts to find and Kill all the Blocked Process in a database

SQL Server database administrators frequently need in especially development and test environments to find and kill all the blocked process. Following scripts are useful in that scenario. Script – 1 — Find All the Blocked Processes SELECT spid, status, loginame=SUBSTRING(loginame,1,12), hostname=SUBSTRING(hostname,1, 12), blk = CONVERT(char(3), blocked), dbname=SUBSTRING(DB_NAME(dbid),1, 10), cmd, waittype FROM master.dbo.sysprocesses WHERE spid IN (SELECT blocked FROM master.dbo.sysprocesses)   Script 2 — Kill all the Blocked Processes of a Database DECLARE @DatabaseName nvarchar(50) — Set the Database Name SET

» Read more

SQL Server – Reset auto number (IDENTITY) column in SQL Server

How to reset auto number (IDENTITY) column in SQL Server? Sometimes we need to reset the auto number – identity column in a table. Using the DELETE statement is not enough. The statement only deletes the data but not reset the identity column. We can reset the auto number (IDENTITY) column in SQL Server using DBCC CHECKIDENT command. Following example shows how to reset auto number (IDENTITY) column the IDENTITY column Hope this post helps you.

» Read more

SQL Server – Error Handling Using TRY…CATCH

Prior to SQL server 2005, the only way of performing the error handling was to test vale of @@ERROR global variable. Once the SQL Server completes the execution of a Transact-SQL statement, it records the status of result in @@ERROR. If the error occurred, @@ERROR contains the error number. If the statement executed successfully it contain the 0. You need to check the variable if the statement succeeded or failed. Using @@ERROR to perform the error handling is not easy

» Read more

SQL Server – Get Comma Separated Values from table

I had requirement to get the Comma Separated Values from the table for specific column. So here sharing with you example how i handle this situation. USE tempdb GO CREATE TABLE test1 ( column1 INT, column2 VARCHAR(6) ) INSERT INTO test1 VALUES (1,'Test1'), (1,'Test2'), (2,'Test3'), (2,'Test4'), (3,'Test5') Now see the table SELECT * FROM TEST1  Create a function to get the Comma Separated Values as below CREATE FUNCTION dbo.CommaSeparatedvalues ( @parameter1 AS INT ) RETURNS VARCHAR(5000) AS BEGIN DECLARE @CSV

» Read more

TSEQUAL and JOIN syntax as *= or =* not supported in SQL Server 2008

I have faced an issue during SQL Server Migration. Here sharing with you. As I need to move some databases from SQL Server 2000 to SQL Server 2008 then I faced a problem because TSEQUAL and JOIN syntax such as *= or =* not supported in SQL Server 2008. Some of scripts are using the function and syntax as Example: TSEQUAL (TxnStatus, @ciTxnSts) JOIN syntax which has *= and =* to specify the OUTER JOIN. Solution: TxnStatus = @ciTxnSts instead of 

» Read more

SQL Server – Table-Valued Parameters

Table-valued parameters are a new parameter type introduced in SQL Server 2008. With the help of table-valued parameters, you can send multiple rows of data to a stored procedure or function, without creating a temporary table or many parameters. Steps to create Table-Valued Parameters Create a table type and define the table structure. Declare a stored procedure or function that has a parameter of the table type. Declare a variable of the table type, and reference the table type. Fill

» Read more

SQL Server – Local temporary table vs. Global temporary table

Temporary tables are a useful feature provided by SQL Server. Temporary tables created at runtime and can do all kinds of operations that one normal table can do. Because this is available at runtime, that’s why the scope of temporary tables is limited. These tables are created inside the tempdb database. There are two types of Temporary tables. Local Temporary table Global Temporary table Local temporary table: Local temporary tables are available to the current connection or sessions to the

» Read more

SQL Server – Database Read Only

Here is simple script with you can set your database in Read-Only Mode Once you specify database is in read-only mode then users can read data from the database, not modify it. Here is Script : Mark Database Read Only USE [master] GO ALTER DATABASE database_name SET READ_ONLY WITH NO_WAIT Similarly you can change database Read-Only Mode to Read-Write Here is script : Mark Data Read- Write Mode USE [master] GO ALTER DATABASE database_name SET READ_WRITE WITH NO_WAIT GO Alternative

» Read more

SQL Server – Import CSV file to SQL table – Bulk-Insert

Here is an example –  Import CSV file to SQL table using Bulk-Insert CSV file means  Comma Separated Values file. USE testdb GO CREATE TABLE table1 ( id INT, name VARCHAR(255), address VARCHAR(255) ) GO Create CSV file in Shared folder on any remote location with name csv.txt Suppose location of the file is \\dcc-566\Share\CSV.txt Content of CSV file as : Now let‘s Import this CSV file into Table1. BULK INSERT table1 FROM '\\dcc-566\Share\CSV.txt' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR

» Read more

SQL Server – Comma Separated INSERT Option

  Here is new feature of SQL Server 2008 – “Comma Separated INSERT Option” In this feature using a single INSERT statement you can pass multiple value for the table using comma separated option. USE testdb GO CREATE TABLE table1 ( id INT IDENTITY(1,1), name VARCHAR(255), address VARCHAR(255) ) SELECT * FROM testdb..table1 INSERT INTO testdb..table1 VALUES (‘Varinder’,’ABC Street’), (‘Amit’, ‘CDE Street’), (‘Dinesh’,’XYZ Street’) Result :    

» Read more

SQL Server – Insert same value multiple time in SQL table

  Here is an example:  Inserting same value multiple time in SQL table USE testdb GO CREATE TABLE table1 ( id INT IDENTITY(1,1), name VARCHAR(255), address VARCHAR(255) ) SELECT * FROM testdb..table1 INSERT INTO testdb..table1 VALUES (‘Varinder’,’ABC Street’) go 5 Result :  

» Read more
1 2 3