Executing a "long-length" Oracle command in C++ - c++

Problem: I have a C++ application that executes different Oracle commands. My application can execute this SQL statement the following way (I have ommited error checking and a few earlier steps):
strcpy(szProcName,"select grantee, granted_role from DBA_ROLE_PRIVS;");
rc=SQLPrepare(sqlc.g_hstmt,(SQLCHAR*)szProcName,(SQLINTEGER)strlen(szProcName));
rc = SQLExecute(sqlc.g_hstmt);
The select statement's data is placed/binded into an MFC List Control. This works without problem...
The issue comes when I try to execute long-length select statements.
I now wish to use the same method, but to run this long SQL statement:
SELECT a.GRANTEE, a.granted_role as "Connect", b.granted_role as "APPUSER" FROM
(SELECT GRANTEE, granted_role from DBA_ROLE_PRIVS where GRANTED_ROLE = 'CONNECT') a
FULL OUTER JOIN
(SELECT GRANTEE, granted_role from DBA_ROLE_PRIVS where GRANTED_ROLE = 'APPUSER') b
ON a.GRANTEE=b.GRANTEE;
Setting that entire statement into szProcName seems like the wrong way to go about things.
What I have tried: I tried to add all the SQL text into szProcName, but it does not fit and makes the code terribly messy. I also thought to create a Stored Procedure to call in C++. The Stored Procedure requires that I use an INTO clause and does not produce a table that I can use in C++. Is there a better way to do this?
Edit: I have found one working way. By increasing szProcName's size and usingstrcat(), I can add each line and then execute. I still wonder if there is a more appropriate way, especially if my statements become any larger (which they probably will).

I have found one working way. By increasing szProcName's size and using strcat(), I can add each line and then execute. I still wonder if there is a more appropriate way, especially if my statements become any larger (which they have).

Related

Select stmt in source qualifier along with procedure call in Informatica

We have a situation where we are dealing with a relational source(Oracle). The system is developed in a way where we have to first execute a package which will enable data read from Oracle and user will be able to get results out of select statement. I am trying to find a way on how to implement this in informatica mapping.
What we tried
1. In PreSQL we tried to execute the package and in SQL query we wrote select statement - data not getting loaded in target.
2. In PreSQL we wrote a block in which we are executing the package and just after that(within same beging...end block) we wrote insert statement on top of select statement - This is inserting data through insert statement however I am not in favor of this solution as both source and target are dummy which will confuse people in future.
Is there any possibility to implement this solution somehow by using 1st option.
Please help and suggest.
Thanks
The stored procedure transformation is there for this purpose configure it to execute source pre load
Pre-Sql and data read are not a part of same session. From what I understand, this needs to be done within the same session as otherwise the read is granted only for the session.
What you can do, is create a stored procedure/package that will grant read access and then return the data. Use it as a SQL Override on your SQ. This way SQ will read the data as usual. The concept:
CREATE PROCEDURE ReadMyData AS
BEGIN
execute immediate 'GiveMeTheReadAccess';
select * from MyTable;
END;
And use the ReadMyData on the Source Qualifier.

Select Statement Vs Find in Ax

while writing code we can either use select statement or select field list or find method on table for fetching the records.
I wonder which of the statement helps in better performance
It really depends on what you actually need.
find() methods must return the whole table buffer, that means, all of the columns are projected into the buffer returned by it, so you have the complete record selected. But sometimes you only need a single column, or just a few. In such cases it can be a waste to select the whole record, since you won't use the columns selected anyway.
So if you're dealing with a table that has lots of columns and you only need a few of them, consider writing a specific select statement for that, listing the columns you need.
Also, keep in mind that select statements that only project a few columns should not be made public. That means that you should NOT extract such statements into a method, because imagine the surprise of someone consuming that method and trying to figure out why column X was empty...
You can look at the find() method on the table and find out the same 'select'-statement there.
It can be the same 'select; statement as your own an the performance will be the same in this case.
And it can be different select statement then your own and the performance will be depend on indexes on the table, select statement, collected statistics and so on.
But there is no magic here. All of them is just select statement - no matter which method do you use.

SQLite How to use LIKE on a column and add wild cards

I'm trying to compare 2 similar columns from 2 tables in SQLite. I wrote this which doesn't throw an error but also doesn't return anything and I know it should.
SELECT t1.* from t1, t2 Where t1.col1 Like '%'||t2.col1||'%';
I'm looking for when t2.col1 is embedded inside of t1.col1. Both columns are of type TEXT.
Note: I'm using the C-Interface in C++ with Visual Studio 2010.
Ideas?
Edit:
I've played around with pulling a value out of t2.col1 that matches something in t1.col1 and writing something like this,
SELECT t1.* from t1, t2 Where t1.col1 Like '%ValueInT1%';
which works and returns something.
Is there a bug with SQLite when concatenating the '%' character in a like statement or is there a different syntax I should be using? I'm at a loss for why this isn't working.
I've also seen an SQL function called Locate which people use in different Databases to do this kind of check. Does SQLite have a Locate function?
EDIT 2:
I've run the first SQL statement in SQLite Administrator with some of my data and it DOES find it. Could there be a simpler problem? There are '_' in the data could that be causing a problem with the like?
(V.V) The answer was that I was joining to the wrong column in the real table. 2 columns had very similar names and I was checking the wrong one.
Thanks for the help.

OR query with Q object hanging

I'm constructing a query using the Q object but it's hanging.
When I "AND" the filters together, the query works fine. Here is the example:
School.objects.filter( Q(city__search='"orlando"'), Q(schoolattribute__attribute__name__search='"subjects"') )
But when I "OR" the filters together, the query just hangs because I'm assuming there's too much to process:
School.objects.filter( Q(city__search='"orlando"') | Q(schoolattribute__attribute__name__search='"subjects"')
I'm wondering what's going on here exactly and what can I do to mitigate it. Why does the query work when "AND" is used, but not when "OR" is used?
EDIT: Good tip #psagers. So it turns out that the AND query gets two INNER JOINs whereas the OR query gets two LEFT OUTER JOINs.
Given your situation, I'll assume the following:
You have a really big data set
You don't want to fetch too many entries
To optimize your code, you'd probably be better off using two queries:
schools_by_city = School.objects.filter(city__search='"orlando"')
schools_by_attribute_city = School.objects.filter(schoolattribute__attribute__name__search='"subjects"')
result = set(schools_by_city).union(set(schools_by_attribute_city))
This will probably be better than your original query (because you can use the INNER join), but you should test it out. If my assumptions are wrong, you should probably rethink your db structure (i.e. use a specialized tool for searching instead of mysql fulltext, rethinking SchoolAttribute, whatever floats your boat).

Syntax error in SQLite query

I am trying to insert a large number of records into a SQLite database. I get the above error if I try to use the sqlite3_exec C-API.
The code looks like this:
ret = sqlite_exec(db_p,".import file.txt table", NULL, NULL, NULL);
I know that the .import is command line, but can there be any way that you can do a extremely large insert of records that takes minimal time. I have read through previous bulk insert code and attempted to make changes but these are not providing the desired results.
Is there not a way to directly insert the string into the tables without having intermediate API's being called?
.import is most probably not available via the API. However there's one crucial thing to speed up inserts: wrap them in a transaction.
BEGIN;
lots of insert statements here;
COMMIT;
Without this, sqlite will need to write to the file after each insert to keep the ACID principle. The transaction let's it write to file later in bulk.
The answer to the syntax error could well be, that your strings are not enclosed in quotes in your SQL statement.