Export (0) Print
Expand All

Executing SQL Statements Using HTTP

SQL Server 2000

Microsoft® SQL Server™ 2000 can be accessed directly by queries executed at the URL (if the allow URL queries option was selected when the virtual root was registered). Clients can make requests using HTTP methods GET and POST. For more information about the URL syntax that is support by the SQL ISAPI extension, see URL Access.


In the following examples, nwind is a virtual directory used to access the Northwind database. For more information about creating the nwind virtual directory, see Creating the nwind Virtual Directory.

In the example queries, if a query returns more than one element at the root of the document, the root element can be added by including either of these:

  • A SELECT <ROOT> in the query.

  • Passing a root keyword as a parameter to the query with a value ROOT (this value can be anything).
A. Specify a simple query

The following statement returns all of the customer data in the Customers table in the Northwind database. In this query, the XML mode is set to RAW.


Here is the result set:

<?xml version="1.0" encoding="utf-8" ?> 
   <Customers CustomerID="ALFKI" ContactName="Maria Andears" /> 
   <Customers CustomerID="ANATR" ContactName="Ana Trujillo" /> 
B. Specify a query on multiple tables

In this example, the SELECT statement returns information from the Customers and Orders tables in the Northwind database. The XML mode is set to AUTO.


This is the partial result:

<?xml version="1.0" encoding="utf-8" ?> 
  <Customers CustomerID="ALFKI"> 
    <Orders OrderID="10643" OrderDate="1997-08-25T00:00:00" /> 
    <Orders OrderID="10692" OrderDate="1997-10-03T00:00:00" /> 
C. Specify special characters in the query

The following query returns all distinct contact titles starting with Sa from the Customers table in the Northwind database. The example uses the LIKE clause and the special character % to search for the contact titles. In the LIKE clause, the special character % is specified as %25.


Here is the result set:

   <Customers ContactTitle="Sales Agent" /> 
   <Customers ContactTitle="Sales Associate" /> 
   <Customers ContactTitle="Sales Manager" /> 
   <Customers ContactTitle="Sales Representative" /> 

In the following example, order and order detail information is retrieved from the Orders and Order Details tables.


This is the partial result:

<Orders OrderID="10248">
  <Order_x0020_Details OrderID="10248" ProductID="11" UnitPrice="14.00" /> 
  <Order_x0020_Details OrderID="10248" ProductID="42" UnitPrice="9.80" /> 
<Orders OrderID="10249">
  <Order_x0020_Details OrderID="10249" ProductID="14" UnitPrice="18.60" /> 
D. Specify a query without the FOR XML clause

You can specify SQL queries without the FOR XML clause. The result is returned as a stream. In the query, you can specify only one column because streaming is not supported over multiple column results. In this example, the query returns the first name of employees from the Employees table in the Northwind database. The result is returned as a concatenated string of first names.

E. Specify the contenttype keyword

The contenttype keyword specifies the content-type of the document returned. text/XML is the default content-type of the document except when xsl is specified in the URL. When xsl is specified in the URL and contenttype is not specified, then contenttype defaults to text/html

In this example, the query returns a picture of an employee from the Employees table in the Northwind database. FOR XML mode is not specified because the returned data is compatible with the receiving application (that is, the browser can handle the returned data).


In retrieving images, contenttype is generally specified. If contenttype is specified, the ISAPI extension does not search for and remove any Access header information. Therefore, to retrieve any images that have the Access header information, contenttype should not be specified as shown in the previous example. In all other cases, contenttype should be specified as shown in this example:


The images can also be brought into an HTML document. In the following example, an .htm file (File1.htm) is created with these contents:

<img src="http://IISServer/nwind?sql=select photo from Employees where EmployeeID=1">

When this file is opened in the browser, an employee photo is displayed.

F. Specify the xsl keyword

In this example, the query returns the first and last name of all employees in the Employee table in the Northwind database. employee.xsl processes the result set.

When xsl is specified in the URL but contenttype is not specified in the URL and there is no content-type defined in the XSL style sheet, contenttype defaults to text/html. Therefore, the result is displayed in the form of a table with two columns (firstname, lastname).


The .xsl file is provided here. This file must exist in the virtual root directory or one of its subdirectories (in which case the file path specified is relative to the virtual root directory). In this example, the .xsl file is stored in the virtual root directory.

<?xml version='1.0' encoding='UTF-8'?>          
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match = 'Employees'>                        
         <TD><xsl:value-of select = '@FirstName' /></TD>      
         <TD><B><xsl:value-of select = '@LastName' /></B></TD>
    <xsl:template match = '/'>                               
           <STYLE>th { background-color: #CCCCCC }</STYLE>    
         <TABLE border='1' style='width:300;'>                
           <TR><TH colspan='2'>Employees</TH></TR>            
           <TR><TH >First name</TH><TH>Last name</TH></TR>    
           <xsl:apply-templates select = 'root' />            

Instead of specifying the contenttype in the URL, contenttype can also be specified as the value of the media-type attribute of the <xsl:output> element. For example, <xsl:output media-type="text/html" /> can be added after the namespace declaration in the preceding XSL file.

G. Pass parameters to SQL statements

Parameters can be passed to SQL queries. In this example, employee information for a given employee ID is returned from the Employees table in the Northwind database. The value of EmployeeID is provided as input to the query. Note that the ? character, used for a parameter marker in the URL, is a special character and is encoded as %3F. For more information about special characters, see Special Characters.


Here is the result set:

   <Employees FirstName="Nancy" LastName="Davolio" /> 

In this query, two parameter values are passed to the query:


Here is the result set:

   <Employees EmployeeID="1" Title="Sales Representative" /> 

See Also

Accessing SQL Server Using HTTP

Retrieving XML Documents Using FOR XML

Using IIS Virtual Directory Management for SQL Server Utility

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft