Using SQL statements in ASP 5: Start execution

Source: Internet
Author: User
Tags execution
Statement | Execution after learning the structure and purpose of a SELECT statement, you should learn how to use it. Under the database tools you have, this may mean that you have to press a button that says "execute." On an ASP Web page, you can execute an SQL statement immediately or as a stored procedure call.

Once you have created the SQL statement, you must also try to access its query results. Obviously, the key here is the ASP recordset. When you use a non-SQL Recordset, the code that creates the recordset is usually as follows:
Dim Objrec
Set Objrec = Server.CreateObject ("ADODB. Recordset ")
Objrec.open "Customers", objconn, 0, 1, 2

If you are familiar with the above code for ASP is not unfamiliar to you, you should know that "customers" means you open a database in the name of a datasheet.

Open recordset
To make the most of your familiar SQL skills, you need to adjust the most commonly used recordset on a regular ASP page:
Dim Objrec
Set Objrec = Server.CreateObject ("ADODB. Recordset ")
Objrec.open SQL, objconn, 0, 1, 2

The only modification here is the Objrec.open, which replaces the name of the datasheet to be queried with a variable containing the SQL statement.

One of the advantages of this approach is that you can specify the cursor type (as shown in 0, 1, 2 above).

Execute SQL
You can use a compact line of code to execute SQL statements to create a recordset. Here's the syntax:
Dim Objrec
Set Objrec = objConn.Execute (SQL)

In the example above, the SQL you see is the variable that holds your own SQL SELECT statement. The code line "runs" the SQL statement (or queries the database), selects the data and stores the data in the recordset, in the case of the variable Objrec. The main disadvantage of this approach is that you can't choose the type of cursor you want to use. Instead, the recordset is always opened with a forward cursor.

Because of the cursor, you might want to familiarize yourself with two methods of creating a recordset. Executing a query directly saves the time it takes to type a character, but in that case you have to use the default cursor, which is likely to encounter problems that are often not functioning properly. Whichever way you use it, the biggest difference between the two is nothing more than code refinement. Without considering what fields you get, what your criteria are, and how you store the data, the SQL Recordset will be much smaller in volume than the standard recordset opened on the ASP, let alone the simplicity of the operation. After all, by filtering the data, you eliminate time-consuming if-then tests and possible loops.

Writing tests with SQL
Here's a tip that many professional ASP programmers are accustomed to "write" their own SQL statements when testing a Web page. Doing so can help you debug your code, because you can see the string passed to the server for execution. And all you have to do is increase the response.writeyourvariable to display the information on the screen. You should attach this information when you submit an SQL-related issue to an ASP discussion group.



Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.