SQL Query fails in Visual Studio - asp.net

I need to display data on a chart from a database. As far as I can tell, the chart has to be bound to a DataSource which in my case is a SqlDataSource. My query to get this data is:
SELECT A.a, A.b, B.b, C.b
FROM (SELECT * FROM T WHERE T.c = 'str1') A,
(SELECT * FROM T WHERE T.c = 'str2') B,
(SELECT * FROM T WHERE T.c = 'str3') C
WHERE A.a = B.a AND A.a = C.a AND A.d = B.d AND A.d = C.d
This works fine in Oracle SQL Developer, but when configuring the SqlDataSource in Visual Studio, I can execute the query in the Query Builder, but it displays an error on the next page--the Test Query page. The error I get is:
There was an error executing the query. Please check the syntax of the command and if present, the types and values of the parameters and ensure they are correct.
Syntax Error: Expecting identifier or quoted identifier.
What's causing this error and how can I fix it?

Related

Mulesoft- Sql select connector query issue

I see the below error while connect and pull the records from the database through the Mule Database connector with an Oracle database. Can someone look at it and let me know what's wrong with this query.
Error:
"java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended\n"
type: DB:BAD_SQL_SYNTAX
Query:
SELECT A.sample_central_id,A.req_ndc_prod_id,B.lot_num,A.req_product_desc,A.req_hcp_fst_name,A.req_hcp_last_name,A.req_prof_desig,A.az_hcp_id,A.req_addr_line_1,A.req_addr_line_2,A.req_city,A.req_state_cd,A.req_zipcode,A.ffevnt_shipped_qty,A.ffevnt_ship_dt FROM scrf.ship_prod_hist_vw A,scrfval.ship_dtl B WHERE Trunc(A.req_sample_requested_dt)>= Trunc(SYSDATE - 30)AND Trunc(A.req_sample_requested_dt)<= Trunc(SYSDATE + 10)AND Trunc(A.ffevnt_ship_dt)>= Trunc(SYSDATE - 8)AND Trunc(A.ffevnt_ship_dt)<Trunc(SYSDATE - 1)AND Upper(A.lkp_brand_desc) LIKE '%LYNPARZA%' AND A.sample_central_id = B.sc_req_id AND A.ffevnt_ff_vendor_id = B.shipment_id AND A.req_ndc_prod_id = B.ndc_prod_id AND A.ffevnt_shipped_qty<>0 ORDER BY 1;
Try removing the semicolon (';') at the end.

EF Core - Count from a specific column

I almost have my EF Core query working... This is the SQL getting produced (notice the Count(*):
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(*) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
What I need is (have Count look at a specific column/table)
SELECT [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn], COUNT(c.ID) AS [Clicks]
FROM [URLs] AS [u]
LEFT JOIN [OwnerUrls] AS [o] ON [u].[Key] = [o].[ShortUrlKey]
LEFT JOIN [Clicks] AS [c] ON [u].[Key] = [c].[ShortUrlKey]
GROUP BY [u].[Key], [u].[Url], [u].[CreatedBy], [u].[CreatedOn]
Here is the EF Query that I'm using...
query = (from u in db.URLs
join ou in db.OwnerUrls on u.Key equals ou.ShortUrlKey into urlOwners
from subSet in urlOwners.DefaultIfEmpty()
join c in db.Clicks on u.Key equals c.ShortUrlKey into urlClicks
from subClicks in urlClicks.DefaultIfEmpty()
group subClicks by new { u.Key, u.Url, u.CreatedBy, u.CreatedOn } into g
select new ShortURL()
{
Key = g.Key.Key,
Url = g.Key.Url,
CreatedBy = g.Key.CreatedBy,
CreatedOn = g.Key.CreatedOn,
Clicks = g.Count()
});
I've tried changing the g.Count() to g.Select(x=>x.Id).Count() and that just causes EF Core to barf and complain about client side evaluation vs server side evaluation etc..
I should mention that the reason I'm joining the first model (OwnerUrls) is to support a where clause that I didn't include here...
Thanks!
I'm not a EF developer, but have worked with SQL Server for a while now. In SQL Server i would use COUNT(DISTINCT c.ID) to eliminate any duplicates you might get from JOINS.
If duplicates are impossible due to the model the COUNT(*) shoud be sufficient.
Maybe this might help:
https://entityframeworkcore.com/knowledge-base/51892585/linq-select-distinct-count-performed-in-memory

How can I see the last SQL statement executed in Oracle database 11g r2?

I am new to oracle database.
Can someone give me an example of the steps for how to see the last statements executed on the Oracle database 11g r2?
You can use the below query to get the last sql executed based on last sql which was active in database
select ltrim(sq.sql_text)
from v$sql sq, v$session se, v$open_cursor oc
where sq.sql_id = oc.sql_id
and se.saddr = oc.saddr
and se.sid = oc.sid
and se.audsid = SYS_CONTEXT('userenv', 'sessionid')
order by oc.LAST_SQL_ACTIVE_TIME desc;
You can also use the below to find the last query executed in your session.
SELECT (SELECT t2.sql_fulltext
FROM v$sql t2
WHERE t1.prev_sql_id = t2.sql_id
AND t1.prev_child_number = t2.child_number) sql_fulltext
FROM v$session t1
WHERE t1.audsid = Sys_context('userenv', 'sessionid');
You can use the below query:
SELECT program_id, program_line#, sql_text
FROM V$SQL VS , ALL_USERS AU
WHERE (executions >= 1)
AND (parsing_user_id != 0)
AND (AU.user_id(+) = VS.parsing_user_id)
AND UPPER(AU.USERNAME) IN (UPPER('YourUser'))
ORDER BY last_active_time DESC;
if you need to know the statements of an PL/SQL object were executed then use or join with
select *
from dba_objects
where object_id = program_id
Find all sql where sql is like ....
select h.sample_time
, u.username
, h.machine
, s.sql_text
, h.*
from dba_hist_active_sess_history h
inner join v$sql s
on s.sql_id = h.sql_id
left outer join dba_users u
on u.user_id = h.user_id
where s.sql_text like 'DELETE%'
order by h.sample_time desc;
You need to be connected as sysdba user for this sql
A couple of hints:
In SQLplus, type a semicolon+ to see, and slash to execute again
In SQLdeveloper, use F8
If you mean see other users' statements then it's not possible by default.
You can configure AUDIT.
You can see some SQL statements in SELECT * FROM V$SQLAREA;
Connect as SYS user and execute the following query
select sql_text from v$sql where first_load_time=(select max(first_load_time) from v$sql) ;
select sq.PARSING_SCHEMA_NAME, sq.LAST_LOAD_TIME, sq.ELAPSED_TIME, sq.ROWS_PROCESSED, ltrim(sq.sql_text)
from v$sql sq, v$session se
where sq.PARSING_SCHEMA_NAME = 'YOUR_SCHEMA'
order by sq.LAST_LOAD_TIME desc;

Semantical Error - Doctrine 2.0

These are my tables:
Table Gift:
-id
-price
...
Table Couple:
-id
-name
...
table registry: //provide a many-many relation between gifts and couples
-id
-coupleId
-giftId
table purchase:
-amount
-registryId
I already wrote a sql query to get all the gift info for a specific couple
$qb = $this->createQueryBuilder('g') //gift
->from('\BBB\GiftBundle\Entity\Registry', 'reg')
->select('g.id , g.price')
->where('reg.gift = g.id')
->andWhere('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->setParameter('coupleID', $coupleID);
OR
SELECT g.id , g.price,
FROM gift g, registry reg
WHERE reg.gift_id = g.id AND reg.couple_id = 1
I also want to get the total amount that for the gifts that have been bought (if any)
EX. SUM(purchase.amount) as totalContribute
I have tried:
$qb = $this->createQueryBuilder('g')
->from('\BBB\GiftBundle\Entity\Purchase', 'p')
->from('\BBB\GiftBundle\Entity\Registry', 'reg')
->select('g.id , g.price')
->addSelect('SUM(p.amount) as totalContribute')
->leftJoin('p','pp', 'ON','reg.id = pp.registry')
->where('reg.gift = g.id')
->andWhere('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->setParameter('coupleID', $coupleID);
but it gives me the following error:
[Semantical Error] line 0, col 145 near 'pp ON reg.id': Error: Identification Variable p used in join path expression but was not defined before.
First of all, you should define join condition in your SQL statements after joins, not in WHERE clause. The reason is that it's really not efficient. So the query shoul look like:
SELECT g.id , g.price,
FROM gift g JOIN registry reg ON reg.gift_id = g.id
WHERE reg.couple_id = 1
But about your Doctrine query, You get error because you're defining joins in wrong way. Your query should more like:
$qb = $this->createQueryBuilder('g') // You don't have put "from" beacuse I assume you put this into GiftRepository and then Doctrine knows that should be \BBB\GiftBundle\Entity\Gift
->select('g.id , g.price')
->addSelect('SUM(p.amount) as totalContribute')
->join('g.purchase','p') // pay attention for this line: you specify relation basing on entity property - I assume that is "$purchase" for this example
->leftJoin('p.registry', 'reg') // here you join with \BBB\GiftBundle\Entity\Purchase
->where('reg.couple = :coupleID')
->orderBy('reg.id','DESC')
->setParameter('coupleID', $coupleID);
Please treat this as pseudocode - I didn't check it works but it should like more like this.
One more thing - if your repository method returns object(s) of X entity you should put this method to XRepository.

ASP.NET DataAdapter Query Not Valid While The Query Works

I started to use DataSet in my ASP.net web app like 6 months ago. It is a beautiful tool, allow me to rapidly develop MVC application without having to do all the dirty works in DB connection/queries.
But today I faced some weird problem. It started with this query:
select a.MR_PART_CODE as PART_CODE,
b.PART_DESC as PART_DESC,
b.PM_MAD_CAT_CODE as CATEGORY,
c.MPC_MIN_QTY as CAT_SS,
a.MR_MAX_LEAD_TIME as LEAD_TIME,
a.MR_MAD as MAD,
ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)) as CAL_SS,
greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY) as SS,
d.SOH as SOH,
d.SOO as SOO,
(select sum(back_order) from STK_REQUEST where part_code=b.part_code) as BO,
(d.SOH+a.MR_SOO) as AVAIL,
((d.SOH + a.MR_SOO)-greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY)) as ROQ,
(d.SOH - greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY) ) as VAR,
a.MR_REMARKS as REMARKS
from ROQ a, PART_MASTER b, MAD_PARTS_CATEGORY c, PART_STATS d
where a.MR_PART_CODE = b.PART_CODE
and d.PART_CODE = b.PART_CODE
and b.PM_MAD_CAT_CODE = c.MPC_CAT_CODE
and b.RETIRE_FLAG = 'N'
and a.mr_year = (select max(mr_year) from roq)
and a.mr_month = (select max(mr_month) from roq where mr_year= (select max(mr_year) from roq))
and a.mr_period = (select max(mr_period) from roq where mr_month=(select max(mr_month) from roq where mr_year= (select max(mr_year) from roq)) and mr_year= (select max(mr_year) from roq))
and greatest(ROUND((a.MR_MAD * a.MR_MAX_LEAD_TIME)),c.MPC_MIN_QTY) > d.SOH`
The query ran fine in Toad for Oracle, but apparently it fails when I tried to setup as a new query in DataAdapter object. It says something like "Error in list of function arguments: SELECT not recognized" to this line:
(select sum(back_order) from STK_REQUEST where part_code=b.part_code) as BO
What did I do wrong?
FYI, the database is Oracle.
It seems to be an exception from some ASP class trying to parse your SQL. There would be an ORA-xxxxx error number if the message came from Oracle.
You shouldn't put SQL like that in ASP. Create a view instead, perhaps it's ok then.

Resources