I have the following query that’s returning the error: “Incorrect syntax near the keyword ‘VIEW’.” I’ve tried to find any reference of this instance online and in SO. If I overlooked a solution or if anyone has any suggestions I’d greatly appreciate it.
Query:
<cfquery datasource="#mydatasource#">
CREATE VIEW #arguments.bulkRow.request_by#_uploader_features_view
(
feature_products_id
, feature_text
, feature_priority
)
AS
SELECT
a1.tbl_products__products_id AS feature_products_id,
a1.tbl_productfeature__feature_text__1 AS feature_text,
1 AS feature_priority
FROM bulk_product_upload a1
WHERE processed = 0
AND request_by = <cfqueryparam value="#arguments.bulkRow.request_by#" cfsqltype="cf_sql_varchar">
AND LEN( a1.tbl_productfeature__feature_text__1 ) > 1
UNION
SELECT
a1.tbl_products__products_id AS feature_products_id,
a1.tbl_productfeature__feature_text__2 AS feature_text,
2 AS feature_priority
FROM bulk_product_upload a1
WHERE processed = 0
AND request_by = <cfqueryparam value="#arguments.bulkRow.request_by#" cfsqltype="cf_sql_varchar">
AND LEN(a1.tbl_productfeature__feature_text__2) > 1
...
UNION
SELECT
a1.tbl_products__products_id AS feature_products_id,
a1.tbl_productfeature__feature_text__20 AS feature_text,
2 AS feature_priority
FROM bulk_product_upload a1
WHERE processed = 0
AND request_by = <cfqueryparam value="#arguments.bulkRow.request_by#" cfsqltype="cf_sql_varchar">
AND LEN(a1.tbl_productfeature__feature_text__20) > 1
</cfquery>
This is an abbreviated form of the query but it should get you started and show the basic layout.
Thanks in advance,
JP
I ran some testing against my own SQL Server and it appears that parameterized queries cause problems when creating views. Removing the CFQUERYPARAM tags should correct the issue. Using CFQUERYPARAM with a standard select statement is a best practice, but in this case you’re passing in SQL that will be executed every time the view is accessed, so the parameterization isn’t able to get passed through into the view.
Here are a couple of examples using the same concept, but tables from one of my own databases.
The following produces the
Incorrect syntax near the keyword 'view'error:The following works fine:
If anyone is able to locate a SQL Server reference which explains this in detail, feel free to edit this answer with a link.
I would also note that depending on the value of the ID, you might consider wrapping the view name with brackets. If the ID begins with a number the statement will fail as-is, but putting that into a bracketed statement will cover those situations (just be sure to use the bracket notation when querying the view as well). Example:
create view [#id#_view]