0% found this document useful (0 votes)
34 views

Generics CTE SQL Delegate

Generics allow you to create data structures without committing to a specific data type, providing type safety at compile time without performance loss. They are similar to C++ templates but implemented differently. Common uses include collections like lists and trees where operations like adding/removing items are similar regardless of data type. A delegate encapsulates the memory address of a function and can be used as a type-safe function pointer. When an event is thrown, the framework calls the function the delegate points to. Delegates can combine functions to call together. The common table expression (CTE) stores results like a view within the scope of an executing statement. It can be used instead of temporary tables or variables in stored procedures.

Uploaded by

Srinivasa Sameer
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Generics CTE SQL Delegate

Generics allow you to create data structures without committing to a specific data type, providing type safety at compile time without performance loss. They are similar to C++ templates but implemented differently. Common uses include collections like lists and trees where operations like adding/removing items are similar regardless of data type. A delegate encapsulates the memory address of a function and can be used as a type-safe function pointer. When an event is thrown, the framework calls the function the delegate points to. Delegates can combine functions to call together. The common table expression (CTE) stores results like a view within the scope of an executing statement. It can be used instead of temporary tables or variables in stored procedures.

Uploaded by

Srinivasa Sameer
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

What Are Generics?

Generics allow you to realize type safety at compile time. They allow
you to create a data structure without committing to a specific data
type. When the data structure is used, however, the compiler makes
sure that the types used with it are consistent for type safety. Generics
provide type safety, but without any loss of performance or code bloat.
While they are similar to templates in C++ in this regard, they are very
different in their implementation. Generic classes encapsulate
operations that are not specific to a particular data type. The most
common use for generic classes is with collections like linked lists,
hash tables, stacks, queues, trees and so on where operations such as
adding and removing items from the collection are performed in much
the same way regardless of the type of data being stored.

What are Delegates

A delegate can be defined as a type safe function pointer. It


encapsulates the memory address of a function in your code.
Whenever you create or use an event in code, you are using a
delegate. When the event is thrown, the framework examines the
delegate behind the event and then calls the function that the
delegate points to. Delegates can be combined to form groups of
functions that can be called together.

What are delegates? How you used then in your project?

Delegate means function pointer. There are two types of delegate


1-simple delegate
2-multicast delegate
Ex---Suppose in a project u r using copy icon in two applications. Then
u needs to use same code in two app which based on some conditions.
Here u can use delegate. User defined user control class, which has an
event on focus which is of type one delegate which takes a control as
first object and a user defined class which is derived from event args
so u want to give a user defined event to all the control which is
derived from a control this scenario u cannot handle without delegate
because u want to give all the controls one event to initialize if they
want have that functionality i.e. on focus that means which ever
control u want to give an additional functionality of onfocus u initialize
that delegate and provide that functionality

COMMON TABLE EXPRESSION


The common table expression is one of the new features in Sql server
2005. It can be used instead of temp table or table variables in the
stored procedures in the circumstances. Let's see CTE with some
example queries. Most of the developers while writing the stored
procedures they create the temp tables or table variables. They need
some table to store the temporary results in order to manipulate the
data in the other tables based on this temp result. The temp variables
will be stored on the tempdb and it needs to be deleted in the tempdb
database. The table variable is best when compare with the temp
tables. Because the table variable initially will be there in the memory
for the certain limit of size and if the size increase then it will be moved
to the temp database. However the scope of the table variable is only
up to that program. When compare with table variable the CTE is best.
It just store the result set like normal view.

CTE are one of the beautiful and the most powerful feature of SQL
Server. It is introduced in SQL Server 2005 and I guess is one of the
best things in SQL Server. This not only simplifies most of the complex
and impossible queries in T-SQL, but also provides one medium to deal
with recursive queries. Moreover, I can say it is mainly for recursive
queries. It can be used in replacement of derived tables (sub queries),
temp tables, table variables, inline user-defined functions etc. The
Common Table Expression (CTE) is called as a temporary named
RESULT SET, within the scope of an executing statement that can be
used within a SELECT, INSERT, UPDATE, or DELETE or even in a
CREATE VIEW or MERGE statement.

CTE (Common Table Expression):


The CTE is one of the essential features in the Sql server 2005.It just
store the result as temp result set. It can be access like normal table or
view. This is only up to that scope.

The syntax of the CTE is the following.

WITH name (Alias name of the retrieve result set fields)


AS
( //Write the Sql query here )

SELECT * FROM name


Here the select statement must be very next to the CTE. The name is
mandatory and the argument is an optional. This can be used to give
the alias to the retrieve field of the CTE.

CTE 1: Simple CTE


WITH ProductCTE
AS
(
SELECT ProductID AS [ID],ProductName AS [Name],CategoryID AS
[CID],UnitPrice AS [Price]
FROM Products
)
SELECT * FROM ProductCTE
Here all the product details like ID, name, category ID and Unit Price
will be retrieved and stored as temporary result set in the ProductCTE.
This result set can be retrieved like table or view.
CTE2:Simple CTE with alias
WITH ProductCTE(ID,Name,Category,Price)
AS
(
SELECT ProductID,ProductName,CategoryID,UnitPrice
FROM Products
)
SELECT * FROM ProductCTE
Here there are four fields retrieves from the Products and the alias
name have given in the argument to the CTE result set name. It also
accepts like the following as it is in the normal select query.
WITH ProductCTE
AS
(
SELECT ProductID AS [ID], ProductName AS [Name], CategoryID AS
[CID], UnitPrice AS [Price]
FROM Products
)
SELECT * FROM ProductCTE

CTE 3: CTE joins with normal table


the result set of the CTE can be joined with any table and also can
enforce the relationship with the CTE and other tables.
WITH OrderCustomer
AS
(
SELECT DISTINCT CustomerID FROM Orders
)
SELECT C.CustomerID,C.CompanyName,C.ContactName,C.Address+',
'+C.City AS [Address] FROM Customers C INNER JOIN OrderCustomer
OC ON OC.CustomerID = C.CustomerID

Here the Ordered Customers will be placed in the CTE result set and it
will be joined with the Customers details.

CTE 4: Multiple resultsets in the CTE

WITH MyCTE1
AS
(
SELECT ProductID, SupplierID, CategoryID, UnitPrice, ProductName
FROM Products
),

MyCTE2
AS
(
SELECT DISTINCT ProductID FROM "Order Details"
)
SELECT C1.ProductID,C1.ProductName,C1.SupplierID,C1.CategoryID
FROM MyCTE1 C1 INNER JOIN MyCTE2 C2 ON C1.ProductID =
C2.ProductID

Here, there are two result sets that will be filtered based on the join
condition.

CTE 5: Union statements in the CTE

WITH PartProdCateSale
AS
(
SELECT ProductID FROM Products WHERE CategoryID = (SELECT
CategoryID FROM Categories WHERE CategoryName='Condiments')
UNION ALL
SELECT ProductID FROM Products WHERE CategoryID = (SELECT
CategoryID FROM Categories WHERE CategoryName='Seafood')
)
SELECT OD.ProductID,SUM(OD.UnitPrice*OD.Quantity) AS [Total Sale]
FROM "Order Details" OD INNER JOIN PartProdCateSale PPCS ON
PPCS.ProductID = OD.ProductID
GROUP BY OD.ProductID

Normally when we combine the many result sets we create table and
then insert into that table. But see here, we have combined with the
union all and instead of table, here CTE has used.
CTE 6: CTE with identity column

WITH MyCustomCTE
AS
(
SELECT CustomerID, row_number () OVER (ORDER BY CustomerID)
AS iNo FROM
Customers
)
SELECT * FROM MyCustomCTE

Basics of anonymous methods


Anonymous methods allow us to define a code block where a delegate object is
acceptable. This facility saves us an extra step of creating a delegate for small code
blocks that we want to pass to a delegate. It also removes the cluttering of small
methods in the class code. Let us say, for instance, that we have a string collection
class named MyCollection. This class has a method that retrieves all the items in
the collection that satisfies a user supplied criteria, i.e., the caller decides whether a
particular item in the collection qualifies for being retrieved as part of the return
array from this method. The anonymous methods are defined at the time the
caller is handling the event.

myClass1.MyEvent += delegate (string message)


{
Console.WriteLine ("your message is: {0}", message);
};

A lambda expression is an anonymous function that can contain expressions and


statements, and can be used to create delegates or expression tree types. All
lambda expressions use the lambda operator =>, which is read as "goes to". The left
side of the lambda operator specifies the input parameters (if any) and the right side
hold the expression or statement block. The lambda expression x => x * x is read "x
goes to x times x." This expression can be assigned to a delegate type as follows.

Delegate int Del (int I);


Static void Main (string [] args)
{
Del myDelegate = x => x * x;
Int j = myDelegate (5); //j = 25
}

Lambda expression is an inline delegate introduced with C # 3.0 language. It’s a


concise way to represent an anonymous method. It provides a syntax to create and
invoke functions. Although Lambda expressions are simpler to use than anonymous
methods, they do slightly differ on how they are implemented. Both anonymous
methods and Lambda expressions allow you define the method implementation
inline, however, an anonymous method explicitly requires you to define the
parameter types and the return type for a method. Lambda expression uses the type
inference feature of C# 3.0 which allows the compiler to infer the type of the variable
based on the context.

Lambda expression can be broken down into parameters followed by execution code.

Parameter => executioncode.


Anonymous methods allow the developer to specify the code right where the delegate
would normally get passed. Instead of creating the method, he may specify the filtering
code right where the delegate would normally be passed.

You might also like