Why I can't use the function predefined? - monetdb

I want to use the optimizer_stats function like:
select optimizer_stats();
And it returns
SELECT: no such operator 'optimizer_stats'
But I can find it in the sys.functions table (id=5976).
Also I find the UDF like reverse(string) can't be used too:
sql>select reverse('abc');
SELECT: no such unary operator 'reverse(char)'
So what's the problem?

Functions in MonetDB are bound in schemas. You cannot find those functions because your current schema is not 'sys'. Either change your session schema to 'sys' or use the full path (e.g. sys.reverse('abc')).
In default branch (next feature release) I added a 'search path' variable to search at other schemas when a SQL object cannot be found. By default it includes the 'sys' schema.

Related

How to create Interactive/Classic Report with dynamic SQL?

I'm running Apex 19.2 and I would like to create a classical or interactive report based on dynamic query.
The query I'm using is not known at design time. It depends on an page item value.
-- So I have a function that generates the SQL as follows
GetSQLQuery(:P1_MyItem);
This function may return something like
select Field1 from Table1
or
Select field1,field2 from Table1 inner join Table2 on ...
So it's not a sql query always with the same number of columns. It's completely variable.
I tried using PL/SQL function Body returning SQL Query but it seems like Apex needs to parse the query at design time.
Has anyone an idea how to solve that please ?
Cheers,
Thanks.
Enable the Use Generic Column Names option, as Koen said.
Then set Generic Column Count to the upper bound of the number of columns the query might return.
If you need dynamic column headers too, go to the region attributes and set Type (under Heading) to the appropriate value. PL/SQL Function Body is the most flexible and powerful option, but it's also the most work. Just make sure you return the correct number of headings as per the query.

Export a DB2 schema in human-readable format through command-line?

Unfortunately db2backup and db2look are not available to me.
Is there a way to export a schema in DB2 into a human-readable file through command-line (Table DDL/DML with CLOB/BLOB, Views, Aliases, Triggers, etc), and later use the same file for import?
There is an undocumented routine you can use for this using DB2 CLP, for example, or any other tool, which is able to call routines with OUT parameters.
db2 "call SYSPROC.DB2LK_GENERATE_DDL('-a -e -td #', ?)"
The 1-st parameter accepts almost all the same parameters as the db2look utility.
The call above returns some X value in the 2-nd output parameter (OP_TOKEN), which you can use to get the corresponding sql statements generated:
select sql_stmt
from systools.db2look_info_v
where op_token=X
order by creation_time, op_sequence;

how to specifically define table name in Oracle SQL

i have a DB which has a table named LIKE.
uppon trying to execute any query on the table, it gives me an error and i know it's because of the name which is trying to use the query keyword LIKE.
Now, i have "bypassed" this issue in MySQL by just selecting the table as
SELECT tk_oseba_id, COUNT(tk_tip_like_id) AS St_Like_haha
FROM student999.`like`;
Now this same line wont work at `l...is there any special way to to this in oracle or how can i manipulate with the table by not using the LIKE keyword.
Oracles's counter part to mysql's back tick is quote for defining tablenames/columns.
To use a key word as a table name though I recommend against it...
wrap the table name in quotes. From student9999."like"
AND... it forces case sensitivity when you use the quotes!

Why does "UPDATE Users SET Password=? WHERE Username=?" give a syntax error? [duplicate]

One of my columns is called from. I can't change the name because I didn't make it.
Am I allowed to do something like SELECT from FROM TableName or is there a special syntax to avoid the SQL Server being confused?
Wrap the column name in brackets like so, from becomes [from].
select [from] from table;
It is also possible to use the following (useful when querying multiple tables):
select table.[from] from table;
If it had been in PostgreSQL, use double quotes around the name, like:
select "from" from "table";
Note: Internally PostgreSQL automatically converts all unquoted commands and parameters to lower case. That have the effect that commands and identifiers aren't case sensitive. sEleCt * from tAblE; is interpreted as select * from table;. However, parameters inside double quotes are used as is, and therefore ARE case sensitive: select * from "table"; and select * from "Table"; gets the result from two different tables.
These are the two ways to do it:
Use back quote as here:
SELECT `from` FROM TableName
You can mention with table name as:
SELECT TableName.from FROM TableName
While you are doing it - alias it as something else (or better yet, use a view or an SP and deprecate the old direct access method).
SELECT [from] AS TransferFrom -- Or something else more suitable
FROM TableName
Your question seems to be well answered here, but I just want to add one more comment to this subject.
Those designing the database should be well aware of the reserved keywords and avoid using them. If you discover someone using it, inform them about it (in a polite way). The keyword here is reserved word.
More information:
"Reserved keywords should not be used
as object names. Databases upgraded
from earlier versions of SQL Server
may contain identifiers that include
words not reserved in the earlier
version, but that are reserved words
for the current version of SQL Server.
You can refer to the object by using
delimited identifiers until the name
can be changed."
http://msdn.microsoft.com/en-us/library/ms176027.aspx
and
"If your database does contain names
that match reserved keywords, you must
use delimited identifiers when you
refer to those objects. For more
information, see Identifiers (DMX)."
http://msdn.microsoft.com/en-us/library/ms132178.aspx
In Apache Drill, use backquotes:
select `from` from table;
If you ARE using SQL Server, you can just simply wrap the square brackets around the column or table name.
select [select]
from [table]
I have also faced this issue.
And the solution for this is to put [Column_Name] like this in the query.
string query= "Select [Name],[Email] from Person";
So it will work perfectly well.
Hi I work on Teradata systems that is completely ANSI compliant. Use double quotes " " to name such columns.
E.g. type is a SQL reserved keyword, and when used within quotes, type is treated as a user specified name.
See below code example:
CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)
--type is a SQL reserved keyword
TYPE
--see? now to retrieve the column you would use:
SEL "type" FROM alpha1
I ran in the same issue when trying to update a column which name was a keyword. The solution above didn't help me. I solved it out by simply specifying the name of the table like this:
UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')
The following will work perfectly:
SELECT DISTINCT table.from AS a FROM table
Some solid answers—but the most-upvoted one is parochial, only dealing with SQL Server. In summary:
If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS.
Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing:
For SQL Server use [square_braces] around the name. This works in an ODBC connection too.
For MySQL use `back_ticks`.
Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.
Dotting the offending word onto the table name may also work.
You can put your column name in bracket like:
Select [from] from < ur_tablename>
Or
Put in a temprary table then use as you like.
Example:
Declare #temp_table table(temp_from varchar(max))
Insert into #temp_table
Select * from your_tablename
Here I just assume that your_tablename contains only one column (i.e. from).
In MySQL, alternatively to using back quotes (`), you can use the UI to alter column names. Right click the table > Alter table > Edit the column name that contains sql keyword > Commit.
select [from] from <table>
As a note, the above does not work in MySQL
Judging from the answers here and my own experience. The only acceptable answer, if you're planning on being portable is don't use SQL keywords for table, column, or other names.
All these answers work in the various databases but apparently a lot don't support the ANSI solution.
Simple solution
Lets say the column name is from ; So the column name in query can be referred by table alias
Select * from user u where u.from="US"
In Oracle SQL Developer, pl/sql you can do this with double quotes but if you use double quotes you must type the column names in upper case. For example, SELECT "FROM" FROM MY_TABLE

oracle default value for a column

You know how we could use:
dateStamp DATE DEFAULT sysdate
to assign a default value to a column in table_x. What if I want to assign a default function? Can I do that?
The function will have some values from "table_params" to run some formula including a column named : "base" in table_x.
I could possibly write a cursor to loop through and run an update statement, but I was just curious if this is possible.
thanks in advance.
From Oracle documentation:
Restriction on Default Column Values
A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the
pseudocolumns CURRVAL, NEXTVAL, LEVEL, PRIOR, and ROWNUM, or date
constants that are not fully specified.
Either use a trigger (as was already mentioned) or run an UPDATE statement after your INSERT statement(s) (shouldn't be a problem if you keep your DML in PL/SQL).
You can write an INSERT trigger for the table that calls the function you want.

Resources