Eminem Eminem - 1 year ago 82
SQL Question

sql server 2014 Why do I get the error with the code in a view and not stored procedure

When I try to add the following code to a view:

SELECT [Albi-Anagrafe-Associati].AnaUnica,
IIf([Comune] <> [ComunePDC], 'NO', 'OK') AS Attenzione,
FROM [Albi-Anagrafe-Associati]
WHERE ( ( ( [Albi-Anagrafe-Associati].ComunePDC ) <> 'Extra Provincia' )
AND ( ( IIf([Comune] <> [ComunePDC], 'NO', 'OK') ) = 'NO' ) );

I get the error:

Error in list of function arguments: '<' not recognized.

Unable to parse query text.

But when I add the same code in a stored procedure I get no error.

What is the reason for the difference in behaviour?

Answer Source

This error comes from the visual designer. This apparently hasn't been updated to cope with the more recent syntax additions.

Don't use that. It is buugy and very limited anyway. Just open a new query window and execute

CREATE VIEW dbo.SomeName
/*Paste your code here*/

and it will work fine if you are on a version later than 2012.

There are no actual syntax errors in your code though there are several things that can be improved.

Fixing the formatting, removing the unneeded two part names and simplifying the WHERE clause and SELECT list results in

SELECT AnaUnica,
       'NO' AS Attenzione, /*Guaranteed by the WHERE that [Comune] <> [ComunePDC]*/
FROM   dbo.[Albi-Anagrafe-Associati]
WHERE  ComunePDC NOT IN ('Extra Provincia',Comune);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download