SQlPractice1

Wednesday, 31 July 2013

Derived column in Sql Server:
CREATE TABLE Students(ID INT,
NAME VARCHAR(33),
M1 INT,
M2 INT,
M3 INT, 
TOTAL AS (M1+M2+M3) )--This is derived column
----- Insert data 
INSERT INTO Students values(1,'raju',67,78,85)
INSERT INTO Students values(2,'Kiran',77,58,55)
INSERT INTO Students values(3,'Ramu',87,78,45)
INSERT INTO Students values(4,'Rani',57,77,49)

SELECT * FROM Students
---Updations done on Derived Column
   UPDATE Students SET M1=0,M2=100,M3=99 WHERE ID=1



About (!= and <>)
Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.
Here is the follow up question I received right I answer that there is no difference between those operator.
If != and <> both are the same, which one should be used in SQL queries?
Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.
Though, many of the leading database applications supports both of the operators. For example -
  • SQL Server
  • MySQL
  • Oracle
  • SQLite
  • Sybase
  • IBM Informix
  • PostgreSQL
Here is my return question to you which one of the following operators you use for NOT EQUAL TO operation?

  1. !=
  2. <>      

Magic of the OUTPUT Clause with INSERT, UPDATE, DELETE


SQL Server 2005 has new OUTPUT clause, which is quite useful. OUTPUT clause has accesses to inserted and deleted tables (virtual tables) just like triggers. OUTPUT clause can be used to return values to client clause. OUTPUT clause can be used with INSERT, UPDATE, or DELETE to identify the actual rows affected by these statements.
OUTPUT clause can generate table variable, a permanent table, or temporary table. Even though, @@Identity will still work in SQL Server 2005, however I find OUTPUT clause very easy and powerful to use. Let us understand OUTPUT clause using example.

Example 1 : OUTPUT clause into Table with INSERT statement


USE AdventureWorks;
GO
--------Creating the table which will store permanent table
CREATE TABLE TestTable (ID INTTEXTVal VARCHAR(100))
----Creating temp table to store ovalues of OUTPUT clause
DECLARE @TmpTable TABLE (ID INTTEXTVal VARCHAR(100))
----Insert values in real table as well use OUTPUT clause to insert
----values in the temp table.
INSERT TestTable (IDTEXTVal)
OUTPUT Inserted.IDInserted.TEXTVal INTO @TmpTable
VALUES (1,'FirstVal')
INSERT TestTable (IDTEXTVal)
OUTPUT Inserted.IDInserted.TEXTVal INTO @TmpTable
VALUES (2,'SecondVal')
----Check the values in the temp table and real table
----The values in both the tables will be same
SELECT FROM @TmpTable
SELECT FROM TestTable
----Clean up time
DROP TABLE TestTable
GO

ResultSet 1:
ID TextVal
——————— ————————
1 FirstVal
2 SecondVal

ID TextVal
——————— ———————
1 FirstVal
2 SecondVal



Example 2 : OUTPUT clause with INSERT statement

USE AdventureWorks;
GO
----Creating the table which will store permanent table
CREATE TABLE TestTable (ID INTTEXTVal VARCHAR(100))
----Insert values in real table as well use OUTPUT clause to insert
----values in the temp table.
INSERT TestTable (IDTEXTVal)
OUTPUT Inserted.IDInserted.TEXTVal
VALUES (1,'FirstVal')
INSERT TestTable (IDTEXTVal)
OUTPUT Inserted.IDInserted.TEXTVal
VALUES (2,'SecondVal')
----Clean up time
DROP TABLE TestTable
GO

ResultSet 2:
ID TextVal
——————— ———————
1 FirstVal
 

(1 row(s) affected)
ID TextVal
——————— ———————
2 SecondVal



Example 3 : OUTPUT clause into Table with UPDATE statement

USE AdventureWorks;
GO
----Creating the table which will store permanent table
CREATE TABLE TestTable (ID INTTEXTVal VARCHAR(100))
----Creating temp table to store ovalues of OUTPUT clause
DECLARE @TmpTable TABLE (ID_New INTTEXTVal_New VARCHAR(100),ID_Old INT,TEXTVal_Old VARCHAR(100))
----Insert values in real table
INSERT TestTable (IDTEXTVal)
VALUES (1,'FirstVal')
INSERT TestTable (IDTEXTVal)
VALUES (2,'SecondVal')
----Update the table and insert values in temp table using Output clause
UPDATE TestTable
SET TEXTVal 'NewValue'
OUTPUT Inserted.IDInserted.TEXTValDeleted.IDDeleted.TEXTVal INTO @TmpTable
WHERE ID IN (1,2)
----Check the values in the temp table and real table
----The values in both the tables will be same
SELECT FROM @TmpTable
SELECT FROM TestTable
----Clean up time
DROP TABLE TestTable
GO

ResultSet 3:
ID_New TextVal_New ID_Old TextVal_Old
——————— ——————— ——————— ———————
1 NewValue 1 FirstVal
2 NewValue 2 SecondVal

ID TextVal
——————— ———————
1 NewValue
2 NewValue



Example 4 : OUTPUT clause into Table with DELETE statement

USE AdventureWorks;
GO
----Creating the table which will store permanent table
CREATE TABLE TestTable (ID INTTEXTVal VARCHAR(100))
----Creating temp table to store ovalues of OUTPUT clause
DECLARE @TmpTable TABLE (ID INTTEXTVal VARCHAR(100))
----Insert values in real table
INSERT TestTable (IDTEXTVal)
VALUES (1,'FirstVal')
INSERT TestTable (IDTEXTVal)
VALUES (2,'SecondVal')
----Update the table and insert values in temp table using Output clause
DELETE
FROM 
TestTable
OUTPUT Deleted.ID
Deleted.TEXTVal INTO @TmpTable
WHERE ID IN (1,2)
----Check the values in the temp table and real table
----The values in both the tables will be same
SELECT FROM @TmpTable
SELECT FROM TestTable
----Clean up time
DROP TABLE TestTable
GO

ResultSet 4:
ID TextVal
——————— ———————
1 FirstVal
2 SecondVal

ID TextVal
——————— ———————

If you run all the above four example, you will find that OUTPUT clause is very useful.