Datagrip: execute sql statements in oracle with a parameter - datagrip

I routinely execute this statement using Ctrl - Shift - F10 in datagrip.
Is there any way to create a SQL or pl/SQL set of statements that contain a variable, and execute the file with DataGrip.
Datagrip's variable handling is not great - or I am missing something.
// somehow set $(jobName?) to a value in the file and re-use it.
update t_msg_log l
set l.job_status = 'FinishedSuccess'
where l.source_detail = $(jobName?)
and created =
(select max(created) from t_msg_log li where li.source_detail = l.source_detail);

This is called user parameters. The parameter pattern you use isn't described in DataGrip by default. You need to do this yourself.
Go to Settings/Preferences | Database | User Parameters
Add new parameter with this regexp: \$\([^\)\?]+\?\)
Don't forget to check 'Enable in console', 'In scripts'.
Enjoy!

Related

Squirrel SQL - user input prompt

I am a first time user of Squirrel SQL client for Oracle DB.
When I am executing the below queries it is not popping up with a user input dialog box, instead just displaying empty rows
Query
Select * from employees e
Where e.id in ('&emp_id');
The same query when executed in SQL Developer gives an user input dialog pop up.
Tried with = and other minor tweaks, nothing has worked for me
I don't use Squirrel SQL, but - from my point of view - your code doesn't work because the engine searches for ID values that are equal to string, literally '&emp_id'.
If you want to be prompted for a parameter value, try colon:
select * from employees e where e.id = :emp_id;
-------
this
You used IN; that's OK as long as you enter a single value for a parameter. If you planned to use several values (e.g. comma-separated list of values), that won't work just because. There's a way to do it - for example, you'd split those values into rows and use them as a subquery, but - that's beyond your current problem. First try to make it work with a single value passed as a parameter.
Squirrel SQL does not support input prompts in the way that SQL Developer does. One way to achieve similar functionality would be to use a prepared statement instead of a prompt.
You can do this by replacing the prompt with a bind variable, like this:
Select * from employees e Where e.id = ?
Then, when you run the query, Squirrel SQL will prompt you to enter the value for the bind variable (in this case, the value of emp_id). This will be done in a separate window in Squirrel.

Replace characters (&amp) in oracle

I'm trying to use the inbuilt oracle function to replace '&amp' with &. I wrote two functions below but it's not working for me. On running these two in sql developer tool its asking me for input. My requirement is to replace html entities.
select REPLACE('&', '&', '&') from DUAL;
select regexp_replace('&', '&', '&') from DUAL;
Could any one please tell me what's wrong I am doing?.
This should probably be marked as a duplicate, but you need to add this to your script
SET SCAN OFF - that tells us to ignore the & which is used for replacing text when running code in SQLPlus
Once you have that disabled, you can run the queries one at a time with data grids (the first execution mode) or as sqlplus scripts (the second execution mode).
You should execute set define off before executing your query to suppress processing of substitution variables which start by &.
The command set define off will stay active until you enter set define on on your sqlplus session or sql developer worksheet.
thatjeffsmith answer was right but set scan off is tagged obsolete by Oracle. Anyway, it still work.

Set define off not working in Oracle SQL Developer

I'm using SQL developer connected to an Oracle database, and I'm trying to create a trigger that looks something like below:
CREATE OR REPLACE TRIGGER my_trigger BEFORE
UPDATE ON ZONE FOR EACH ROW BEGIN :new.LAST_UPDATED_DTTM := SYSTIMESTAMP ;
END;
/
The problem I'm having is that the ":new" causes SQL developer to prompt for the value of the variable "new" (to try to bind the variable).
I tried using:
set define off;
It executes successfully, but I'm still prompted to enter a value for the variable.
Any idea what to do?
The trigger is created as expected if I use the "Run Script" option.
If I try to run the create statement using "Run Statement (Ctrl+enter)" it doesn't work.
So to successfully create the script I had to put only the trigger creation statements in the window and use "Run Script".

Use a database value in SQLPLUS as a parameter to a batch file

I need to run a batch file and pass in a database value. Depending on the day, a different value needs to be passed into the DOS command. This particular business rule is defined in a view in the database.
Our primary scheduling tool is datastage which runs scripts in SQLPLUS, so this is my preferred method for schedules activities. I could also run a batch file directly taht calls SQLPLUS and gets a value, but there is much more hassle setting it up so I'd prefer not to.
I'm a SQLPLUS amateur.
The SQL script I'm passing into SQLPLUS is below. The problem I'm having is it's passing in :sTriggerName, not the value of it.
set echo on
set feedback on
set termout on
VAR sTriggerName VARCHAR2(100)
SELECT TRIGGERNAME INTO :sTriggerName
FROM SCHEMA.VIEW
WHERE CALENDAR_DATE = TRUNC(SYSDATE) AND ROWNUM < 2;
/
HOST "E:\CallScheduleTrigger.CMD :sTriggerName."
quit
In the example above I am using a bind variable.
This link showed me hwo to load a substitution variable from the database and use that instead:
http://www.oracle.com/technetwork/testcontent/sub-var9-086145.html
To load a a database value into a substitution variable called sTriggerName
COLUMN TRIGGERNAME new_value sTriggerName
SELECT TRIGGERNAME FROM SCHEMA.VIEW WHERE CALENDAR_DATE = TRUNC(SYSDATE) AND ROWNUM < 2;
To use this substitution variable in a host command (i.e. as a parameter to a batch file):
HOST "E:\CallScheduleTrigger.CMD &sTriggerName"

How to resolve SQL query parameters mapping issues while using Oracle OLE DB provider?

When trying to enter a SQL query with parameters using the Oracle OLE DB provider I get the following error:
Parameters cannot be extracted from the SQL command. The provider might not help to parse parameter information from the command. In that case, use the "SQL command from variable" access mode, in which the entire SQL command is stored in a variable.
ADDITIONAL INFORMATION:
Provider cannot derive parameter information and SetParameterInfo has not been called. (Microsoft OLE DB Provider for Oracle)
I have tried following the suggestion here but don't quite understand what is required:Parameterized queries against Oracle
Any ideas?
To expand on the link given in the question:
Create a package variable
Double click on the package variable name. (This allows you to access the properties of the variable)
Set the property 'EvaluateAsExpression' to true
Enter the query in the expression builder.
Set the OLE DB source query to SQL Command from Variable
The expression builder can dynamically create expressions using variable to create 'parametised queries'.
So the following 'normal' query:
select * from book where book.BOOK_ID = ?
Can be written in the expression builder as:
"select * from book where book.BOOK_ID = " + #[User::BookID]
You can then do null handling and data conversion using the expression builder.
If You use Data Flow Task and use OLE DB Source, and you need parameterize your Query :
Create Variable to save "Full" of Query statement : Right Click on blank area outside the package - and Click Variables :
Click Add Variables on Variables Window :
Make the name is SQL_DTFLOW_FULL or something that can you understand easily. The variable data type is STRING
Create Variable(s) to save your parameter(s).
i.e, the full of Query stamements is :
SELECT * FROM BOOK WHERE BOOK_ID = #BookID --#BookID is SQL Parameter
at the sample above, I have just one parameter : #BookID, so I need to create one variable to save my parameter. Add more variables depends on your Queries.
Give it name SQL_DTFLOW_BOOKID
The variable data type is STRING
So, you need make your SSIS neat, and the variables is sorted in understandable parts.
Try to make the variable name is SQL_{TASK NAME}_{VariableName}
Make Expression for SQL_DTFLOW_FULL variable, click on number 1, and start fill number 2. Make Your SQL Statements to be a correct SQL Statement using string block. String block usually using "Double Quote" at the beginning and the end. Concat the variables with the string block.
Click evaluate Expression, to showing result, to make sure your query is correct, copy-paste the Query result at SSMS.
Make sure by yourself that the variables is free from SQL Injection using your own logic. (Use your developer instinct)
Open the Data Flow Task, open the OLE DB Source Editor by double click the item.
Select the Data Access Mode : SQL Command From Variable
Select the Variable Name : SQL_DTFLOW_FULL
Click Preview to make sure it works.
That is all, my way to prevent this SSIS failure case. Since I use this way, I never got that problem, you know, SSIS something is weird.
To change the variable value, set it before Data Flow Task, the SQL Result of SQL_DTFLOW_FULL variable will changed every you change your variable value.
In my case the issue was that i had comments within the sql in the normal form of /* */ and i also had column aliases as "Column name" instead of [Column Name].
Once i removed them it works.
Also try to have your parameter ? statement within the WHERE clause and not within the JOINS, that was part of the issue too.

Resources