Tutorial on using SQL statements in ASP
There are so many SQL products in absurdly large way, maybe you don’t care about other things and just throw them away and do it. But if you want to use ASP and SQL at the same time, you may feel dizzy. MySQL, SQLServer and mSQL are all excellent SQL tools, but unfortunately, in the ASP environment, you don't need them to create practical SQL statements. However, you can use the Access knowledge you have mastered and the corresponding Access skills, plus our tips and skills, and I believe that you will be able to successfully add SQL to your ASP web page.
1.SELECT statement
In the SQL world, the most basic operation is the SELECT statement. When using SQL directly under the database tool, many people will be familiar with the following operations:
SELECTwhat
FROMwhichTable
WHEREcriteria
Executing the above statement will create a query that stores its results.
On ASP page files, you can also use the above general syntax, but the situation is slightly different. When programming ASP, the content of the SELECT statement should be assigned to a variable as a string:
SQL="SELECTwhatFROMwhichTableWHEREcriteria"
Okay, I understand the way SQL "talk" under ASP, and then do it the same. As long as your needs are met, the traditional SQL query mode and conditional query can be useful.
For example, you might as well assume that there is a data table in your database, the name is Products, and now you want to retrieve all the records in this table. Then you wrote the following code:
SQL="SELECT*FROMProducts"
The above code - the function of the SQL statement is to retrieve all the data in the table - after execution, all records in the data table will be selected. However, if you only want to remove a specific column from the table, such as p_name. Then you can't use the * wildcard character. You have to type the name of a specific column here, the code is as follows:
SQL="SELECTp_nameFROMProducts"
After executing the above query, all the contents in the Products table and the p_name column will be selected.
2. Set query conditions with WHERE clause
Sometimes taking out all the database records may just meet your requirements, but in most cases we usually only need to get partial records. How to design a query in this way? Of course it will be more brain-intensive, and this article also deliberately doesn't want you to use that hard recordset.
For example, if you only plan to take out the p_name records, and the names of these records must be started with the letter w, then you have to use the following WHERE clause:
SQL="SELECTp_nameFROMProductsWHEREp_nameLIKE'W%'"
The WHERE keyword is followed by the conditions used to filter data. With the help of these conditions, only data that meets certain standards will be queried. In the above example, the query result will only get a p_name record with the name starting with w.
In the above example, the percentage symbol (%) means that the query returns all record entries that start with w letters and are followed by any data or even no data. Therefore, when executing the above query, west and willow will be selected from the Products table and stored in the query.