ORA-00936 error:insert query is not working in procedure - stored-procedures

execute immediate 'insert into ' || rec.destinationtable || '(' || rec.destinationcolumn || ',' || rec.destinationcolumn1 || ') values ( select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||' ,select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||')';
it is showing "ORA-00936: missing expression" error in ORACLE 11g

You should use brackets when your are using select from multiple tables
Try
execute immediate 'insert into ' || rec.destinationtable || '(' || rec.destinationcolumn || ',' || rec.destinationcolumn1 || ') values ( (select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||' ),(select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||'))';

use like this
execute immediate 'insert into ' || rec.destinationtable ||
'(' || rec.destinationcolumn || ',' || rec.destinationcolumn1 || ')
( select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||' ,
select ' || rec.sourcecolumn || ' from ' || rec.sourcetable||')';
if you are specifying the cloumn names to which insertion is to be done, omit the 'values' phrase in the query.

'insert into '
|| rec.destinationtable
|| '(' || rec.destinationcolumn
|| ',' || rec.destinationcolumn1
|| ') values ( select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable
|| ' ,select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable || ')';
If you execute the above expression, you get something like this:
insert into [destinationtable]
([destinationcolumn]
,[destinationcolumn1]
)
values (
select [sourcecolumn] from [sourcetable]
,select [sourcecolumn] from [sourcetable]
)
which is not a valid insert statement.
You are using the VALUES clause and supplying two expressions, but those expressions are themselves subqueries, so they need to be surrouned by parentheses, e.g.:
insert into [destinationtable]
([destinationcolumn]
,[destinationcolumn1]
)
values (
( select [sourcecolumn] from [sourcetable] )
, ( select [sourcecolumn] from [sourcetable] )
)
So you need to include them in your expression, e.g.:
execute immediate
'insert into '
|| rec.destinationtable
|| '(' || rec.destinationcolumn
|| ',' || rec.destinationcolumn1
|| ') values ( ( select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable
|| ' ) , ( select '
|| rec.sourcecolumn
|| ' from '
|| rec.sourcetable || ' ) )';
To make the code somewhat clearer you could do this:
DECLARE
source1 VARCHAR2(1000);
source2 VARCHAR2(1000);
BEGIN
source1 := 'select ' || rec.sourcecolumn || ' from ' || rec.sourcetable;
source2 := 'select ' || rec.sourcecolumn || ' from ' || rec.sourcetable;
execute immediate
'insert into ' || rec.destinationtable
|| '(' || rec.destinationcolumn
|| ',' || rec.destinationcolumn1
|| ') values ('
|| '(' || source1 || ')'
|| ', (' || source2 || ')'
|| ')';
END;

Related

How can I implement this Automaton in Java?

I want to implement this deterministic finite automaton in Java and I want the program the program to recognize the language of the automaton.
Automaton
I already got this code, but I donĀ“t no how to make the switch the best way :
int estado = 0;
char c = 0;
//else ir para erro
switch(aOpcao) {
case 1:
if(c == '/')
estado = 1;
break;
case 2:
if(c == '!')
estado = 7;
if(c == '#')
estado = 6;
break;
case 3:
if(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'
|| c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' ||
c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q'
|| c == 'r' || c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w'
|| c == 'x' || c == 'y' || c == 'z' )
estado = 8;
if(c == '!')
estado = 7;
if(c == ',')
estado = 14;
if(c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'
|| c == 'g' || c == 'h' || c == 'i' || c == 'j' || c == 'k' ||
c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q'
|| c == 'r' || c == 's' || c == 't' || c == 'u' || c == 'v' || c == 'w'
|| c == 'x' || c == 'y' || c == 'z' )
estado = 13;
break;
if you are looking for a better way to check if c is alphabet or not for those two long if statement.
then one simple way in java is to change it to
if( (c >= 'a' && c <= 'z') )
if you are talking about how to completely translate the above code into java. You have read java switch() statement and java documentation by yourself. Your question seems like a homework problem.

How to use date filter with MuleSoft OData (version 2.0)

I am using MuleSoft (3.9) OData (2.0) RAML and passing query to an Oracle database. Adding date filter in the url
&$filter=START_DATE le datetime'2016-01-01T11:00:00' throws database error:
SQL command not properly ended.
How can date filter be added to OData RAML?
The database query is being generated as select....where START_DATE <= datetime'2016-01-01T11:00:00'. Do we need to explicitly convert using to_date?
Use below variable to parse OData filter to SQL filter
%var odataFilterToSQLFilter = (odataFilter) ->
(( odataFilter replace "eq null" with "is null"
replace "ne null" with "is not null"
replace " eq " with " = "
replace " ne " with " != "
replace " gt " with " > "
replace " lt " with " < "
replace " ge " with " >= "
replace " le " with " <= "
replace " and " with " AND "
replace " or " with " OR " ) splitBy " " map (
("TO_DATE('" ++ (($ replace "datetime'" with "" ) replace "T" with " ") ++ ",'yyyy-MM-dd HH24:MI:SS')") when $ as :string contains "datetime" otherwise $
)) joinBy " "
%var toSQLWhere = (odataFilter) -> (" WHERE " ++ odataFilterToSQLFilter(odataFilter)) unless odataFilter == null otherwise ""
---
"SELECT " ++ generateSqlFields(filters.select) ++ " FROM $remoteEntityName"
++ (
(toSQLWhere(filters.filter))

Lua gsub chars '(' and ')' fails

For some reason only the open and close bracket wont work, all others are fine.
RequestEncoded = string.gsub(RequestEncoded, '<', ' ')
RequestEncoded = string.gsub(RequestEncoded, '>', ' ')
RequestEncoded = string.gsub(RequestEncoded, '"', ' ')
RequestEncoded = string.gsub(RequestEncoded, '\'', ' ')
RequestEncoded = string.gsub(RequestEncoded, '\\', ' ')
-- RequestEncoded = string.gsub(RequestEncoded, '(', ' ') keeps failing
-- RequestEncoded = string.gsub(RequestEncoded, ')', ' ')
-- RequestEncoded = string.gsub(RequestEncoded, "\x28", " ") --keeps failing
-- RequestEncoded = string.gsub(RequestEncoded, "\x29", ' ')
-- RequestEncoded = string.gsub(RequestEncoded, '\050', ' ') --keeps failing
-- RequestEncoded = string.gsub(RequestEncoded, '\051', ' ')
) and ( are special characters that form a capturing group in a Lua pattern.
You need to escape them when they are outside of square brackets, [...], to match literal parentheses. You need to escape them with %.
string.gsub(RequestEncoded, '%(', ' ')
string.gsub(RequestEncoded, '%)', ' ')
However, since you are using the same replacement pattern in all the subsequent gsub calls, you may simplify your code to
RequestEncoded = string.gsub(RequestEncoded, '[<>"\'\\()]', ' ')
Note that here, () are inside a bracket expression and do not need escaping.
See Lua patterns docs:
Some characters, called magic characters, have special meanings when used in a pattern. The magic characters are
( ) . % + - * ? [ ^ $

A typical SQL Query in Erlang and Ejabberd

I want some help in writing a typical SQL query in Erlang. I just want to prevent logging the offline message based on this query.
Want to put this query at start of this function.
store_offline_msg(Host, {User, _Server}, Msgs, Len, MaxOfflineMsgs, odbc) ->
Count = if MaxOfflineMsgs =/= infinity ->
Len + count_offline_messages(User, Host);
true -> 0
end,
if Count > MaxOfflineMsgs -> discard_warn_sender(Msgs);
Mysql Table
ID | user | touser | starttime | end
586 | my_gmail.com | touser_gmail.com | 2014-10-29 14:03:16 | 60
Query I need is to check:
if 'user' and 'touser' have a record in this table, and current time comes in between starttime and startime + end (60 seconds)
Please help me in this, I appreciate it.
Here is how I implemented it and solved my problem.
Check = case catch ejabberd_odbc:sql_query(
LServer,
["select id from archive_disable_user"
" where ( ( user ='", To, "' "
" and tuser='", From , "' ) "
" or ( user ='", From, "' "
" and tuser='", To , "' ) )"
" and ( ( UNIX_TIMESTAMP(start) + end ) > UNIX_TIMESTAMP()); "]) of
{selected, ["id"], Rs} ->
Rcheck = len(Rs),
if
Rcheck == 0 ->
false;
true->
true
end;
_-> false
end,
Check.

Check if char contains a number, dash or parentheses

Given char how can I check if it is either numeric, contains a dash - or contains a left or right brace? ()
Something like this.
char ch = ... // some char
If (ch == '-' || ch == '{' || ch == '}' || (ch >= '0' && ch <= '9'))
If you give the parameters than you can choose which character you would like for example:
char ch = "-";
cout << "Enter date (month-day-year): " << endl;
cin << month << ch << day << ch << year;
cout << month << ch << day << ch << year << endl;

Resources