I mentioned in this article, "If it moves, script it," How to use Windows Script Host (WSH) to manage applications in Windows and Windows. Most of the examples in this article are based on managing the Windows operating system itself and are not based on applications running under that operating system. To meet the new millennium, I think I should talk about how to use scripts in numerous applications that expose scriptable interfaces. This time it involves only "SQL Server". In the next few months, I'll focus on Exchange, Office, and system Management servers.
You can use the script for SQL Server by using Distributed Management Objects, Data Transformation Services, and a new SQL Server XML implementation.
Many people are able to access the database through active Data Object (ADO) and active Server Page (ASP) technology. ADO does a lot of work to help you query and update your database-but what about the backup (Y2K) or data transfer between databases? And then it's not about XML.
Here, I'll tell you how to use the accompanying technology of ADO-especially the distributed management object, Data Transformation Services, and the new SQL Server XML implementation-to use the script for SQL Server.
Distributed Management Objects
Distributed Management Object (DMO) is a set of COM objects that encapsulate the SQL Server database with replication management. This means that you can write a WSH script that copies all the data from a particular table to a tab-delimited file, which helps to move a lot of data. I chose this example because its code is simple to write, but DMO allows you to get every object in SQL Server so that you can write some very good and meaningful management scripts.
The key to DMO is SQLDMO. SQL Server object, which is the basic object that allows you to connect to the server and get all available objects. In this case, I will use the database collection to select the databases, and then access the tables to be dumped to the file from the table collection. If you do not supply a database, an error message appears and the script ends. If you do not provide a table name, the script loops through all the tables in the database and exports the non-system tables. If a database is provided, it exports the table. This example, although limited in functionality, provides SQL Server with a simple export utility based on the command line, which you can base on.
Now let's look at a piece of code:
' Declare variable Dim oserver ' SQL Server Object Dim odatabase ' To use target database Dim obcp ' bcp Object Dim nrows ' number of rows returned from bcp Dim table ' table Object On Error Resume Next ' first parameter must be a database if wscript.arguments (0) = "Thenwscript.echo" You did not provide the database to be connected "wscript.quitend if" create SQL DMO instance Set oserver = CreateObject ("SQLDMO. SQL Server ") ' creates an instance set obcp = CreateObject (" SQLDMO ") of an DMO BulkCopy object. BulkCopy ") oserver.enablebcp = True ' Log on to local server ' I hope you have changed the sa password!! Oserver.connect ".", "sa" is connected to the provided database set odatabase = oserver.databases (wscript.arguments (0)) ' Set the delimiter to comma Obcp.columndelimiter = Vbcomma ' To set the file type to comma-delimited obcp.datafiletype = SQLDMODataFile_ Commadelimitedcharobcp.importrowsperbatch = 1000obcp.maximumerrorsbeforeabort = 1BCP. RowDelimiter = Vbcrlfobcp.serverbcpdatafiletype = Sqldmobcpdatafile_charobcp.useexistingconnection = True ' If no table is provided, All table F wscript are dumped. Arguments (1) = "" thenfor each table in Odatabase.tables ' to ensure that the table is not a system table if Table.systemobject = False Thenobcp.datafilepath = t Able.name & ". csv" nrows = table. ExportData (OBCP) WScript. ECho nrows & "Rows exported from" & Table.nameend ifnextelse ' Set output file Obcp.datafilepath = WScript. Arguments (1) & ". csv" nrows = Odatabase.tables (wscript.arguments (1)). ExportData (OBCP) WScript. Echo nrows & ' rows exported from ' & WScript.Arguments (1) End If
DMO's full content is much more than this article, but I hope this article will give you some perceptual knowledge: what can be done with some simple WSH scripts and DMO. You can download my sample (in English). For more information about DMO, please visit http://msdn.microsoft.com/library/psdk/sql/9_dmoc01.htm (English).
Data Transformation Services
Exporting to the Comma separated file (CSV) can be used as a starting point for exporting data to SQL and importing from SQL, but it's not like cutting-edge technology (too 20th century, don't you think?). SQL Server 7.0 provides a fairly sophisticated import and export mechanism in the form of Data Transformation Services (DTS). Fortunately, the script still has a place in this new area of SQL Server, so you can extend the capabilities of DTS with Visual Basic (r) script editing (VBScript), JScript (R), or Perl technology.
DTS settings are simple, especially when using SQL serverenterprise Manager. By default, SQL Server has a defined folder that stores any transformations, and Enterprise Manager provides a large graphical user interface (GUI) for creating and editing DTS packages. In the following example, I selected a employees.csv file that has been created by the Sqlexport.wsf file and a new table in SQL Server. The DTA package creates the table, loads it into the Text file, and then runs a script that converts the data to a table in SQL Server.
A transformation in a DTS package that keeps the script running throughout the conversion process. SQL Enterprise Manager provides a simple script editor with a parsing script button. The button will warn you of errors in the script before running the script. During the conversion process, the script uses the Cint feature of VBScript to convert the input of EmployeeID and deptid to int and convert all e-mail addresses to lowercase letters.
With DTS, you can take some perfect import/export mechanisms and enable you to use scripts in any step of the transformation. It is important to note that scripts may not be the best way to manipulate data-especially if your dataset is large. If you have a large amount of data to convert and performance is important, you might want to consider using Visual Basic or C + + to create a COM component and then call that component from within DTS. That is, if performance is not important and you want to convert it when data is imported/exported, the script gives you the flexibility to do this and enables you to store all of your code in the SQL Server database, making deployment fairly simple.
About XML
Currently, XML looks like the best tool for sharing data between applications, so all of the management of SQL Server certainly has the benefit of XML. It is possible to write scripts that extract all the data in the database and to write scripts that create XML documents in a programmatic way. However, if you just query "SQL Server", it's a good idea to have it return data in XML and then manipulate the XML with a script. The SQL Server group has recently released an informal version of SQL Server XML technology, which is completely done.
The new XML technology is very simple to use. It actually sends an HTTP request to the server, passes the query to the server, and the server returns XML. To illustrate, I created a simple WSH script that queries the local machine for all the data in the employee table of the North wind database. To access XML, the script uses the XML Analyzer that is included with Internet Explorer 5.01. The beauty of this object is that it processes all of your HTTP requests and enables you to synchronize calls. Because you no longer have to deal with any event-driven programs, it is useful for scripting.
The script is very simple. It creates an instance of the XML Analyzer, opens the URL using an analyzer, and then saves the result as an. XML file. Just five elements of the script, not bad!
' Set the URL of the request Xmlpath = ' Http://localhost/northwind?sql=select+*+from+employees+for+xml+auto ' to create an instance set of the XML analyzer Myxmldoc = CreateObject ("Microsoft.XMLDOM") ' does not require asynchronous Myxmldoc.async = False ' to load this URL myxmldoc.load xmlpath ' Save document Myxmldoc . Save "Employees.xml"
The strength of it is that it is now easy to establish a remote connection to the server and dump the data, as long as you change the URL, you have already done so. This example tells you how to export, but you can easily write an import routine, use an XML analyzer to iterate through the XML, and insert values into the database.
Summary
SQL Server provides a comprehensive and scriptable mechanism for managing databases locally or remotely. DMO and DTS are already on the market (in fact, many versions of DMO have been released), so you can use them directly to automate your database management tasks. Windows 2000 has been listed with Windows Script Host 2.0, so all of the above scripts are not subject to the conditions of Windows 2000 Server with SQL Server installed. The new XML technology in SQL Server makes it easy to access data, making it easier to write "SQL Server" scripts. For more information about SQL Server, please visit SQL Server Developer enter (English).
Andrew Clinick is the program manager in the Microsoft script Technology group, with the opportunity to be able to do so as long as it involves scripting. He spends most of his spare time watching the best football games on American television and explaining cricket to his American colleagues.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service