How do you make Korma output the SQL it would execute? - clojure

I'm trying to get Korma to output the SQL it would execute so I can debug a problem I'm having, but the docs are very terse on how to use the as-sql function. Can anyone give me an example of how to make Korma output the SQL for an INSERT query?

from: http://sqlkorma.com/api/0.3.0/korma.core.html
dry-run
(dry-run & body)
Wrap around a set of queries to print to the console all SQL that would
be run and return dummy values instead of executing them.

If you want to get the SQL query as a string, there is also sql-only
(println
(sql-only
(select users)))

Related

Classic report issue with multiple inputs with IN statement

I am trying to refresh a report with dynamic action. And get the following errors:
{'dialogue': {'uv': true, 'line': [{'V': "failure of Widget}]}}
ORA-20876: stop the engine of the APEX.
classic_report"}]}}
I think its an issue with string which can't take and ST.ID IN (:P11_ROW_PK) in sql query.
Please suggest a workaround for the same.
This question requires the context you've provided in
https://stackoverflow.com/a/63627447/527513
If P11_ROW_PK is a delimited list of IDs, then you must structure your query accordingly, not expect the IN statement to deconstruct a bind variable containing a string.
Try this instead
select * from your_table
where st.id in (select column_value from apex_string.split(:P11_ROW_PK))
where REGEXP_LIKE(CUSTOMER_ID, '^('|| REPLACE(:P4_SEARCH,',','|') ||')$')
Above code will act same as APEX_STRING only if you are using lower version Apex

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.

Get the SQL statement made by a .exists() query in Django

Is it possible to print the SQL made by an queryset.exists() statement in Django?
Usually yes you can print SQL QuerySets generate however since exists() does not return a QuerySet but returns a simple boolean, it is more difficult.
Perhaps the easiest way is not to print SQL query for just exists() but for all queries in the view. You can follow other SO question on how to do that (example) or you can use django-debug-toolbar.
If you are also interesting in printing queries QuerySet generates, you can print a complete SQL query using:
print(Model.objects.filter(...).exists().query)
That will print a complete query.
If your intention however is to be able to copy-paste the query and execute it directly, it might not always work. For example printing the query does not always produce correct syntax such as with dates. There however is another useful method in Django Query objects (which is QuerySet.query is instance of) - sql_with_params(). That returns your parameterized query with the parameters themselves. For example:
sql, params = Model.objects.filter(...).exists().query.sql_with_params()
Model.objects.raw(sql, params=params)

Concat strings in SQL Server and Oracle with the same unmodified query

I have a program that must support both Oracle and SQL Server for it's database.
At some point I must execute a query where I want to concatenate 2 columns in the select statement.
In SQL Server, this is done with the + operator
select column1 + ' - ' + column2 from mytable
And oracle this is done with concat
select concat(concat(column1, ' - '), column2) from mytable
I'm looking for a way to leverage them both, so my code has a single SQL query literal string for both databases and I can avoid ugly constructs where I need to check which DBMS I'm connected to.
My first instinct was to encapsulate the different queries into a stored procedure, so each DBMS can have their own implementation of the query, but I was unable to create the procedure in Oracle that returns the record set in the same way as SQL Server does.
Update: Creating a concat function in SQL Server doesn't make the query compatible with Oracle because SQL Server requires the owner to be specified when calling the function as:
select dbo.concat(dbo.concat(column1), ' - '), column2) from mytable
It took me a while to figure it out after creating my own concat function in SQL Server.
On the other hand, looks like a function in Oracle with SYS_REFCURSOR can't be called with a simple
exec myfunction
And return the table as in SQL Server.
In the end, the solution was to create a view with the same name on both RDBMs but with different implementations, then I can do a simple select on the view.
If you want to go down the path of creating a stored procedure, whatever framework you're using should be able to more or less transparently handle an Oracle stored procedure with an OUT parameter that is a SYS_REFCURSOR and call that as you would a SQL Server stored procedure that just does a SELECT statement.
CREATE OR REPLACE PROCEDURE some_procedure( p_rc OUT sys_refcursor )
AS
BEGIN
-- You could use the CONCAT function rather than Oracle's string concatenation
-- operator || but I would prefer the double pipes.
OPEN p_rc
FOR SELECT column1 || ' - ' || column2
FROM myTable;
END;
Alternatively, you could define your own CONCAT function in SQL Server.
Nope, sorry.
As you've noted string concatentaion is implemented in SQL-Server with + and Oracle with concat or ||.
I would avoid doing some nasty string manipulation in stored procedures and simply create your own concatenation function in one instance or the other that uses the same syntax. Probably SQL-Server so you can use concat.
The alternative is to pass + or || depending on what RDBMS you're connected to.
Apparently in SQL Server 2012 they have included a CONCAT() function:
http://msdn.microsoft.com/en-us/library/hh231515.aspx
If you are trying to create a database agnostic application, you should stick to either
Stick to very basic SQL and do anything like this in your application.
Create different abstractions for different databases. If you hope to get any kind of scale out of your application, this is the path you'll likely need to take.
I wouldn't go down the stored procedure path, you can probably get it to work, but but week you'll find out you need to support "database X", then you'll need to rewrite your stored proc in that database as well. Its a recipe for pain.

<cfquery> not retrieving DATA

I am unable to retrieve any data from my cfquery. Same query when i run in sql developer i get the result.
Any reason why ?
Hi all, thanks for the responses. Sorry, it was my fault.
It was a data issue. I was retrieving uncommited data from CF.
You can also build the query in CFEclipse, test it and then paste the query in your CFQuery tag.
Also check how you have put the query name in the CFoutput tag, so many times I've put #queryname# instead of queryname in cfoutput.
Is the query actually being ran?
If you can turn debugging on, does the query show as being executed?
Also when you run the same query do you mean you copy/paste the query from the debugger into sql developer?
Perhaps the same values are not being included (if you are using variables in there)