1.

CTRL + “.”: This is actually a shortcut for a shortcut. it is the same as CTRL + SHIFT + F10 which opens the smart tag window and allows you to add Using statementsAdd Using Statement

Or implement interfaces Implement Interface

and much more.
2.

ALT + CTRL + “e”: This one will open the Exceptions window, which allows you to tell Visual Studio to halt at specific exceptions, halt on all exceptions or selecet some exceptions to ignore.

Exception Popup Windows
3.

CTRL + “k” + “f” and CRTL + “k” + “d”: these two will format the code in the window to be nicely indented. using “d” will format all the document while using “f” will format only selected text. The formatting is for all types of documents, HTML, Xaml, XML, C#… This one is my favorite.
4.

SHIFT + Del: This one will cut the entire row from the document and past it to the clipboard. No need to select the row, just put the marker there and click SHIFT + Del and it is gone.
5.

CTRL + “k” + “c” and CTRL + “k” + “u”: These two are for commenting selected text (the “c” options) and uncommenting selected text (the “u” option).
6.

ALT + ENTER: this little shortcut will open up the Properties windowProperties Window
7.

CTRL + “k” + “s”: This one opens up the code snippets dialogue within the code

Open Snippets Dialogue

If you want to create code snippets you should read my post about snippy the code snippets editor.
8.

F12: I think you all know this but still F12 is the shortcut for the “Go to definition” command which will take you to the definition of the object your marker is currently on.
9.

F9: Another one i think you all know, clicking on F9 will add a breakpoint to the code line your marker is currently at. Clicking F9 again will remove this breakpoint from that line.
10.

CTRL + ALT + “q”: This one will open the Quick watch window while you debugOpen Quick Watch Window

The following SQL Server T-SQL scripts illustrates the calculation of percent of orders by year and by country on base of all orders. The CONVERT function is used to format the percentage figures in currency format.

USE AdventureWorks2008;

GO

– Calculate percent sql – SQL Server calculate percentage – sql convert percent

SELECT YEAR=YEAR(OrderDate),

Percentage = convert(VARCHAR,convert(MONEY,100.0 * SUM(TotalDue) /

(SELECT SUM(TotalDue) FROM Sales.SalesOrderHeader)),1) + ‘%’

FROM Sales.SalesOrderHeader

GROUP BY YEAR(OrderDate)

ORDER BY YEAR ASC

GO

/*

YEAR Percentage

2001 10.18%

2002 28.34%

2003 38.60%

2004 22.88%

*/

————

USE Northwind;

GO

– SQL calculate percentage – calculate percentage sql

SELECT Country,

Percentage = convert(VARCHAR,convert(MONEY,100.0 * count(* ) /

(SELECT count(* ) FROM Orders)), 1) + ‘%’

FROM Customers c

INNER JOIN Orders o

ON c.CustomerID = o.CustomerID

GROUP BY Country

ORDER BY count(* ) DESC

GO

/* Partial results

Country Percentage

USA 14.70%

Germany 14.70%

Brazil 10.00%

France 9.28%

UK 6.75%

Venezuela 5.54%

Austria 4.82%

*/

Execute the following Microsoft SQL Server T-SQL scripts in Management Studio Query Editor to demonstrate the usage of NULLIF function.

– SELECT NULLIF quick demo

– Counts all, including ListPrice=0.0

SELECT COUNT(ListPrice)

FROM AdventureWorks2008.Production.Product

– 504

– Counts only when ListPrice != 0 – does not count NULLs (ListPrice = 0.0)

SELECT COUNT(NULLIF(ListPrice,0.0))

FROM AdventureWorks2008.Production.Product

– 304

————

– SQL NULLIF: returns NULL when the two operands are equivalent, else the first one

– SQL COALESCE

USE tempdb

GO

CREATE TABLE Budget (

BudgetID TINYINT IDENTITY,

DepartmentID TINYINT,

CurrentYear DECIMAL NULL,

PriorYear DECIMAL NULL);

GO

INSERT Budget VALUES(10,2000000,1800000);

INSERT Budget VALUES(20,NULL,3000000);

INSERT Budget VALUES(30,0,8000000);

INSERT Budget VALUES(70,NULL,3500000);

INSERT Budget VALUES(90,6000000,6500000);

GO

SELECT * FROM Budget

GO

/*

BudgetID DepartmentID CurrentYear PriorYear

1 10 2000000 1800000

2 20 NULL 3000000

3 30 0 8000000

4 70 NULL 3500000

5 90 6000000 6500000

*/

SELECT COALESCE(CurrentYear,PriorYear) AS ‘Yearly Budget’

FROM Budget;

/*

Yearly Budget

2000000

3000000

0

3500000

6000000

*/

– SQL NULLIF changes 0 to NULL

SELECT NULLIF(COALESCE(CurrentYear,PriorYear),0.00) AS ‘Yearly Budget No Zeros’

FROM Budget;

/*

Yearly Budget No Zeros

2000000

3000000

NULL

3500000

6000000

*/

– SQL NULLIF

SELECT AVG(NULLIF(COALESCE(CurrentYear,PriorYear),0.00)) AS ‘Average Yearly Budget’

FROM Budget;

GO

/* Result

Average Yearly Budget

3625000.000000

*/

– Cleanup

DROP TABLE tempdb.dbo.Budget

GO

Execute the following script in Query Editor to demonstrate the use of PATINDEX for complex search. The following search will find all bikes which off-road yet comfortable.

use AdventureWorks2008;

select ProductName=[Name], ProductNumber, [Description], ListPrice

from [Production].[Product] p

join [Production].[ProductModelProductDescriptionCulture] pmpdc

on p.ProductModelID = pmpdc.ProductModelID

join [Production].[ProductDescription] pd

on pmpdc.ProductDescriptionID = pd.ProductDescriptionID

where patindex( ‘%off-road%comfortable%’, pd.[Description]) > 0

Partial results:
ProductName ProductNumber Description ListPrice
Mountain-500 Silver, 40 BK-M18S-40 Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride. 564.99
Mountain-500 Silver, 42 BK-M18S-42 Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride. 564.99
Mountain-500 Silver, 44 BK-M18S-44 Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride. 564.99
Mountain-500 Silver, 48 BK-M18S-48 Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride. 564.99
Mountain-500 Silver, 52 BK-M18S-52 Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride. 564.99
Mountain-500 Black, 40 BK-M18B-40 Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride. 539.99

You can establish an Access database as a linked server to Microsoft SQL Server and query it with OPENQUERY. Here are some setup examples:

EXEC sp_addlinkedserver
@server = ‘Warehouse’,
@provider = ‘Microsoft.Jet.OLEDB.4.0′,
@srvproduct = ‘OLE DB Provider for Jet’,
@datasrc = ‘e:\access\warehouse.mdb’
GO

SQL Server linked server setup to Access 2007 database:

EXEC sp_addlinkedserver
@server = ‘NYCInventory’,
@provider = Microsoft.ACE.OLEDB.12.0′,
@srvproduct = ‘OLE DB Provider for ACE’,
@datasrc = ‘e:\access\nycwarehouse.accdb’
GO

This is how you can query a linked Access database:

SELECT * FROM OPENQUERY(Warehouse, ‘SELECT SKU, Item, Description, Quantity FROM dbo.Inventory’)
GO

source:SQL USA

Oracle functions:

Single-Row Functions:
-Manipulates Data Items
-Accepts arguments and return one value
-Act on each row returned.
-Return one result per row
-May modify the data type
-can be nested.
-Accepts arguments which can be a column or an expression.
Syntax:
Function_name [(arg1,arg2,….)]

Single row functions are as below:
Character functions:
—-Case-manipulation functions
a. LOWER e.g [LOWER (‘QA COURSE’) => qa course]
b. UPPER e.g [UPPER (‘qa course’) =>QA COURSE]
c. INITCAP e.g [INITCAP(‘QA COURSE ’)=>Qa course]

—–Character-manipulation functions
a. CONCAT e.g [CONCAT (‘Hello’,’World’) =>HelloWorld]
b. SUBSTR e.g [SUBSTR (‘HelloWorld’,1,5) =>Hello ]
c. LENGTH e.g [LENGTH (‘HelloWorld’) =>10 ]
d. INSTR e.g [INSTR (‘HelloWorld’,’W’) =>6 ]
e. LPAD e.g [LPAD (salary,10,’*’) =>*****24000]
f. RPAD e.g [RPAD(salary,10,’*’) =>24000*****]
g. TRIM e.g [‘H’ FROM ‘HelloWorld’ =>elloworld]
h. REPLACE
EXAMPLE:
SELECT employee_id,CONCAT(first_name,last_name) NAME,job_id,LENGTH (last_name),INSTR (last_name,’a’) “Contains ‘a’?”
FROM employees
WHERE SUBSTR(job_id,4) =’REP’;

—–Number Functions

a. ROUND (45.926 ,2) =>45.93
b. TRUNC (45.926 ,2) =>45.92
c. MOD (1600,300) =>100

—–Date Functions
SYSDATE is a function that returns
–Date
–Time

-MONTHS_BETWEEN (’01-SEP-95’,’11-JAN-94’) => 19.6774194
-ADD_MONTHS (’11-JAN-94’,6) =>’11-JUL-94’
-NEXT_DAY (’01-SEP-95’,’FRIDAY’) =>’08-SEP-95’
-LAST_DAY (’01-FEB-95’) =>’28-FEB-95’

—-Conversion Functions:
a. Implicit data type conversion.
b. Explicit data type conversion.

Implicit data conversion of those Oracle server can automatically convert.

e.g
VARCHAR2 OR CHAR =>NUMBER
VARCHAR2 OR CHAR=>DATE
NUMBER =>VARCHAR2
DATE =>VARCHAR2

Explicit Data type Conversion occurred when opposite above.
e.g
SELECT last_name,TO_CHAR(hire_date,’fmDD Month YYYY’) as HIREDATE
From empoyees;

General Functions:
–NVL
–NVL2
–NULLIF
–COALESCE

I will discuss details in my next post

How to uninstall MS SQL Server 2005 manually???
Backup all databases, if you have any, before you uninstall the application. Also save a copy of all the data and of all the log files.
Also, be careful when you play around with the Windows registry files, while using Registry Editor. Its probably a good idea to take a backup of your registry.
STEP 1: Make sure you have you have ‘Microsoft SQL Server Setup Support Files component’ installed in the server.
Open ‘Add/Remove Programs’ and check whether Microsoft SQL Server Setup Support Files component is listed there, if it’s there you can proceed to Step 2.
Otherwise, you will have to install it from the MS SQL Server 2005 installation media.
STEP 2: Run “C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\ARPWrapper.exe” /Remove – in the command prompt.
This will evoke the MS SQL Uninstall program and uninstall all the MS SQL server components one by one.
If you experience problems here and you are not able to remove ‘MS SQL Server 2005′ from the Server. The last hope is seeking the help of Window’s registry file.
Start the Window’s Registry Editor and look for the registry key: ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall’
On the left side of the Registry Editor, you will be able to see so many GUIDs, one after another. {26F1A218-3158-4107-B3A6-37FD61CEE969} – This is how a GUID looks like.
Click on each GUID, you will be able to see the details of each GUID on the right pane or side of the Registry Editor. Look out for the GUID that gives out ‘Microsoft SQL Server 2005′ in the ‘Display Name’ details.
For each GUID related to ‘Microsoft SQL Server 2005′, run this command in the command prompt.
start /wait msiexec /x {GUID} /l*v c:\sql_uninstall.log
For eg: start /wait msiexec /x {26F1A218-3158-4107-B3A6-37FD61CEE969} /l*v c:\sql_uninstall.log
(You can copy a GUID name by right clicking on the GUID on the left pane or side of Registry Editor)
c:\sql_uninstall.log – stores the uninstall logs
This should uninstall the Microsoft SQL Server 2005 without much problem.
STEP 3: Now it’s time to uninstall the support components.
Unistall them in the order specified below:
- MSXML 6.0 Parser
- SQLXML4
- Microsoft SQL Server VSS Writer
- Microsoft SQL Server 2005 Backward Compatibility
- Anything else that is related to SQL Server 2005, except for the Microsoft SQL Server Native Client component and for the Microsoft SQL Server Setup Support Files component.
If any of the components fail to uninstall, you will have to seek the help of Registry Editor and uninstall them using the GUID for each component.
a. Start the Registry Editor and locate the registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
b. Click on each GUID, you will be able to see the details of each GUID on the right pane or side of the Registry Editor. Look out for the details of MS SQL support components, listed above.
c. For each GUID, to uninstall, run the command below:
start /wait msiexec /x {GUID} SKIPREDISTPREREQS=1 /l*v c:\sqlredist_uninstall.log
STEP 4: Uninstall the Microsoft SQL Server Native Client component using Add/Remove Programs.
STEP 5: Uninstall the Microsoft SQL Server Setup Support Files component Add/Remove Programs.

source:The Crucial Blog

Joins in Oracle(8i & prior):
Equijoin
Non-equijoin
Outer join
Self Join.

We have to use a join to query a data from more than one table.
Note:To join N tables together ,we need a minimum of n-1 join conditions.

Equijoin:
Equijoin retrieves the data those are exact match of the tables queried.
It is also called simple join or inner join.

Retrieving Records with Equijoins:
SELECT e.employee_id,e.last_name,e.department_id,d.department_id
FROM employees e, departments d
Where e.department_id = d.department_id;

Non-Equijoins:
A non-equijoin is a join condition containing something other than an equality operator.
[we need to create a table JOB_GRADES having fields GRA,LOWEST_SAL,HIGHEST_SAL]
e.g
SELECT e.last_name,e.salary,j.grade_level
FROM employees e,job_grades j
Where e.salary
Between j.lowest_sal and j.highest_sal;

Outer Joins:
We all are known about outer join using (+) operator.
Hence I am not discussing it.Rather I am showing it bit different ways.

LEFT OUTER JOIN:
e.g
SELECT e.last_name,e.department_id,d.department_name
FROM employees e
LEFT OUTER JOIN department d
ON (e.department_id =d.department_id);

This query will retrieves all rows in the EMPLOYEES table, which is the left table even if there is no match in the DEPARTMENTS table.

The same query using (+) operator is as below

SELECT e.last_name,e.department_id,d.department_name
FROM employees e, departments
WHERE e.department_id =d. department_id(+)

The right Outer Join is vice verse. Hence I am not discussing on it.

* Full Outer Join:

Full Outer join is combination of Left Outer join as well as Right outer Join.
e.g
SELECT e.last_name,d.department_name
FROM employees e
FULL OUTER JOIN departments d
on (e.department_id = d.department_id);

The above query retrieves all rows in the EMPLOYEES table ,even if ther is no match in the DEPARTMENTs table as well as retrieves all rows in the DEPARTMENTS table,even if there is no match in the EMPLOYEES table.

Self Join:
Sometimes we need to join a table to itself.
e.g.
Q.find out the managers name of employees

SELECT worker.last_name || ‘works for ’ ||manager.last_name
FROM employees worker,employees manager
WHERE worker.manager_id = manager.employees_id;

o/p
Mourgos works for King

Cross Join:

The CROSS JOIN clause produces the cross-product of two tables.
This is the same as Cartesian product(as an IT people we know what is Cartesian product ery well) between the two tables.
e.g
SELECT last_name ,department_name
FROM employees, departments;

source:Oracle Education

In this post I am trying to discuss with an important object in Oracle i.e View.
What is view:
View logically represents subsets of data from one or more tables. It’s a logical table based on a table or another view. A view contains no data of its own like table but its like a window through which data from table can be viewed or changed. The tables on which a view is based are called base table.The view is stored as a select statement in the data dictionary.

Why Use Views:
-To restrict* data access
-To make complex query easy
-To provide data Independence**
-To represent different views of the same data.
*View restrict access to the data because the view can display selective columns from the table. We can give privilege to the user to access of a particular column using view. Some times It needs to give privilege some certain fields to the user instead of a whole table.
**Views provide data independence for ad hoc users & application programs. One view can be used to retrieve data from several tables.

Types of Views:
There are two classifications of views.Simple & Complex.The basic difference is related to the DML operations. I am depicting a comparative feature below.

Table:

We all know how to create a view .Still I am giving the basic syntax of creating a view.

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW viewname
AS subquery
[(alias[ , alias]….)]
[WITH CHECK OPTION [CONSTRAINT constraintname ]]
[WITH READ ONLY [CONSTRAINT constraintname ]];

-OR REPLACE re-creates the view if it already exists.
-FORCE creates the view regardless of whether or not the base tables exist
-NOFORCE create the view iff(if and only if) the the base table exist.(this is default)
alias specifies names for the expressions selected by the view’s query(The number of aliases must match the number of expressions selected by the view)
-subquery The subquery can contain complex Select syntax.
-WITH CHECK OPTION specifies that only rows accessible to the view can be
inserted or updated.
-constraint is the assigned to the CHECK OPTION constraint.
-WITH READ ONLY ensures that no DML operations can be performed on view.

e.g.
CREATE VIEW salvu80
AS SELECT employee_id ID_NUMBER,last_name NAME,salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;

Retrieving Data from a View

SELECT *
FROM salvu80;

Views in the Data Dictionary:
Once our view has been created, We can query the data dictionary view called USER_VIEWS to see the name of the view and the view definition. The text of the SELECT statement that constitutes .Our view is stored in a LONG column.

Data Access Using Views:
When We access data using a view, the Oracle server performs the following operations:

1. It retrieves the view definition from the data dictionary table USER_VIEWS.
2. It checks access privileges for the view base table.
3. It converts the view query into an equivalent operation on the underlying base table or tables. In other words, data is retrieved from or an update is made to, the base tables.

Modifying a View:
Suppose we have a view named empvu80
Modify the empvu80 view by using CREATE OR REPLACE VIEW clause. Add an alias for each column name.

CREATE OR REPLACE VIEW empvu80
(id_number,name,sal,department_id)
AS SELECT employee_id,first_name || ‘ ‘|| last_name,salary,department_id
FROM employees
WHERE department_id = 80;

Note:
With the OR REPLACE option, a view can be created even if one exist s with this name already, thus replacing the old version of the view for its owner. This means that the view can be altered without dropping, re-creating and regranting object privileges.
when assigning column aliases in the CREATE VIEW clause ,we have to remember that the aliases are listed in the same order as the columns in the subquery.

Creating a complex View:
It is an example to create a complex view that contains group functions to display values from two tables.

CREATE VIEW dept_sum_vu
(name, minsal, maxsal, avgsal)
AS SELECT d.department_name,MIN(e.salary),MAX(e.salary),AVG(e.salary)
FROM employees e,department d
WHERE e.department_id =d.department_id
GROUP BY d.department_name;

Rules for performing DML Operations on a view

-We can perform DML operation on simple views.
-We cannot remove a row if the view contains the following:
-Group functions.
-A GROUP BY clause.
-The Distinct Keyword.
-The pseudocolumn* ROWNUM keyword.

-We Cannot modify data in a view if it contains
-Group functions.
-A GROUP BY clause.
-The Distinct Keyword.
-The pseudocolumn ROWNUM keyword.
-Columns defined by expressions(e.g salary * 12)
-We Cannot add data through a view if the view includes
-Group functions.
-A GROUP BY clause.
-The Distinct Keyword.
-The pseudocolumn ROWNUM keyword.
-NOT NULL columns in the base tables that are not selected by the view.
(We can add data through a view unless it contains any of the items listed above or there are NOT NULL colums without default values in the base table that are not selected by the view. All required values must be present in the view. We have to remember that we are adding values directly into the underlying table through the view )

*[ A pseudo column is an item of data which does not belong in any particular table but which can be treated as if it did.e.g
rowid,versions_xid,versions_operation,versions_startscn,versions_starttime,versions_endscn,versions_endtime ,sysdate,systimestamp,rownum ,ora_rowscn,object_value etc ]

Denying DML Operations:
-We can ensure that no DML operations occur by adding the WITH READ ONLY option to Wer view definition.
– Any attempt to perform a DML on any row in the view results in an Oracle server error.

e.g
CREATE OR REPLACE VIEW empvu10
(employee_number,employee_name,job_title)
AS SELECT employee_id,last_name,job_id
From employees
WHERE department_id = 10
WITH READ ONLY;

ERRORS:
DELETE FROM empvu10
WHERE employee_number = 200;

ERROR at line 1:
ORA – 01752 :cannot delete from view without exactly one key-preserved table

Any attempt to insert a row or modify a row using theview with s read-only constraint results in a Oracle server error:
01733:virtual column not allowed here.

Removing a View:
DROP VIEW viewname;
e.g
DROP VIEW empvu80;

INLINE VIEWS:

-An inline view is a subquery with sn alias (or correlation name) that We can use within a SQL statement.
-A named subquery in the FROM clause of the main query is an example of an inline view.
-An Inline view is not a schema* object.

e.g

SELECT a.last_name,a.salary,a.department_id,b.maxsal
FROM employees a (select department_id,max(salary), maxsal
From employees
GROUP BY department_id ) b
WHERE a.department_id =b.department_id
AND a.salary <b.maxsal;

The above example will display the employees last name, salary ,their department id & the maximum salary of that department as well.

Using the WITH CHECK OPTION Clause
We can ensure that DML operations performed on the view stay within the domain of the view by using the WITH CHECK OPTION clause.

CREATE OR REPLACE VIEW empvu80
AS SELECT *
FROM employees
WHERE department_id =20
WITH CHECK OPTION CONSTARINT empvu20_ck;
-any attempt to change the department number for any row in the view fails because it violates the WITH CHECK OPTION constraint.
e.g
UPDATE empvu20
SET department_id = 10
WHERE employee_id =20;

UPDATE empvu20
*
ERROR at line 1:
ORA – 01402 : view WITH CHECK OPTION where-clause violation

source:Oracle Education.

Press CTRL-PageUp or CTRL-PageDown

Next Page »

Follow

Get every new post delivered to your Inbox.