Showing posts with label SQL Server 2008. Show all posts
Showing posts with label SQL Server 2008. Show all posts

Saturday, February 12, 2011

Find Second Highest Value in a Table Without Sub-Query

Let have a simple table to working-on and some dummy data.

SQL:
CREATE TABLE Course(
    CourseTitle nvarchar(32) NOT NULL,
    CourseCapacity int NOT NULL);

If we want to get second highest value in that table, the first idea may we have is use sub-query (there are many answers that we can find on internet). Some of those solutions:

(1) SQL:
SELECT * FROM Course c1 WHERE (2 - 1) =
    (SELECT COUNT(DISTINCT(CourseCapacity))
            FROM Course c2
            WHERE c2.CourseCapacity > c1.CourseCapacity);

(2) SQL:
SELECT TOP 1 * FROM Course WHERE
    CourseCapacity < (SELECT MAX(CourseCapacity) FROM Course)
    ORDER BY CourseCapacity DESC;

But, how if only used single query?
The first idea that I got is use JOIN. And the possible JOIN type is NATURAL JOIN for this case. But that still not answer the problem. So, I tried to JOIN the same table with condition column in table-1 less than column in table-2.

SQL:
SELECT *
    FROM Course c1, Course c2
    WHERE c1.CourseCapacity < c2.CourseCapacity;

Results:
image
Above result, show to us that capacity of right part is less than left part. And the highest value of right part is the second highest of the table (MPP ~ 13). GOT IT!

So, the next step is modify the previous value with add TOP and ORDER BY clauses. The objective is do ORDER BY the above result according to right part of column CourseCapacity and then select the TOP 1 row.

SQL:
SELECT TOP 1 c1.*
    FROM Course c1, Course c2
    WHERE c1.CourseCapacity < c2.CourseCapacity
    ORDER BY c1.CourseCapacity DESC;

Result:
image

Case closed Smile

Wednesday, February 4, 2009

The Connection String Reference for OLE DB (SQL Server 2008)

SQL Server Native Client 10.0 OLE DB Provider

· Standard security

Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

Are you using SQL Server 2008 Express? Don't miss the server name syntax Servername\SQLEXPRESS where you substitute Servername with the name of the computer where the SQL Server 2008 Express installation resides.

· Trusted connection

Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase; Trusted_Connection=yes;

Equivalent key-value pair: "Integrated Security=SSPI" equals "Trusted_Connection=yes"

· Connecting to an SQL Server instance

The syntax of specifying the server instance in the value of the server key is the same for all connection strings for SQL Server.

Provider=SQLNCLI10;Server=myServerName\theInstanceName;Database=myDataBase; Trusted_Connection=yes;

· Enabling MARS (multiple active result sets)

Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase; Trusted_Connection=yes;MarsConn=yes;

Equivalent key-value pair: "MultipleActiveResultSets=true" equals "MARS_Connection=yes"
Use ADO.NET for MARS functionality. MARS is not supported in ADO.NET 1.0 nor ADO.NET 1.1.

Using MARS with SQL Native Client, by Chris Lee

· Encrypt data sent over network

Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase; Trusted_Connection=yes;Encrypt=yes;

· Attach a database file on connect to a local SQL Server Express instance

Provider=SQLNCLI10;Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname; Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

· Attach a database file, located in the data directory, on connect to a local SQL Server Express instance

Provider=SQLNCLI10;Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;

Why is the Database parameter needed? If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection.

· Database mirroring

If you connect with ADO.NET or the SQL Native Client to a database that is being mirrored, your application can take advantage of the drivers ability to automatically redirect connections when a database mirroring failover occurs. You must specify the initial principal server and database in the connection string and the failover partner server.

Provider=SQLNCLI10;Data Source=myServerAddress;Failover Partner=myMirrorServerAddress;Initial Catalog=myDataBase;Integrated Security=True;

There is ofcourse many other ways to write the connection string using database mirroring, this is just one example pointing out the failover functionality. You can combine this with the other connection strings options available.

.NET Framework Data Provider for OLE DB

· Bridging to SQL Native Client OLE DB

This is just one connection string sample for the wrapping OleDbConnection class that calls the underlying OLEDB provider. See respective OLE DB provider for more connection strings to use with this class.

Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

SQLXML 4.0 OLEDB Provider

· Using SQL Server Native Client provider

Provider=SQLXMLOLEDB.4.0;Data Provider=SQLNCLI10;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

References: http://www.connectionstrings.com/sql-server-2008