Problems with a Union Query with an image/varbinary field - sql-server-2005

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.

Related

U-sql error: Expected one of: AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')' ','

I have a following table:
EstimatedCurrentRevenue -- Revenue column value of yesterday
EstimatedPreviousRevenue --- Revenue column value of current day
crmId
OwnerId
PercentageChange.
I am querying two snapshots of the similarly structured data in Azure data lake and trying to query the percentage change in Revenue.
Following is my query i am trying to join on OpportunityId to get the difference between the revenue values:
#opportunityRevenueData = SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue) AS PercentageRevenueChange, optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue, opty.crmId, opty.OwnerId From #opportunityCurrentData AS opty JOIN #opportunityPreviousData AS optyPrevious on opty.OpportunityId == optyPrevious.OpportunityId;
But i get the following error:
E_CSC_USER_SYNTAXERROR: syntax error. Expected one of: AS EXCEPT FROM
GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE ';' ')'
','
at token 'From', line 40
near the ###:
This expression is having the problem i know but not sure how to fix it.
(((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue)*100)/opty.EstimatedCurrentRevenue)
Please help, i am completely new to U-sql
U-SQL is case-sensitive (as per here) with all SQL reserved words in UPPER CASE. So you should capitalise the FROM and ON keywords in your statement, like this:
#opportunityRevenueData =
SELECT (((opty.EstimatedCurrentRevenue - optyPrevious.EstimatedPreviousRevenue) * 100) / opty.EstimatedCurrentRevenue) AS PercentageRevenueChange,
optyPrevious.EstimatedPreviousRevenue,
opty.EstimatedCurrentRevenue,
opty.crmId,
opty.OwnerId
FROM #opportunityCurrentData AS opty
JOIN
#opportunityPreviousData AS optyPrevious
ON opty.OpportunityId == optyPrevious.OpportunityId;
Also, if you are completely new to U-SQL, you should consider working through some tutorials to establish the basics of the language, including case-sensitivity. Start at http://usql.io/.
This same crazy sounding error message can occur for (almost?) any USQL syntax error. The answer above was clearly correct for the provided code.
However since many folks will probably get to this page from a search for 'AS EXCEPT FROM GROUP HAVING INTERSECT OPTION ORDER OUTER UNION UNION WHERE', I'd say the best advice to handle these is look closely at the snippet of your code that the error message has marked with '###'.
For example I got to this page upon getting a syntax error for a long query and it turned out I didn't have a casing issue, but just a malformed query with parens around the wrong thing. Once I looked more closely at where in the snippet the ### symbol was, the error became clear.

Openquery Hung at Same Row Number

I'm attempting to do run an openquery in SQL Server 17 via a linked server to an Oracle connection feed. When I run the query posted below it gets stuck at row 7833 every single time.
Query:
SELECT
sys_ohi,
"Region",
sub_acct_no_ohi,
serv_cde_ohi,
connect_dte_ohi,
charge_amt_ohi
FROM openquery (MyServer, '
(SELECT DISTINCT
sys_ohi,
CASE
WHEN prin_ohi = ''1000'' THEN ''Seattle East''
WHEN prin_ohi = ''1500'' THEN ''Seattle West''
WHEN prin_ohi = ''2000'' THEN ''Oregon''
WHEN prin_ohi = ''3000'' THEN ''Sacramento''
WHEN prin_ohi = ''3500'' THEN ''San Francisco''
END AS "Region",
sub_acct_no_ohi,
serv_cde_ohi,
connect_dte_ohi,
charge_amt_ohi
FROM mytable_ohi
WHERE serv_cde_ohi IN (''INSTALL'')
AND connect_dte_ohi > trunc(to_date(''06-01-2017'',''MM-DD-YYYY'')))')
I have 36 different serv_cde_ohi IN (''INSTALL'') but I have created 36 UNION ALL queries for the different scenarios since attempting to put them within the 'IN' statement was terrible for performance.
I need to know why this query gets stuck at the same spot.
It is possible your UNION ALL declaration is dependent upon implicit data conversions to begin executing, then a data condition occurs during the cursor fetch where the implicit conversion fails in this manner you are observing.
Eliminate all implicit dependencies. For each item in each SELECT, explicitly establish the data types (the same type for each item, ordinally of course).

Declaring a variable in sql

Im sorry about the mess but what I'm trying to do is create a variable for ops$u2970. I've tried some ways that I found online and they did not work. This is part of a much larger query so replacing ops$u2970 with a variable would be great especially since it will need to be changed in the future throughout the query. This is easy in Python, but alas this is sql.
--***Creates a VIEW of all TIS GN's with their Corridor ID, Accum Mile and XY coords
create or replace view GN_DC_LOCATE as
select distinct t.gn_id, n.tcr_rt||n.tcr_rb DC_ID,
case when n.beg_brkm<n.end_brkm then
round(((t.gn_km-n.beg_brkm)+n.beg_tcrkm)*.6213712,3)
else round(((n.beg_brkm-t.gn_km)+n.beg_tcrkm)*.6213712,3)
end as GN_DCMI,c.x_coord TIS_XCOORD, c.y_coord TIS_YCOORD
from tis.tis_gn_locate t,tis.tis_tcr_lookup n,tis.tis_gn_coords c
where t.route=n.br_id and t.gn_km>=n.beg_abskm and t.gn_km<=n.end_abskm
and t.gn_id=c.gn_id
--***Creates a VIEW of all begin and end GN's on ops$u2087.sec_segments
--from the view GN_DC_LOCATE and adds in the records where GN=999999999999
create or replace view PVMGT_SEGS_GNs_DCMI as
select p.corridor_code_rb,
b.gn_id,b.GN_DCMI TIS_MI,
b.TIS_XCOORD TIS_X,b.TIS_YCOORD TIS_Y
from ops$u2970.sec_segments p, GN_DC_LOCATE b
where p.corridor_code_rb=b.DC_ID and p.beg_gn=b.gn_id
UNION
select p.corridor_code_rb,
e.gn_id,e.GN_DCMI TIS_MI,e.TIS_XCOORD TIS_X,e.TIS_YCOORD TIS_Y
from ops$u2970.sec_segments p,GN_DC_LOCATE e
where p.corridor_code_rb=e.DC_ID and p.end_gn=e.gn_id
UNION
select p.corridor_code_rb,999999999999 GN_ID, NULL TIS_MI,NULL TIS_X,NULL TIS_Y
from ops$u2970.sec_segments p
where p.beg_gn=999999999999 or p.end_gn=999999999999
order by 1,3
It's not possible to use variables in Oracle VIEW definitions. Variables are allowed in stored procedures, functions, triggers and packages.

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

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';

Pervasive Control Center SQL INTO Error

I am getting an error in PCC which doesn't make a lot of sense. I have two statements inside a user defined function that are nearly exactly the same and one runs fine while the other is returning an error:
'INTO': Syntax error
end and start are parameters being passed to the function.
The Error is being thrown on the second INTO statement
SELECT count(*) INTO :divModelTot1
FROM "table1"."info" i
WHERE i.compldate <:end
AND (i.agree is null OR i.agree>:start)
UNION ALL
SELECT count(*) INTO :divModelTot2
FROM "table2"."info" i
WHERE i.compldate <:end
AND (i.agree is null or i.agree>:start);
Any help or suggestions would be appreciate.
Thanks!
SELECT INTO must be the first query in a statement containing a UNION.
SELECT count(*) INTO :divModelTot1
FROM "table1"."info" i
WHERE i.compldate <:end
AND (i.agree is null OR i.agree>:start)
UNION ALL
SELECT count(*)
FROM "table2"."info" i
WHERE i.compldate <:end
AND (i.agree is null or i.agree>:start);