Skip to main content

Posts

Showing posts from 2014

Difference between null and coalesce

ISNULL ISNULL function is used to replace the NULL value with specified value. It contains only two arguments. Same data type not compulsory. Example -1: SELECT ISNULL (NULL, 'Raki' ) AS [ISNULL] Output:  Raki Example -2: DECLARE @name VARCHAR ( 10 ) DECLARE @marks INT = 500 SELECT ISNULL ( @name , @marks ) AS [ISNULL] Output:  500 Example-3: SELECT ISNULL (NULL,NULL, 'Raki' ) AS [ISNULL]       Output:    Msg 174, Level 15, State 1,    The isnull function requires 2 argument(s). COALESCE Coalesce function is returns first non null value among arguments.    It contains multiple arguments. Same data type compulsory for arguments or precedence data type order should follow. Example-1: SELECT COALESCE (NULL,NULL, 'Raki' ) as [COALESCE] Output:  Raki Example-2: DECLARE @name VARCHAR ( 5 )= 'Raki' DECLARE @mark...

Charindex function in sql server

Charindex is used to find the position of the expression in another expression. Syntax: select charindex ( Exptofind , Exptosearch , [Start_Position] ) Examples: select charindex ( 'a' , 'Rakesh' ) [Position] -- Here 'a' position is 2 select charindex ( 'H' , 'Hello World' ) [Position] -- Here 'H' position is 1 select charindex ( ' ' , 'C# corner' ) [Position] -- Here [Space] position is 3 select charindex ( 'W' , 'C# corner' ) [Position] --Here 'W' position is 0 beacuse there no char 'W' in search Exp select charindex ( 'a' , NULL) -- if any of the exp is null then result also null select charindex (NULL, 'Rakesh' ) -- if any of the exp is null then result also null select charindex ( 'o' , 'Hello World' , 6 ) --Here 6 is the start postion of char to find the Exp Example-2: create table Employee (  Em...

Difference between delete and truncate

Delete Truncate In Delete Query we can use Where Clause In Truncate Query we can not use Where Clause Syntax DELETE FROM [Table_Name] or DELETE FROM [Table_Name] WHERE [Condition] Syntax: TRUNCATE TABLE [Table_Name] Comparing the Delete is Slower than truncate Comparing the Truncate is faster than the delete query In Delete Query we can delete single only or multiple rows by using where clause. In Truncate Query All the rows data truncated from table. We can use triggers when delete query is executed . We can not use triggers when truncate query is executed. Delete query is logs the data. Truncate can not log the data. Delete query delete the data from table using row by row scenario. Truncate query truncate the data from table using page by page scenario...

Running .Sql file from command prompt

In this article I am demonstrating the running the .sql files form command prompt. Step -1 create the table using the sql management studio. CREATE TABLE tbl_Student ( ID INT IDENTITY ( 1 , 1 ) PRIMARY KEY , Fname VARCHAR ( 100 ), Lname VARCHAR ( 100 ), Salary INT ) Step-2 INSERT INTO tbl_Student ( Fname , LName , Salary ) VALUES ( 'Rakesh' , 'Kalluri' , 10000 ) INSERT INTO tbl_Student ( Fname , LName , Salary ) VALUES ( 'Srujan' , 'Kumar' , 15000 ) INSERT INTO tbl_Student ( Fname , LName , Salary ) VALUES ( 'Raju' , 'Bhai' , 17000 ) INSERT INTO tbl_Student ( Fname , LName , Salary ) VALUES ( 'Dany' , 'Mark' , 18000 ) The above script file is saved in Desktop with the file name of Student.sql . The file is saved in desktop now just we need to run the file from command prompt. Step-3 Go to run - -> Type CMD - → Click OK ...

How to Reset identity column values in sql server

Here is the sample demonstration for reset identity column value Step-1 : Create table CREATE TABLE dbo . Emp ( ID INT IDENTITY ( 1 , 1 ), Name VARCHAR ( 10 ) ) Step-2 :Insert some sample data INSERT INTO dbo . Emp ( name ) VALUES ( 'Rakesh' ) INSERT INTO dbo . Emp ( Name ) VALUES ( 'Rakesh Kalluri' ) When we run above query the second insert statement will failed because of varchar(10) length. Step-3 :Check the identity column value DBCC CHECKIDENT ( 'Emp' ) Even second insert was failed but the identity value is increased .if we insert the another record the identity value is 3 INSERT INTO dbo . Emp ( Name ) VALUES ( 'Kalluri' ) SELECT * FROM Emp Step-4: Reset the identity column value DELETE FROM EMP WHERE ID = 3 DBCC CHECKIDENT ( 'Emp' , RESEED , 1 ) INSERT INTO dbo . Emp ( Name ) VALUES ( 'Kal...

Back Up All the DataBases

Here is the simple script for backup all database except system database. declare @DbBackup varchar ( max )=null select @DbBackup = coalesce ( @DbBackup + char ( 10 )+ char ( 13 ), '' )+ 'backup database ' + Name + ' to disk=''D:\Backup\' + name + CONVERT ( varchar ( 10 ), getdate (), 112 )+ '.BAK''' from sys . databases where name not in( 'master' , 'tempdb' , 'model' , 'msdb' ) print @DbBackup

How Pass table variable parameter to Stored Procedure

Step-1: --create Emp type as table create type Emp as table ( Id int , Name varchar ( 50 ) null) Step-2: --create stored procedure with table variable parameter create proc sp_GetEmp ( @In_emp as dbo . Emp readonly ) as begin set nocount on select * from @In_emp set nocount off end Step-3: --Insert some dummy data declare @temp_emp as Emp insert into @temp_emp values ( 1 , 'rakesh' ),( 2 , 'raki' ),( 3 , 'ramu' ),( 4 , 'raju' ) Step -4: --exec stored procedure exec sp_GetEmp @temp_emp