Question

OK, based on the error message, you would think this is simple (and it probably is...).

I'm using an IIF (immediate if) in a FoxPro 9.2 database to evaluate an expression and then perform one of two actions depending on how the expression evaluates (basically, if a record exists, update it; if not, insert it).

IIF(
    ((SELECT COUNT(*) FROM tblName 
    WHERE tblName.Fldname = 'FLDMy' AND tblName.rCode = '000004') > 0), 
    (UPDATE tblName SET tblName.Desc = 'Me' 
    WHERE tblName.Fldname = 'FLDMy' AND tblName.rCode = '000004'), 
    (INSERT INTO tblName(Fldname, rCode, Desc) 
    VALUES ('FLDMy','000004','Me'))
    )

This always returns error: "Function name is missing )." Note that all the parentheses are matched, so it's not missing a ")" anywhere. The individual UPDATE and INSERT pieces work fine when run separately, so I suspect it's the expression (first statement in the IIF) that's the problem.

I'm executing the SQL from a C# service, so I'd like to be able to do the check and action in a single call into the FoxPro database.

Was it helpful?

Solution 2

You're confusing VFP. "SELECT" is a (built-in) SQL command, a built-in command to change the current work area, and it's also a function which returns the currently selected work area number.

In addition, IIF() is a function which takes expressions (something that evaluates to a single value) as its parameters. The SQL SELECT and UPDATE commands don't do that, even when you wrap them in parentheses. It's really for returning one of two values based on the value of the first parameter - it's really not for control flow logic. I suggest rewriting your code in the form of an IF .. ELSE.. ENDIF block.

OTHER TIPS

The VFP IIF() can return "Character, Numeric, Currency, Date, or DateTime". But it can't execute a statement. What I would try is to wrap your IIF() in the VFP EVALUATE() function. I would also make the second and third parameters return a string of the UPDATE and INSERT statements. For Example:

EVALUATE( IIF(.t., "UPDATE tblName SET tblname.desc = 'ME' WHERE fldname = 'FLDMy'", "INSERT INTO tblName(fldname) VALUES( 'FLDMy' ) " )  )

The above example is abbreviated from your statement for brevity. The above EVALUATE() will execute the UPDATE statement in this case. Give this a try. Then substitute your full statement.

I tried replicating what you are doing in the command window and receive the same error.

 x = ((SELECT COUNT(*) FROM tblName 
WHERE tblName.Fldname = 'FLDMy' AND tblName.rCode = '000004') > 0)

Returns the error "Function name is missing )." Execute your query, toss it in a cursor and do an If statement on the _Tally > 1

SELECT COUNT(*) FROM tblName 
WHERE tblName.Fldname = 'FLDMy' AND tblName.rCode = '000004' into cursor cTrash

If _Tally > 0
    UPDATE tblName SET tblName.Desc = 'Me' WHERE tblName.Fldname = 'FLDMy' 
      AND tblName.rCode = '000004'
Else
    INSERT INTO tblName(Fldname, rCode, Desc) 
      VALUES ('FLDMy','000004','Me')
EndIf
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top