SQL Developer 'run script' failing but 'run statement' works - sql

I have a simple left join query on two tables linking to get the description:
SELECT WORK.ACTION_ID, WORK.ACTION_STAT, WORK.DESCRIPTION_ID,
WORK.CLIENT_SERVER_IND, WORK.UPDATE_TSTAMP, WORK.UPDATE_USER_ID,
WORK.OTHER_ACTION_DATA, D.DESCRIPTION_ID, D.LANGUAGE, D.DESCRIPTION_TEXT,
D.UPDATE_TSTAMP DSCRP_UPD_TSTAMP
FROM xxxx.R_ACTION WORK
INNER JOIN xxxx.R_DESC_TEXT D ON WORK.DESCRIPTION_ID = D.DESCRIPTION_ID
WHERE D.LANGUAGE = 'ENGLISH'
AND D.DESCRIPTION_STAT = 'P';
I have the query in SQL Developer and if I select 'Run Statement' it works fine in less than a second and I can retrieve all 283 records.
If I select 'Run Script' I can see the output for about 33 lines and it then just hangs and eventually I get a 'socket read timed out' message.
I have the same table in a different schema, but with slight different data in it, and in that one the run statement and run script work fine.
Why would it work for 'Run Statement' but fail for 'Run Script' in this schema?

I'm on the CLOB bandwagon, too. Thinking 90% chance your DESCRIPTION_TEXT is a CLOB field.
As a test, try this query as "run script", which ignores the actual DESCRIPTION_TEXT and outputs "bogus" instead:
SELECT WORK.ACTION_ID, WORK.ACTION_STAT, WORK.DESCRIPTION_ID,
WORK.CLIENT_SERVER_IND, WORK.UPDATE_TSTAMP, WORK.UPDATE_USER_ID,
WORK.OTHER_ACTION_DATA, D.DESCRIPTION_ID, D.LANGUAGE, 'bogus' DESCRIPTION_TEXT,
D.UPDATE_TSTAMP DSCRP_UPD_TSTAMP
FROM xxxx.R_ACTION WORK
INNER JOIN xxxx.R_DESC_TEXT D ON WORK.DESCRIPTION_ID = D.DESCRIPTION_ID
WHERE D.LANGUAGE = 'ENGLISH'
AND D.DESCRIPTION_STAT = 'P';
If that works, try using this query, which makes use of DBMS_LOB.SUBSTR() to help cast the CLOB as VARCHAR by truncating output to first 4000 characters of it.
SELECT WORK.ACTION_ID, WORK.ACTION_STAT, WORK.DESCRIPTION_ID,
WORK.CLIENT_SERVER_IND, WORK.UPDATE_TSTAMP, WORK.UPDATE_USER_ID,
WORK.OTHER_ACTION_DATA, D.DESCRIPTION_ID, D.LANGUAGE,
dbms_lob.substr(D.DESCRIPTION_TEXT, 4000) DESCRIPTION_TEXT,
D.UPDATE_TSTAMP DSCRP_UPD_TSTAMP
FROM xxxx.R_ACTION WORK
INNER JOIN xxxx.R_DESC_TEXT D ON WORK.DESCRIPTION_ID = D.DESCRIPTION_ID
WHERE D.LANGUAGE = 'ENGLISH'
AND D.DESCRIPTION_STAT = 'P';

Related

One Line of Code Breaks Otherwise Working Query

Everything in this query works just fine until I try to add the commented out portion (noted by '--') back into the query and run it. When I attempt to add the commented out WHERE clause back into the query body, it breaks the query. I have researched and experimented with adding the commented out line in different locations and in different ways, but nothing I have tried has worked so far.
SELECT
Count(DISTINCT a.acct_id),
CASE
WHEN b.user_seg = 'MTR' AND b.user_func = 'Sales' THEN 'MTR Sales'
WHEN b.user_seg = 'JMT' AND b.user_func = 'Sales' THEN 'JMT Sales'
THEN 'Partner Account Executive'
WHEN b.user_func IN ('Account Manager','Associate Account Manager','Outreach','Executive') THEN
'Customer Success'
WHEN b.user_func IN ('Relationship Manager') THEN 'Account Executive'
END AS sales_group
FROM qrs_access_views.mxp_sf_acct a
JOIN qrs_access_views.mxp_sf_user b
ON a.acc_crtd_by_id = b.user_id
AND a.is_sf_acct_del_y_n= 'n'
AND a.owner_id= '22573PAA3'
AND a.acct_mgr_user_id='22573PAA3'
AND b.user_seg <> '#'
--WHERE a.acc_crtd_date BETWEEN '2020-01-01' AND '2020-03-31'
LEFT JOIN grs_access_views.mxp_sf_user f
ON b.mgr_id = f.user_id
GROUP BY 2
When I run the query as above, I get a nice little grouped table kicked back to me by Teradata, but it is for all values for all time. (I just want to see the Q1 2020 numbers.)
When I add the commented out line back into the query I get some variation of the following error:
SELECT Failed. [3706] Syntax error: expected something between a string or a Unicode character literal and the 'LEFT' keyword.
Any help or assistance is greatly appreciated.
try this,
Instead of adding it to where clause use AND.
AND b.user_seg <> '#'
AND a.acc_crtd_date BETWEEN '2020-01-01' AND '2020-03-31'

Using the results of a select sub query as the columns to select in the main query. Injection?

I have a table that contains a column storing sql functions, column names and similar snippets such as below:
ID | Columsql
1 | c.clientname
2 | CONVERT(VARCHAR(10),c.DOB,103)
The reason for this is to use selected rows to dynamically create results from the main query that match spreadsheet templates. EG Template 1 requires the above client name and DOB.
My Subquery is:
select columnsql from CSVColumns cc
left join Templatecolumns ct on cc.id = ct.CSVColumnId
where ct.TemplateId = 1
order by ct.columnposition
The results of this query are 2 rows of text:
c.clientname
CONVERT(VARCHAR(10),c.DOB,103)
I would wish to pass these into my main statement so it would read initially
Select(
select columnsql from CSVColumns cc
left join Templatecolumns ct on cc.id = ct.CSVColumnId
where ct.TemplateId = 1
order by ct.columnposition
) from Clients c
but perform:
select c.clientname, CONVERT(VARCHAR(10),c.DOB,103) from clients c
to present a results set of client names and DOBs.
So far my attempts at 'injecting' are fruitless. Any suggestions?
You can't do this, at least not directly. What you have to do is, in a stored procedure, build up a varchar/string containing a complete SQL statement; you can execute that string.
declare #convCommand varchar(50);
-- some sql to get 'convert(varchar(10), c.DOB, 103) into #convCommand.
declare #fullSql varchar(1000);
#fullSql = 'select c.clientname, ' + #convCommand + ' from c,ients c;';
exec #fullSql
However, that's not the most efficient way to run it - and when you already know what fragment you need to put into it, why don't you just write the statement?
I think the reason you can't do that is that SQL Injection is a dangerous thing. (If you don't know why please do some research!) Having got a dangerous string into a table - e.g 'c.dob from clients c;drop table clients;'- using the column that contains the data to actually execute code would not be a good thing!
EDIT 1:
The original programmer is likely using a C# function:
string newSql = string.format("select c.clientname, {0} from clients c", "convert...");
Basic format is:
string.format("hhh {0} ggg{1}.....{n}, s0, s1,....sn);
{0} in the first string is replaced by the string at s0; {1} is replaces by tge string at s1, .... {n} by the string at sn.
This is probably a reasonable way to do it, though why is needs all the fragments is a bit opaque. You can't duplicate that in sql, save by doing what I suggest above. (SQL doesn't have anything like the same string.format function.)

PLSQL - Select works but Select within Where Clause returns no data

This is driving me crazy. I want to do simple comparison of a column and a variable but it just doesn't work. The QUERY 1 in following code returns me my value when i do a simple select, but i use the resulting variable in my 2nd query it just doesn't work..
It looks sooooo simple but I've been working on this for hours. The complete sql proc is
The big confusing thing is that if I replace v_bbg_symbol with some hard coded 'Value' (like 'FEDL01') it gives a correct answer for Query 2, but when I use the variable v_bbg_symbol it just doesn't work any more
Declare
v_bbg_symbol VARCHAR2(50);
V_OLD_INS_NAME Varchar2(50);
Begin
--QUERY 1
SELECT BBG_SYMBOL into v_bbg_symbol FROM quotes_external WHERE ID = 1;
--Gives output - 'FEDL01'
DBMS_OUTPUT.PUT_LINE('I got here:'||v_bbg_symbol||' is my value');
-QUERY 2
SELECT NAME INTO V_OLD_INS_NAME FROM INSTRUMENT
JOIN CURVE_INSTRUMENT ON
INSTRUMENT.INSTRUMENT_ID = CURVE_INSTRUMENT.INSTRUMENT_ID
JOIN GENERIC_INSTRUMENT ON
CURVE_INSTRUMENT.GENERIC_INSTRUMENT_ID = GENERIC_INSTRUMENT.GENERIC_INSTRUMENT_ID
WHERE CURVE_INSTRUMENT.CURVE_SNAPSHOT_ID =
(SELECT MAX(CURVE_INSTRUMENT.CURVE_SNAPSHOT_ID) FROM CURVE_INSTRUMENT)
AND GENERIC_INSTRUMENT.INSTRUMENT_NAME = v_bbg_symbol;
--ORACLE ERROR 'No Data Found'
DBMS_OUTPUT.PUT_LINE('I got here:'||V_OLD_INS_NAME||' is the new value');
END;
The first 'SELECT' gives me value which i select INTO a variable 'v_bbg_symbol', but when I use the same variable 'v_bbg_symbol' in my 2nd QUERY it pretends as if there is no value passed and does not return any result. If I give static value of 'v_bbg_symbol' i.e. ('FEDL01' in this case) in my 2nd QUERY, the results come as expected.
Please help..
Here is your query, with table aliases to facilitate following it:
SELECT NAME INTO V_OLD_INS_NAME
FROM INSTRUMENT i JOIN
CURVE_INSTRUMENT ci
ON i.INSTRUMENT_ID = ci.INSTRUMENT_ID JOIN
GENERIC_INSTRUMENT gi
ON ci.GENERIC_INSTRUMENT_ID = gi.GENERIC_INSTRUMENT_ID
WHERE ci.CURVE_SNAPSHOT_ID = (SELECT MAX(ci.CURVE_SNAPSHOT_ID) FROM CURVE_INSTRUMENT ci) and
gi.INSTRUMENT_NAME = v_bbg_symbol;
What this says is that the maximum ci.curve_snapshot_id is not for the instrument that is associated with v_bbg_symbol. I think you want a correlated subquery:
SELECT NAME INTO V_OLD_INS_NAME
FROM INSTRUMENT i JOIN
CURVE_INSTRUMENT ci
ON i.INSTRUMENT_ID = ci.INSTRUMENT_ID JOIN
GENERIC_INSTRUMENT gi
ON ci.GENERIC_INSTRUMENT_ID = gi.GENERIC_INSTRUMENT_ID
WHERE ci.CURVE_SNAPSHOT_ID = (SELECT MAX(ci2.CURVE_SNAPSHOT_ID)
FROM CURVE_INSTRUMENT ci2
WHERE ci2.instrument_id = i.instrument_id
) and
gi.INSTRUMENT_NAME = v_bbg_symbol;

SQL "column ambiguously defined"- when run from hibernate

When I run a SQL query from sqldeveloper , it runs fine When I run the same query from Hibernate SQL session, it gives me: "ORA-00918: column ambiguously defined" error.
Dies hibernate generate any kind of sql logs that can be used to debug the error?
Edit: here is the sql:(works fine from sqldeveloper)
SELECT main_contact.cont_name,
sub_contact.cont_name,
main_contact.cont_role_desc,
main_contact.cont_start_dte,
main_contact.cont_end_dte,
main_contact.cont_id,
sub_contact.cont_id,
main_contact.lookup_desc,
main_contact.cont_role_desc
FROM
(SELECT cont_rlat.cont_rlat_id ,
cont_role.cont_role_desc ,
cont.cont_name ,
cont.cont_ty_cde ,
cont.cont_sid ,
cont.cont_id ,
cont_rlat.rlat_from_dte ,
cont.cont_start_dte ,
cont.cont_end_dte ,
cont_rlat.app_id ,
lookup_data_mgmt.lookup_desc
FROM cont_rlat join
cont on cont_rlat.cont_sid = cont.cont_sid
join cont_role on cont_rlat.cont_role_id=cont_role.cont_role_id
join app on cont_rlat.app_id = app.app_ID
join LOOKUP_DATA_MGMT on app.app_TY_CDE = LOOKUP_DATA_MGMT.LOOKUP_ID
where
app.app_id =:investmentProfileCId
) main_contact left join
(SELECT cont.cont_id,
cont.cont_name,
cont_sub_rlat.cont_rlat_id
FROM cont_sub_rlat join
cont on
cont_sub_rlat.individual_cont_id = cont.cont_sid
) sub_contact on
main_contact.cont_rlat_id = sub_contact.cont_rlat_id
You're selecting main_contact.cont_role_desc twice. Also, you have two columns called 'cont_name' and two columns called 'cont_id', which also might not make it happy. If dropping the extra cont_name doesn't work, maybe try aliasing those?
SELECT main_contact.cont_name,
sub_contact.cont_name AS sub_cont_name,
main_contact.cont_role_desc,
main_contact.cont_start_dte,
main_contact.cont_end_dte,
main_contact.cont_id,
sub_contact.cont_id AS sub_cont_id,
main_contact.lookup_desc
Neither of those will be a problem in SQLDeveloper, but I imagine that at the very least having two exact copies of main_contact.cont_role_desc in the result set will be confusing Hibernate -- how will it tell them apart?
in hibernate.cfg.xml to make it log sql statements
<property name="hibernate.show_sql">true</property>

Problems with a Union Query with an image/varbinary field

I'm having some problems with the following Query:
SELECT v.idnum
,v.full_name
,convert(varbinary(max),s.signature) as Sig
FROM AppDB.dbo.v_People1 AS v INNER JOIN
OtherDB.dbo.Signatures AS s ON v.idnum = s.idnum
UNION
SELECT v.idnum
, v.full_name
, convert(varbinary(max), s.signature) as Sig
FROM AppDB.dbo.v_People2 AS v INNER JOIN
AppDB.dbo.Signatures AS s ON v.idnum = s.idnum
When i run them each of the queries separately without the union they execute quickly (<5 seconds), but when i run it using the union it's taking forever to execute (infact all it says is executing. I haven't seen it run successfully)
In OtherDB.dbo.Signatures the signature field is a varbinary(max) and in AppDB.dbo.Signatures the field is an image which is why i am using the convert expression.
Does anyone know what the problem might be and how i can go about fixing it?
Thanks
I wonder if it's because you are using a UNION instead of a UNION ALL. UNION by itself will remove duplicate rows between the data sets. This may be causing a delay on your blob field. See if there is a difference using UNION ALL.