Referencing R values in SQL blocks in RMarkdown - sql

I'm working with an SQL block in RMarkdown and know that I can reference R variables like:
```{sql connection=db}
select * from [dbo].[B]
where BATCH_ID = ?BATCH_ID
```
Is there any special syntax that can be used to evaluate an R expression? I'm hoping I can do something like:
```{sql connection=db}
select * from [dbo].[B]
where BATCH_ID = ?RESULT$BATCH_ID
```
Is this possible?

OK, Found https://github.com/yihui/knitr/blob/ca04aa42ca59740408013dab7e6172af1c92f20d/R/engine.R and from line 429 onwards I can see the code for processing the variable substitutions. It just looks for variables, no evaluation of expressions, but at least I know where to go now to add that if I want to.
In the short term, I added the following to mass-pollute my environment with the variables I needed (didn't want to be eeking them out one-by-one).
mapply(FUN=assign, names(RESULT), RESULT, MoreArgs=list(pos=1))

Related

How to dynamically format BigQuery `dataset.schema.table name` with backticks

I need to work through how to take stored procedure functions from
region-us.INFORMATION_SCHEMA.ROUTINES
and modify the backticks that default to coming through around the project and place them around the dataset.schema.table()
The reason is more for uniform results across our system than a technical error need.
currently when I run this query
SELECT
replace(ddl, 'CREATE PROC', 'CREATE OR REPLACE PROC'),
FROM region-us.INFORMATION_SCHEMA.ROUTINES
where lower(routine_type) = 'procedure'
It will return the below:
`project-data-sandbox`.schema.MySP()
`project-data-sandbox`.schema.YourSP(MySP)
`project-data-sandbox`.inv.partnumber(orderid)
`project-data-sandbox`.inv_part.part_number(part_id)
I have tried the below query
SELECT
REGEXP_REPLACE(ddl, r"project-data-sandbox`.", "project-data-sandbox.") AS replaced_word
, REGEXP_REPLACE(ddl, r'`([a-zA-Z]+(-[a-zA-Z]+)+)`\.[a-zA-Z]+\.[a-zA-Z]+\(\)','Apples') tester
FROM region-us.INFORMATION_SCHEMA.ROUTINES
where lower(routine_type) = 'procedure'
I get part of what I want. However, the problem is our stored procedures can be named any sort of names and they could require objects to be passed to them.
I added the tester column to see if I could replace the project string with another word (or regex) but it isn't even replacing it with apples yet.
which I would want turned into this:
`project-data-sandbox.schema.MySP`()
`project-data-sandbox.schema.YourSP`(MySP)
`project-data-sandbox.inv.partnumber`(orderid)
`project-data-sandbox.inv_part.part_number`(part_id)
I'm working through Regexp_replace but I'm having difficulty figuring out how to get the backtick between the parenthesis and the last letter.
Thanks for any help!

How to replace append lines of in ABAP 7.5?

I have the following code snippet, that I would like to write in functional style :
data(lt_es) = me->prepare_process_part_ztoa1( ).
APPEND LINES OF me->prepare_process_part_protocol( ) to lt_es.
How to rewrite the code above in new ABAP 7.5?
Use the LINES OF construct (available since ABAP 7.40 SP 8).
For instance, it could be something like this:
lt_es = VALUE #( BASE me->prepare_process_part_ztoa1( )
( LINES OF me->prepare_process_part_protocol( ) ) ).
Whether it is better/simplier than the original, that's another question :)
It can be also done without BASE. However one must specify the type explicitly (usage of # ends with a syntax error).
REPORT ZZZ.
DATA: lt_t1 TYPE string_table,
lt_t2 TYPE string_table.
DATA(lt_t3) = VALUE string_table( ( LINES OF lt_t1 ) ( LINES OF lt_t2 ) ).
Would be interesting to know if this is maybe more performant than the usage of BASE if used in a loop for example.

Update command in sql having issue

I have below details in the table
GEMS#TEST1>select BUILTIN_ARGUMENTS from FND_FORM_CUSTOM_ACTIONS WHERE (RULE_ID = 2243);
BUILTIN_ARGUMENTS
--------------------------------------------------------------------------------
='http://prod.client.com:3001/ords/f?p=1:2:::NO::P_ORDER_HEADER_ID,P_SESSION
_ID:'||${item.ORDER.HEADER_ID.VALUE}||','||${ps.db_session_id.value}
For a need ,I have to update this "prod.client.com:3001" as
"test1-scan.client.com"
When I am executing below getting error
GEMS#TEST1>update FND_FORM_CUSTOM_ACTIONS set = '='http://test1-scan.client.com/ords/f?p=1:2:::NO::P_ORDER_HEADER_ID,P_SESSION
_ID:'||${item.ORDER.HEADER_ID.VALUE}||','||${ps.db_session_id.value}' WHERE (RULE_ID = 2243);
SP2-0552: Bind variable "NO" not declared.
GEMS#TEST1>
I know I might have to use escape character or declare the variable but not getting clue as I am not very good in coding .
Using REPLACE in this case is better.
UPDATE fnd_form_custom_actions
SET builtin_arguments = REPLACE (builtin_arguments, 'prod.client.com:3001',
'test1-scan.client.com')
WHERE rule_id = 2243 ;
You have obvious typos in your statement:
set = '='http://test1-scan.client.com/ords/f?p=1:2:::NO [.......]
set what = .....?
Then: what is with the second equal sign, in single quotes? (OR... I see - did you mean single quotes within the assigned string? You must enter TWO single quotes to represent one single quote in a string!)
Then: since the second equal sign consumes the single quotes, what follows AFTER it is not quoted. So :NO is seen as a bind variable. Correct the syntax and Oracle won't ask you about any bind variables.
With that fixed, look at Kaushik's answer for a better approach altogether.

SQL request to find item in table by value in column

Its pretty simple question, I know, but I really stacked with a problem with it...
I have a table customer_customer and a column code in it. So I need to find all items with a specific code value. So I wrote that:
SELECT * FROM customer_customer WHERE code LIKE "КL-12345"
and got an error:
column "КL-12345" does not exist
Why КL-12345 became a column if I specify it as value of code column? What am I doing wrong?
String literals must be enclosed in single quotes.
By enclosing it in double quotes, you specified a variable name.
Also, note that your where condition is the same as writing
where code = 'КL-12345'
LIKE is used for pattern matching. For instance you would match all codes that contain 'KL-12345' like this
where code like '%KL-12345%'
Change it to single quotes
SELECT * FROM customer_customer WHERE code LIKE 'КL-12345'
or
SELECT * FROM customer_customer WHERE code = 'КL-12345'

SQL Paramaters in FoxPro 2.6 DOS

In FoxPro 2.6 for MS-DOS is there a way to use a variable in a SELECT command? For example, how can I write the following query:
SELECT * FROM DBFILE WHERE Ord_no = temp_no
Given that temp_no is a previously defined variable. I tried using "&temp_no" but this does not appear to be the correct syntax.
Your code looks correct, and you shouldn't need to macro it via the "&". What may be failing is due to data types. If your table "dbfile", column "ord_no" is numeric and your variable "temp_no" is a character string, that would fail due to a data type mismatch... make sure they are the same data type... again, REGARDLESS of using the "&" macro.
MyVarOrd_No = 23
select * from DBFile where Ord_No = MyVarOrd_No
or if a string/charcter based column, just change
MyVarOrd_No = "23"
However you may need to pad with spaces/justify if its being picky.
The microsoft line on using variables in foxpro.