SQL SERVER – How to work with Row Constructors , inserted and deleted tables and Handle Result Set

There are many ways to track changes made to the table in SQL Server one of the method is to use inserted and deleted tables and get the result set and use in code appropriately.

Step 1: Creating Table

create table Emp

(

empId int primary key identity,

class int,

empName varchar(30),

sal money,

empState varchar(25)

)

Step 2: Inserting values into the table

use row constructors or insert one by one

insert into Emp values

(2,’Dilip’,3000,’AP’),

(2,’Rahul’,5000,’DH’),

(3,’Mohan’,7000,’KR’),

(2,’Dilip’,3000,’AP’),

(2,’Rahul’,5000,’DH’),

(3,’Mohan’,7000,’KR’),

(4,’Dilip’,3000,’AP’),

(5,’Rahul’,5000,’DH’),

(6,’Mohan’,7000,’KR’),

(5,’Dilip’,3000,’AP’),

(6,’Rahul’,5000,’DH’)

Then,

Output clause

/* Local System*/

Updating

update Emp Set sal = sal +1

output inserted.empId,deleted.sal as [old sal], inserted.sal as [new sal]

where class =2

/* table Variable for server side maintenance*/

declare @change table

(

id int not null,

[old sal] money,

[new sal] money

)

update Emp Set sal = sal +1

output inserted.empId,deleted.sal as [old sal], inserted.sal as [new sal] into @change

where class =2

select * from @change

Deleting

delete Emp

output deleted.*

where empId=3 or empId = 6

Inserting

insert into Emp

output inserted.*

values

(13,’Dilip’,332.93,’JK’),

(14,’rakesh’,312.93,’GJ’)

For Updating,deleting and inserting we get a result set.

Update 21 Jan 2013

One of my reader Mini asked me that she is trying to insert multiple values into table using row constructor but it is giving error in the comma in the syntax ?

Ans:

1)  Result set are not supported in sql server expressed Edition

2) Inserting multiple rows or technically can be called as Row Constructors are new feature added in SQL Server 2008

if we try to execute the above code on SQL Server 2005 we will get an error at comma stating that it is not allowed, because this syntax is introduced in SQL Server 2008 and even when we are working on client server environment no matter what SQL Server you have if the server to whom you are connecting is previous versions of SQL Server 2008 it will not execute, so these things should kept into consideration.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com)

We have a table with a CHARACTER data type field. We apply a “>” row comparison between this field and another CHARACTER field in another table. What will be the results for records with field value of NULL

This is something  so many asked me because I also faced the same situation before, so having a NULL in a row when compared will give an UNKNOWN result.

The result will not be correct hence creates an ambiguity.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com)

SQL SERVER – CREATE INDEX myIndex ON myTable(myColumn)What type of Index will get created after executing the above statement

Non-clustered index. Important thing to note: By default a clustered index gets created on the primary key, unless specified otherwise.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )

SQL SERVER – what are the DBCC commands that are commonly use for database maintenance

DBCC CHECKDB, DBCC CHECKTABLE, DBCC CHECKCATALOG, DBCC CHECKALLOC, DBCC SHOWCONTIG, DBCC SHRINKDATABASE, DBCC SHRINKFILE etc. But there are a whole load of DBCC commands which are very useful for DBAs. Check out SQL Server books online for more information.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )

SQL SERVER – What is the difference between group by and order by

Group by controls the presentation of the rows, order by controls the presentation of the columns  for the results of the SELECT statement.

EX 1 : Group By helps us display any aggregate of any column based on a field what has repeated names.

use AdventureWorksDW2008R2

go

select Title,SUM(BaseRate) as Rate  from dbo.DimEmployee group by Title

Output:

EX 2 : Order By clause helps us display any table based on the values present on that particular column.

use AdventureWorksDW2008R2

go

select FirstName,Title  from dbo.DimEmployee order by FirstName

the difference is how we choose and what is the reuirement.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )

SQL SEREVER – What is a View and An Indexed View

 A view  is a virtual tables that is made up of one or more tables. It can be used for security purpose, hiding complex queries.

An Indexed view is a view that has unique clustered index created on it. This takes physical storage i.e it stores data. It will be used in OLAP, decision support where inserts/updates are low. It can be used in the following scenarios

1) Joins and aggregation of big tables.

2) Repeated joins of the same tables on the same keys.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )

SQL SERVER 2008 – What is the difference betweeen Execute and sp_executesql

sp_executesql and Execute statement are used to execute a sql stored Procedure. The execute statement doesn’t support parameter substitution in the executed string.

According to Sql server Book Online Link Here.

‘To execute a string, we recommend that you use the sp_executesql stored procedure instead of the EXECUTE statement. Because this stored procedure supports parameter substitution, sp_executesql is more versatile than EXECUTE; and because sp_executesql generates execution plans that are more likely to be reused by SQL Server, sp_executesqlis more efficient than EXECUTE.’

So the best thing to use is sp_executesql but depends according to the requirement.

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )