Wednesday, April 8, 2009

SQL QUERY

1. 2 Tables.
Table 1 :- Employee
empid
empname
salary
mgrid
Table 2 :- Phone
empid
phnumber

2. Select all employees who doesn't have phone?

SELECT empname FROM Employee
WHERE
(empid NOT IN
(SELECT DISTINCT empid FROM phone))

3. Select the employee names who is having more than one phone numbers.

SELECT empname FROM employee
WHERE
(empid IN
(SELECT empid FROM phone
GROUP BY empid
HAVING COUNT(empid) > 1))

4. Select the details of 3 max salaried employees from employee table.

SELECT TOP 3 empid, salary FROM employee
ORDER BY salary DESC

5. Display all managers from the table. (manager id is same as emp id)

SELECT empname FROM employee
WHERE (empid IN
(SELECT DISTINCT mgrid FROM employee))

6. Write a Select statement to list the Employee Name, Manager Name
under a particular manager?

SELECT e1.empname AS EmpName, e2.empname AS ManagerName
FROM Employee e1
INNER JOIN
Employee e2 ON e1.mgrid = e2.empid
ORDER BY e2.mgrid

7. 2 tables emp and phone.
emp fields are - empid, name
Ph fields are - empid, ph (office, mobile, home). Select all
employees who doesn't have any ph nos.

SELECT * FROM employee LEFT OUTER JOIN
phone ON employee.empid = phone.empid
WHERE (phone.office IS NULL OR phone.office = ' ')
AND (phone.mobile IS NULL OR phone.mobile = ' ')
AND (phone.home IS NULL OR phone.home = ' ')

8. Find employee who is living in more than one city.
Two Tables:
Emp City
Empid
empName
Salary
Empid
City
SELECT empname, fname, lname FROM employee
WHERE (empid IN
(SELECT empid
FROM city
GROUP BY empid
HAVING COUNT(empid) > 1))

10.Find all employees who is living in the same city. (table is same as above)

SELECT fname FROM employee
WHERE (empid IN
(SELECT empid
FROM city a
WHERE city IN
(SELECT city
FROM city b
GROUP BY city
HAVING COUNT(city) > 1)))

11.There is a table named MovieTable with three columns - moviename,
person and role. Write a query which gets the movie details where Mr.
Amitabh and Mr. Vinod acted and their role is actor.

SELECT DISTINCT m1.moviename
FROM MovieTable m1 INNER JOIN
MovieTable m2 ON m1.moviename = m2.moviename
WHERE (m1.person = 'amitabh' AND m2.person = 'vinod' OR
m2.person = 'amitabh' AND m1.person = 'vinod') AND (m1.role =
'actor') AND (m2.role = 'actor')
ORDER BY m1.moviename

12.There are two employee tables named emp1 and emp2. Both contains same
structure (salary details). But Emp2 salary details are incorrect and
emp1 salary details are correct. So, write a query which corrects
salary details of the table emp2

update a set a.sal=b.sal from emp1 a, emp2 b where a.empid=b.empid

13.Given a Table named “Students” which contains studentid, subjectid
and marks. Where there are 10 subjects and 50 students. Write a Query
to find out the Maximum marks obtained in each subject.

14.In this same tables now write a SQL Query to get the studentid also
to combine with previous results.

15.Three tables – student , course, marks – how do go at finding name of
the students who got max marks in the diff courses.

SELECT student.name, course.name AS coursename, marks.sid, marks.mark
FROM marks INNER JOIN
student ON marks.sid = student.sid INNER JOIN
course ON marks.cid = course.cid
WHERE (marks.mark =
(SELECT MAX(Mark)
FROM Marks MaxMark
WHERE MaxMark.cID = Marks.cID))

16.There is a table day_temp which has three columns dayid, day and
temperature. How do I write a query to get the difference of
temperature among each other for seven days of a week?

SELECT a.dayid, a.dday, a.tempe, a.tempe - b.tempe AS Difference
FROM day_temp a INNER JOIN
day_temp b ON a.dayid = b.dayid + 1

OR

Select a.day, a.degree-b.degree from temperature a, temperature b
where a.id=b.id+1

17.There is a table which contains the names like this. a1, a2, a3, a3,
a4, a1, a1, a2 and their salaries. Write a query to get grand total
salary, and total salaries of individual employees in one query.

SELECT empid, SUM(salary) AS salary
FROM employee
GROUP BY empid WITH ROLLUP
ORDER BY empid

18. How to know how many tables contains empno as a column in a database?

SELECT COUNT(*) AS Counter
FROM syscolumns
WHERE (name = 'empno')

19. Find duplicate rows in a table? OR I have a table with one column
which has many records which are not distinct. I need to find the
distinct values from that column and number of times it’s repeated.

SELECT sid, mark, COUNT(*) AS Counter
FROM marks
GROUP BY sid, mark
HAVING (COUNT(*) > 1)

20. How to delete the rows which are duplicate (don’t delete both
duplicate records).

SET ROWCOUNT 1
DELETE yourtable
FROM yourtable a
WHERE (SELECT COUNT(*) FROM yourtable b WHERE b.name1 = a.name1 AND
b.age1 = a.age1) > 1
WHILE @@rowcount > 0
DELETE yourtable
FROM yourtable a
WHERE (SELECT COUNT(*) FROM yourtable b WHERE b.name1 = a.name1 AND
b.age1 = a.age1) > 1
SET ROWCOUNT 0


21. How to find 6th highest salary

SELECT TOP 1 salary
FROM (SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary

22. Find top salary among two tables

SELECT TOP 1 sal
FROM (SELECT MAX(sal) AS sal
FROM sal1
UNION
SELECT MAX(sal) AS sal
FROM sal2) a
ORDER BY sal DESC

23. Write a query to convert all the letters in a word to upper case

SELECT UPPER('test')

24. Write a query to round up the values of a number. For example even if
the user enters 7.1 it should be rounded up to 8.

SELECT CEILING (7.1)

25. Write a SQL Query to find first day of month?

SELECT DATENAME(dw, DATEADD(dd, - DATEPART(dd, GETDATE()) + 1,
GETDATE())) AS FirstDay

Datepart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms

26.Table A contains column1 which is primary key and has 2 values (1, 2)
and Table B contains column1 which is primary key and has 2 values
(2, 3). Write a query which returns the values that are not common
for the tables and the query should return one column with 2 records.

SELECT tbla.a
FROM tbla, tblb
WHERE tbla.a <>
(SELECT tblb.a
FROM tbla, tblb
WHERE tbla.a = tblb.a)
UNION
SELECT tblb.a
FROM tbla, tblb
WHERE tblb.a <>
(SELECT tbla.a
FROM tbla, tblb
WHERE tbla.a = tblb.a)
OR (better approach)
SELECT a
FROM tbla
WHERE a NOT IN
(SELECT a
FROM tblb)
UNION ALL
SELECT a
FROM tblb
WHERE a NOT IN
(SELECT a
FROM tbla)

27.There are 3 tables Titles, Authors and Title-Authors (check PUBS db).
Write the query to get the author name and the number of books
written by that author, the result should start from the author who
has written the maximum number of books and end with the author who
has written the minimum number of books.

SELECT authors.au_lname, COUNT(*) AS BooksCount
FROM authors INNER JOIN
titleauthor ON authors.au_id = titleauthor.au_id INNER JOIN
titles ON titles.title_id = titleauthor.title_id
GROUP BY authors.au_lname
ORDER BY BooksCount DESC

28.

UPDATE emp_master
SET emp_sal =
CASE
WHEN emp_sal > 0 AND emp_sal <= 20000 THEN (emp_sal * 1.01)
WHEN emp_sal > 20000 THEN (emp_sal * 1.02)
END

29.List all products with total quantity ordered, if quantity ordered is
null show it as 0.

SELECT name, CASE WHEN SUM(qty) IS NULL THEN 0 WHEN SUM(qty) > 0 THEN
SUM(qty) END AS tot
FROM [order] RIGHT OUTER JOIN
product ON [order].prodid = product.prodid
GROUP BY name
Result:
coke 60
mirinda 0
pepsi 10

30.ANY, SOME, or ALL?

ALL means greater than every value--in other words, greater than the
maximum value. For example, >ALL (1, 2, 3) means greater than 3.

ANY means greater than at least one value, that is, greater than the
minimum. For example >ANY (1, 2, 3) means greater than 1.

SOME is an SQL-92
standard equivalent for ANY.

31. Count how many table in current database.

select count(*) from sysobjects where type='U'

32. Query queries for the objects in the user databases in both SQL Server 2000 and 2005
For finding columns name in tables PK.FK and other constraints.


ID

Object Type

SQL Server 2000

SQL Server 2005

1

Data Models

Table = dtproperties

SELECT *
FROM dbo.dtproperties
GO

Table = dbo.sysdiagrams

SELECT *
FROM dbo.sysdiagrams;
GO

2

Tables

Table = sysobjects

SELECT *
FROM dbo.sysobjects
WHERE xtype = 'u'
ORDER BY Name
GO

Table = sys.tables

SELECT *
FROM sys.tables
ORDER BY Name;
GO

3

Columns

Table = syscolumns

SELECT o.name, c.name
FROM dbo.syscolumns c
INNER JOIN dbo.sysobjects o
ON c.id = o.id
WHERE o.name = 'MyTableName'
ORDER BY c.colorder
GO

Table = sys.all_columns

SELECT OBJECT_NAME([Object_ID]) AS 'TableName', [Name] AS 'ColumnName', Column_ID
FROM sys.all_columns
ORDER BY TableName, Column_ID;
GO

4

Primary Keys

Table = sysobjects

SELECT p.name, OBJECT_NAME(parent_obj) AS 'Table Name'
FROM dbo.sysobjects p
WHERE p.xtype = 'PK'
ORDER BY p.Name
GO

Table = sys.objects

SELECT OBJECT_NAME(o.parent_object_id) AS 'ParentObject', s.name AS 'Schema', o.Name AS 'PrimaryKey'
FROM sys.objects o
INNER JOIN sys.schemas s
ON o.schema_id = s.schema_id
WHERE o.Type = 'PK'
ORDER BY o.Name;
GO

5

Foreign Keys

Table = sysforeignkeys

SELECT OBJECT_NAME(f.constid) AS 'ForeignKey', OBJECT_NAME(f.fkeyid) AS 'FKTable', c1.[name] AS 'FKColumnName', OBJECT_NAME(f.rkeyid) AS 'PKTable', c2.[name] AS 'PKColumnName'
FROM sysforeignkeys f
INNER JOIN syscolumns c1
ON f.fkeyid = c1.[id]
AND f.fkey = c1.colid
INNER JOIN syscolumns c2
ON f.rkeyid = c2.[id]
AND f.rkey = c2.colid
ORDER BY OBJECT_NAME(f.rkeyid)
GO

Table = sys.foreign_key_columns

SELECT OBJECT_NAME(f.constraint_object_id) AS 'ForeignKey', OBJECT_NAME(f.parent_object_id) AS 'FKTable', c1.[name] AS 'FKColumnName', OBJECT_NAME(f.referenced_object_id) AS 'PKTable', c2.[name] AS 'PKColumnName'
FROM sys.foreign_key_columns f
INNER JOIN sys.all_columns c1
ON f.parent_object_id = c1.[object_id]
AND f.parent_column_id = c1.column_id
INNER JOIN sys.all_columns c2
ON f.referenced_object_id = c2.[object_id]
AND f.referenced_column_id = c2.column_id ORDER BY OBJECT_NAME(f.referenced_object_id);
GO

6

Constraints

Table = sysconstraints

SELECT o.[name] AS 'DefaultName', OBJECT_NAME(c.[id]) AS 'TableName', col.[name] AS 'ColumnName'
FROM dbo.sysconstraints c
INNER JOIN dbo.sysobjects o
ON c.constid = o.[id]
INNER JOIN dbo.syscolumns col
ON col.[id] = c.colid
ORDER BY o.[name]
GO

Table = sys.objects

SELECT OBJECT_NAME(o.parent_object_id) AS 'ParentObject', s.name AS 'Schema', o.Name AS 'PrimaryKey'
FROM sys.objects o
INNER JOIN sys.schemas s
ON o.schema_id = s.schema_id
WHERE o.Type IN ('C', 'D', 'UQ')
ORDER BY o.Name;
GO

7

FileGroups\Partitions

Table = sysfilegroups

SELECT *
FROM sysfilegroups
GO

Table = sys.data_spaces

SELECT *
FROM sys.data_spaces;
GO

8

Stored Procedures

Table = sysobjects

SELECT o.[name], o.[id], o.xtype, c.[text]
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c
ON o.[id] = c.[id]
WHERE o.xtype = 'p'
ORDER BY o.[Name]
GO

Table = sys.objects

SELECT o.[Name], o.[object_id], o.[type], m.definition
FROM sys.objects o
INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id
WHERE o.[type] = 'p'
ORDER BY o.[Name];
GO

9

Functions

Table = sysobjects

SELECT o.[name], o.[id], o.xtype, c.[text]
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c
ON o.[id] = c.[id]
WHERE o.xtype IN ('fn', 'if', 'tf')
ORDER BY o.[Name]
GO

Table = sys.objects

SELECT o.[Name], o.[object_id], o.[type], m.definition
FROM sys.objects o
INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id
WHERE o.[type] IN ('fn', 'fs', 'ft', 'if', 'tf')ORDER BY o.[Name];
GO

10

Views

Table = sysobjects

SELECT o.[name], o.[id], o.xtype, c.[text]
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c
ON o.[id] = c.[id]
WHERE o.xtype = 'v'
ORDER BY o.[Name]
GO

Table = sys.objects

SELECT o.[Name], o.[object_id], o.[type], m.definition
FROM sys.objects o
INNER JOIN sys.sql_modules m
ON o.object_id = m.object_id
WHERE o.[type] = 'V'
ORDER BY o.[Name];
GO


33.Dispaly detailed records instead of its ID
we have a table tab1 which include all the ids
create table tab1( srno varchar(50), empid varchar(10),depid varchar(10),addressID varchar(10))

and table tab1employeentab1dep,tab1address show details of these ids.

create table tab1employee( empid varchar(10), empname varchar(50))
create table tab1dep( depid varchar(10), depname varchar(50))
create table tab1address( addressID varchar(10), address varchar(50))

so we have to write a query for showing all detailed info on ids in tab1 without using
subquery.




insert into tab1 values('1','1','1','1')
insert into tab1 values('2','2','2','2')
insert into tab1 values('3','3','3','3')

insert into tab1address values( '1','address1')
insert into tab1address values( '2','address2')
insert into tab1address values( '3','address3')




select tab1employee.empname,tab1dep.depname, tab1address.address --*
from tab1
inner join tab1employee
on tab1.empid = tab1employee.empid
join tab1dep
on tab1dep.depid= tab1.depid
join tab1address
on tab1address.addressID = tab1.addressID

34. How to add foreign key constraints on existing table


Create table AdminInfo( Adminid nvarchar(50), AdminRole int, UserName nvarchar(50),
UserPassword nvarchar(50))

Create table AdminRoleMaster
(ID int primary key, description nvarchar(50), Active bit default(1))

alter table AdminInfo
add
foreign key(AdminRole)
references AdminRoleMaster(ID)

How to add unique key constraint in existing table

ALTER TABLE table_name ADD CONSTRAINT constraint_name (columne_name)

Select statement using UDF,CASE, JOINS.

declare @membership varchar(50)
set @membership='A2B8D5E9-2F8A-4EE2-AFAC-6D92CCDB7B33'
select videosToVote.studioname, videosToVote.videosname, COALESCE(
cast( dbo.UDF_CalRating(VedioRating.videosid)*100 as varchar),'Not rated yet')[Percentage],case when membership=@membership then 'you can''t vote' else 'you can vote' end status from VedioRating

right outer join

videosToVote

on

videosToVote.videosID = VedioRating.videosID



group by VedioRating.videosid,videosToVote.videosname,videosToVote.studioname,VedioRating.membership







Thanks
HelponDesk Team

Monday, March 30, 2009

SQL SCRIPT BANK

Hi this link to Pinal dev(MVP) 's scirpt bank.

SQL SCRIPT BANK




CREATE PROCEDURE [dbo].[tESTrEP]

AS
BEGIN

select
Jobrequest.jobid,
Jobrequest.jobTitle,
jobrequest.jobstatusid,
CONVERT(VARCHAR(11),RequestDate,103)[RequestDate],
count(jobrequest.jobTitle)[ToBeHire]
from candidate,jobrequest,Department,JobApplication
where
jobrequest.jobid=jobapplication.jobId and
jobapplication.candidateId=candidate.candidateId and
Department.DepartmentId=Jobrequest.DepartmentId and
Candidate.candidatestatustypeid = 5

group by(jobrequest.jobTitle),Jobrequest.jobid,jobrequest.jobstatusid,
CONVERT(VARCHAR(11),RequestDate,103)
compute sum(count(jobrequest.jobTitle))
END
Hi this is very clear difference
@@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT

I Copied it from following link
blog.sqlauthority.com/

SELECT @@IDENTITY
It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.
SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.

SELECT IDENT_CURRENT(’tablename’)
It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.

To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.

Thursday, March 19, 2009

some sql server points

Question: what is cascade and restrict in drop table sql
Answerr:The CASCADE and RESTRICT keywords specify the action to be taken if other objects exist that are
dependent on the object being dropped. If CASCADE is specified, such objects will be dropped as well. If
RESTRICT is specified, an error is returned if other objects are affected, and no objects are dropped.
Question: How to import table using "INSERT" statement.
Answer : INSERT INTO new_table (Foo, Bar, Fizz, Buzz)SELECT Foo, Bar, Fizz, Buzz FROM source_table --
optionally WHERE ...
Question : what are DML,DDL,DCL,TCL Commands.
Answer:
DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert and
update data in database.
Examples: SELECT, UPDATE, INSERT statements
DDL
DDL is abbreviation of Data Definition Language. It is used to create and modify the structure of database
objects in database.
Examples: CREATE, ALTER, DROP statements
DCL
DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and referential
integrity as well it is used to control access to database by securing it.
Examples: GRANT, REVOKE statements
TCL
TCL is abbreviation of Transactional Control Language. It is used to manage different transactions occurring
within a database.
Examples: COMMIT, ROLLBACK statements
question: What is Ties in sql statement.
Answer:
TOP (expression) [PERCENT] [ WITH TIES ]
PERCENT Indicates that the query returns only the first expression percent of rows from the result set.
WITH TIES Specifies that additional rows be returned from the base result set with the same value in the ORDER BY columns
appearing as the last of the TOP n (PERCENT) rows. TOP...WITH TIES can be specified only in SELECT statements,
and only if an ORDER BY clause is specified.
The following example obtains the top 10 percent of all employees with the highest salary and returns them in
descending order according to salary base rate. Specifying WITH TIES makes sure that any employees that have
salaries equal to the lowest salary returned are also included in the result set, even if doing this exceeds 10
percent of employees.
Copy CodeUSE AdventureWorks;GOSELECT TOP(10) PERCENT WITH TIESc.FirstName, c.LastName, e.Title, e.Gender, r.RateFROM Person.Contact AS c INNER JOIN HumanResources.Employee AS e ON c.ContactID = e.ContactID INNER JOIN HumanResources.EmployeePayHistory AS r ON r.EmployeeID = e.EmployeeIDORDER BY Rate DESC;
Thanks
HELPONDESK TEAM

Wednesday, March 18, 2009

some important points for always remmber.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT @@IDENTITY
It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.
SELECT SCOPE_IDENTITY()
It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.
SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.
SELECT IDENT_CURRENT(’tablename’)
It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.
To avoid the potential problems associated with adding a trigger later on, always use SCOPE_IDENTITY() to return the identity of the recently added row in your T SQL Statement or Stored Procedure.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Refresh your page after every 6 second:


[meta http-equiv="refresh" content="6"]
[meta http-equiv="refresh" content="10;URL=Default2.aspx" /]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Find control in master page:: Master.FindControl("lbl")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Load master page dynamicaly::
protected void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MasterPage2.master";
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Why only one runat server form only one time in pae

in asp.net page::
1. The form is always submitted to the page itself. If you specify an action attribute, it is ignored. If you omit the method attribute, it will be set to method="post" by default. Also, if you do not specify the name and id attributes, they are automatically assigned by ASP.NET.
If you select view source in an .aspx page containing a form with no name, method, action, or id attribute specified, you will see that ASP.NET has added these attributes to the form. It looks something like this:

...some code


2.All server controls must appear within a

tag, and the tag must contain the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. Eg. If we put a button inside a form tag without runat="server" then its click event give an
Error:'Button' must be placed inside a form tag with runat=server
3. But one can create more then one form tag with runat="server". Using the visible property to set visible one at a time.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Differece Between Cast And Convert::
1. The difference is CAST is more ANSI standard and use this only if you are writing a program which you will need to use in many database platforms. Where CONVERT is Microsoft specific and more powerful.
2. Convert can used to format datatype but Cast can not do so.
SELECT GETDATE() AS UnconvertedDateTime, CAST(GETDATE() AS nvarchar(30)) AS UsingCast, CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601
3. Cast can be used in the like command.
SELECT p.FirstName, p.LastName, s.SalesYTD, s.SalesPersonID FROM Person.Contact p JOIN Sales.SalesPerson s ON p.ContactID = s.SalesPersonID WHERE CAST(CAST(s.SalesYTD AS int) AS char(20)) LIKE '2%';
4. The CAST function is much better at preserving the decimal places when converting decimal and numeric data types. Most of developers and DBA’s that I have come across prefer to us the CAST function although CONVERT tends to be a bit for useful since it allows you to format the output.

Thanks

Helpondesk Team

Saturday, March 14, 2009

Virtual method call at runtime

The virtual mechanism (as you

correctly depicted as being implemented through a V-table) ensures

that the virtual method will be called no matter the type of the

reference you're using to call the method. Consider this example:


class A
{
public void M()
{
Console.WriteLine("A.M() being called");
}

public virtual void N()
{
Console.WriteLine("A.N() being called");
}
}

class B : A
{
public new void M()
{
Console.WriteLine("B.M() being called");
}

public override void N()
{
Console.WriteLine("B.N() being called");
}
}


say

A a = new B();

a.M();
a.N();


The results would be

A.M() being called
B.N() being called

because M() is called based on the compile time type (I am declaring

a as being an A) and N() is called based on the runtime type (after

all a is a B). Virtual introduces an method call indirection by

using a pointer to the method instead of the method itself. Because

of that the call is slower. C#, like C++ chose to be flexible in

this area, introducing the virtual keyword and retain the best of

both worlds (speed vs polymorhism) whereas, for instance Java,

considers all instance methods as virtual.