basic oracle question - sql

I have this query:
select total.lecgrouplecture(l.groupcode) lecturename,
total.lecgrouptime(l.groupcode) lecttime
from total.lecgroup l
where term = (select term
from total.CURENTTERM)
and rownum < 10
order by lecturename
I want to know what total.lecgrouptime(l.groupcode) is, and get this information from where?

total is the package name
lecgrouplecture is a function within that package
Look in user_source for the code or use a GUI like SQL Developer or TOAD

it looks like TOTAL is the name of a schema (SELECT * FROM all_users WHERE username = 'TOTAL'). If this is the case then lecgrouplecture must be a pl/sql function. You will find what it does with Robert's query:
SELECT *
FROM all_source
WHERE owner = 'TOTAL'
AND name = 'LECGROUPLECTURE'
ORDER BY line;

Related

How to get Sql Text for every username [duplicate]

I need to see the queries that are being sent to Oracle to execute them. Can someone give me specific detailed instructions on how to do this ?
If you want to see the queries from a specific user, you can use this (assuming you have privileges to query v$session and v$sqlarea (usually through SELECT_CATALOG_ROLE)
SELECT sess.sid,
sess.username,
sqla.optimizer_mode,
sqla.hash_value,
sqla.address,
sqla.cpu_time,
sqla.elapsed_time,
sqla.sql_text
FROM v$sqlarea sqla, v$session sess
WHERE sess.sql_hash_value = sqla.hash_value
AND sess.sql_address = sqla.address
AND sess.username = 'SCOTT'
Replace SCOTT with the appropriate username in your system
Output:
544 SCOTT ALL_ROWS 2004330732 07000001064088E8 89391 131836 SELECT sess.sid, sess.username,
sqla.optimizer_mode, sqla.h
ash_value, sqla.address, s
qla.cpu_time, sqla.elapsed_time,
sqla.sql_text FROM v$sqlarea sq
la, v$session sess WHERE sess.sql_hash_
value = sqla.hash_value AND sess.sql_
address = sqla.address AND sess.usern
ame = 'SCOTT'
This query will show queries that are currently running:
select sql_text from v$sqlarea where users_executing > 0;
See documentation of V$SQLAREA
You can check and get the data if you have access to these two oracle tables/views (v$sqlarea & v$sqltext), Also accoridng to your need you can also modify the query and add A.cpu_time, A.elapsed_time if required.
Query -
SELECT A.SQL_ID,
A.FIRST_LOAD_TIME,
A.SQL_TEXT,
A.SQL_FULLTEXT
FROM v$sqlarea A, v$sqltext B
WHERE A.PARSING_SCHEMA_NAME = 'TESTUSER' --YOUR USERNAME
AND A.SQL_ID = B.SQL_ID
AND A.HASH_VALUE = B.HASH_VALUE
ORDER BY A.FIRST_LOAD_TIME DESC
Output -

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

SQL query customized output

I have below query:
select 'my.MYNAME=' + name from hostnames;
my.MYNAME=abc
my.MYNAME=xyz
my.MYNAME=poi
The query is dynamic is gives3 result, it may give more result depending upon data.
I need following output:
my.MYNAME1=abc
my.MYNAME2=xyz
my.MYNAME3=poi
Numbers appending to MYNAME according to result it gives.
I have tried
select 'my.MYNAME={c}' + name from hostnames where (select count(*) as c from name);
but it is not working.
One way to go about it is:
SELECT CONCAT(CONCAT(CONCAT('my.MYNAME',ROWNUM),'='), name) FROM hostnames
DEMO

Invalid syntax for SQL Server

I'm trying to execute the following query in SQL Server, but it's throwing an error. How can I fix it?
select
T.T_Email
from
Stu_Question S, Tutor_Answer T
where
S.S_Quest_Id = '4f7a1518-a765-40c0-ae53-3ee61eef6673'
and S.S_Quest_Id = T.S_Quest_Id
and (T_Email,T_Answer_Update_Status)
IN (T_Email, Select MAX(T_Answer_Update_Status)
from Tutor_Answer
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
group by T_Email)
and S.S_Quest_Update_Status = (Select MAX(S_Quest_Update_Status)
from Stu_Question
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673')
This is the offending part of your statement:
and (T_Email,T_Answer_Update_Status)
IN (T_Email, Select MAX(T_Answer_Update_Status)
from Tutor_Answer
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
group by T_Email)
What on earth are you trying to do here???
T-SQL's IN operator works on one column at a time - like this:
WHERE T_EMail IN (SELECT EMail FROM .....)
marc_s correctly pointed out the offending part of your query.
You'll have to try to convert that to a join instead. Here is how I would do it:
select T.T_Email
from Stu_Question S
join (select T_Email,
row_number() over (partition by S_Quest_Id order by S_Quest_Update_Status desc) as rn
from Tutor_Answer) T
on T.S_Quest_Id=S.S_Quest_Id
and T.rn = 1
where S.S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673'
AND S.S_Quest_Update_Status=(Select MAX(S_Quest_Update_Status)
from Stu_Question
where S_Quest_Id='4f7a1518-a765-40c0-ae53-3ee61eef6673')
Notice that you can definitely improve this further. But it should get you going.

MySQL IN Operator

http://pastebin.ca/1946913
When i write "IN(1,2,4,5,6,7,8,9,10)" inside of the procedure, i get correct result but when i add the id variable in the "IN", the results are incorrect. I made a function on mysql but its still not working, what can i do?
Strings (broadly, variable values) don't interpolate in statements. vKatID IN (id) checks whether vKatID is equal to any of the values listed, which is only one: the value of id. You can create dynamic queries using PREPARE and EXECUTE to interpolate values:
set #query = CONCAT('SELECT COUNT(*) AS toplam
FROM videolar
WHERE vTarih = CURDATE() AND vKatID IN (', id, ') AND vDurum = 1;')
PREPARE bugun FROM #query;
EXECUTE bugun;
You could use FIND_IN_SET( ) rather than IN, for example:
SELECT COUNT(*) AS toplam
FROM videolar
WHERE vTarih = CURDATE()
AND FIND_IN_SET( vKatID, id ) > 0
AND vDurum = 1
Sets have limitations - they can't have more than 64 members for example.
Your id variables is a string (varchar) not an array (tuple in SQL) ie you are doing the this in (in java)
String id = "1,2,3,4,5,6,7"
you want
int[] ids = {1,2,3,4,5,6,7}
So in your code
set id = (1,2,3,4,5,6,7,8,9,10)
I cannot help you with the syntax for declaring id as I don't know. I would suggest to ensure the code is easily updated create a Table with just ids and then change your stored procedure to say
SELECT COUNT(*) AS toplam
FROM videolar
WHERE vTarih = CURDATE() AND vKatID IN (SELECT DISTINCT id FROM idtable) AND vDurum = 1;
Hope this helps.