MS SQL PHP Generator online Help
Prev | Return to chapter overview | Next |
Custom SQL queries
To use a SELECT query as a page data source, click the Create Query button or select the corresponding popup menu item, specify the query name and text, and click OK.
Rules for creating queries used by PHP Generator
All queries must satisfy the simple requirement: the following query must be correct.
select * from
(
QUERY_TEXT_YOU_ENTERED
) an_alias
This happens because the software uses similar queries for internal needs. In case such SQL expression is not valid, the wizard marks the query as invalid and displays its name in red.
To meet this requirement, make sure that all the columns in the result dataset have unique aliases. For example, the following query works fine itself, but returns a dataset with two columns named id:
SELECT
table1.*,
table2.*
FROM table1, table2
WHERE table1.id = table2.id;
This is the reason the wizard marks this query as invalid. To solve the problem, provide these columns with unique aliases:
SELECT
table1.id as table1_id,
table2.id as table2_id
FROM table1, table2
WHERE table1.id = table2.id;
Another possible reason is that SQL Server doesn't allow the ORDER BY clause in subqueries unless TOP or FOR XML is also specified. To use this clause, you can specify the top row count, but the best solution is to setup the order of rows using the Default Ordering option in the Page Properties window.
Creating updatable datasets (For Professional Edition Only)
To get an updatable dataset based on an SQL query, you have to provide up to three SQL queries: UPDATE, INSERT, and DELETE to be able to modify, add and remove records accordingly. The first query provides an UPDATE statement for modifying existing records; the second query provides an INSERT statement to add a new record to the tables; and the third one provides a DELETE statement to remove the records. Each of these queries can contain several parameterized statements that use parameters like :field_name.
Example
Assume that we have the following SELECT statement:
SELECT
id,
first_name,
last_name
FROM customer
WHERE last_name LIKE 'A%'
To create an updatable dataset based on this query, INSERT, UPDATE and DELETE statements can be specified as follows:
INSERT INTO
customer
VALUES (:id, :first_name, :last_name);
UPDATE customer
SET id = :id,
first_name = :first_name,
last_name = :last_name
WHERE id = :OLD_id;
DELETE FROM customer
WHERE id = :id;
Prev | Return to chapter overview | Next |