Direct SQL (Cannot select a record in (). The SQL database has issued an error.)

Hi All,

When I tried to execute the following query, kinda unexpected error I prompted by. Can anyone provide a solution to fix the iss

ue.

Hi Saktianatha try the below Code…

DictTable tblDirPartyTable;

DictField fldPartyName;

SqlSystem sqlSystem;

str sqlStatement;

ResultSet resultSet;

Statement statement;

UserConnection userConnection;

SqlStatementExecutePermission sqlPermission;

UserId userId;

tblDirPartyTable = new DictTable(tableNum(CustTable));

fldPartyName = new DictField(tableNum(CustTable), fieldNum(CustTable, AccountNum));

SqlSystem = new SqlSystem();

sqlStatement = ‘select %2 from %1’;

sqlStatement = strFmt(sqlStatement,

tblDirPartyTable.name(),

fldPartyName.name(),

sqlSystem.sqlLiteral(curext(), false),

sqlSystem.sqlLiteral(CustVendorBlocked::No, false));

userConnection = new userConnection();

statement = userConnection.createStatement();

sqlPermission = new SqlStatementExecutePermission(sqlStatement);

sqlPermission.assert();

resultSet = statement.executeQuery(sqlStatement);

CodeAccessPermission::revertAssert();

while (resultSet.next())

{

info(strFmt(’%1’, resultSet.getString(1)));

}

Fine thank you for the code. Still the same error I’m facing guys. What I suppose to do now ?

First of all, don’t use direct SQL queries unless absolutely necessary, which is not the case of your code. You got already stuck with a simple query and you may get into much more serious troubles later.

The query failed because the T-SQL command is select ACCOUNTNUM from X77X and no such table exists. Don’t bother executing it until you fix it.

You want to select from CUSTTABLE, not from X77X, therefore you obviously got the name wrong. The reason is that you call DictTable.name() with parameter _pseudoname = true, which isn’t what you want. Remove that.

(By the way, I don’t believe you actually ran the code provided by Saadullah, because he already fixed this particular error, although he didn’t explain it to you)

Also, you call strFmt() with four parameters intended to replace placeholders in the statement, but it accepts only two. Either update the statement or remove unused values from strFmt().

And is there any reason why the variable holding DictTable instance for CustTable is called tblDirPartyTable?

Well. Thank you Martin. But I Actually run the code which SAADULLAH provided but still the same error I got Martin. Also I’ve attached the code and the error screen shot for the saadullah code.

That’s a completely different error. You got “Request for the permission of type ‘SqlStatementExecutePermissions’ failed” because you ran it from a job on client. You have to execute your code on server tier. See more in Code Access Security.