日期:2013-02-10  浏览次数:20509 次

  SQL2005提供了一个新的执行存储过程或者T-SQL的方法,它可以以WEB服务的方式发布到服务器上,而无须使用IIS 这个新特点通过HTTP API把HTTP端点暴露给用户,在WINXP SP2和WIN2003上被支持

  建立一个HTTP端点是非常简单的,如下:

CREATE ENDPOINT MyEndpoint?
STATE = STARTED
AS HTTP (
 AUTHENTICATION = (INTEGRATED),
 PATH = '/sql/myendpoint',
 PORTS = (CLEAR) )
FOR SOAP (
 BATCHES = ENABLED,
 WSDL = DEFAULT
)

  在上面的案例中我建立一个命名为MyEndpoint的端点,它在http://localhost/sql/myendpoint监听T-SQL语句,你可以使用下面URL测试它

  http://localhost/sql/myendpoint?wsdl.

  上面这个URL还可以附加很丰富的参数,具体参见SQL协助

  下面这个例子显示如何通过JAVSCRIPT来调用端点执行T-SQL语句,如下:

function SendBatchRequest( strServerName, strUrlPath, strQuery )
{
  var objXmlHttp = null;
  var strRequest = "";
  objXmlHttp = new ActiveXObject( "microsoft.xmlhttp" );
  objXmlHttp.open( "POST", "http://" + strServerName + strUrlPath, false );
  objXmlHttp.setrequestheader( "Content-Type", "text/xml" );
  objXmlHttp.setRequestHeader( "Host", strServerName );
  strRequest = "<SOAP-ENV:Envelope
              xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'
              xmlns:sql='http://schemas.microsoft.com/sqlserver/2004/SOAP'>
               <SOAP-ENV:Body>
                 <sql:sqlbatch>
                  <sql:BatchCommands>" + strQuery + "</sql:BatchCommands>
                 </sql:sqlbatch>
               </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>";
  objXmlHttp.send( strRequest );
  if( objXmlHttp.status == 200 )
   return objXmlHttp.responseXML.xml;
  else
   return "";
}
var response = SendBatchRequest( 'localhost', '/sql/myendpoint', 'Select * from sys.http_endpoints' );