I'm trying to insert value into my temporary table with the code below.
And I got this error
SAP Hana Database Error: cannot
use parameter variable: DOCENTRY: line 8 col 35 (at pos 127) 2 0
I don't know what is wrong,
Please give some advise
thanks
This is the code:
CREATE PROCEDURE REP_PROC11( in docentry nvarchar(2))
AS
totalval DECIMAL (19,6);
BEGIN
CREATE local TEMPORARY TABLE #TempItem AS (
select
'FGA000001' as "ItemCode",
'2IN' as "WhsCode",
ifnull(
(
select
"U_IM1_GR"
from "#OFNC" where "DocEntry" = :docentry
),0) as "Qty",
'11080302' as "Account",
'S02' as "ProfitCode",
'IN-PN' as "ProfitCode2"
from DUMMY
union all
select
'FGA000002' as "ItemCode",
'2IN' as "WhsCode",
ifnull(
(
select
U_IM2_GR
from "#OFNC" where "DocEntry" = :docentry
),0) as "Qty",
'11080302' as "Account",
'S02' as "ProfitCode",
'IN-FN' as "ProfitCode2" from dummy);
SELECT X.*, (X."Qty"*:totalval)/Y."TotalQty" as "Val", :totalval as "TotalVal"
FROM #TempItem X
LEFT JOIN (SELECT SUM(X1."Qty") as "TotalQty" FROM #TempItem X1) Y ON 1 = 1
WHERE X."Qty" > 0;
drop TABLE #TempItem;
end
That’s a limitation of the SQL in your DDL code - the CREATE TEMPORARY TABLE bit.
Given your code so far, you don’t need to use a temp. table but could yield the same result (with less resource usage) by using table variables.
Related
I'm trying to call this calculated column 'RelativeEffectiveSpreadAbsoluteValue' in SQL servers' FROM part.
, case when cast(sa.Mid_Price as float) = 0
then 0
else ((CAST(sa.Ask_Price as float)-cast(sa.Bid_Price as float))/CAST(sa.Mid_Price as float))/(0.01/100)
end As RelativeEffectiveSpreadAbsoluteValue
like this, but the SQL server won't recognize it
left join [RISK].[dbo].[FILiquidityBuckets] FB6
ON FB6.Metric = 'Relative spread ' AND (
((CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)>= 0 AND CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)< 1000000) AND
FB6.LiquidityScore = 5) OR
((CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)>= 1000000 AND CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)<10000000) AND
FB6.LiquidityScore = 4) OR
((CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)>= 10000000 AND CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)< 100000000) AND
FB6.LiquidityScore = 3) OR
((CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)>= 100000000 AND CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT)<1000000000) AND
FB6.LiquidityScore = 2) OR
(CAST(RelativeEffectiveSpreadAbsoluteValue AS FLOAT) >= 1000000000 AND F65.LiquidityScore = 1)
)
So far I know by using 'Cross Apply' a calculated column can calculate another column in the same view,
like this example
Select
ColumnA,
ColumnB,
c.calccolumn1 As calccolumn1,
c.calccolumn1 / ColumnC As calccolumn2
from t42
cross apply (select (ColumnA + ColumnB) as calccolumn1) as c
but this is only for the select part, can we use it in the From part?
Please help thank you!
Put the apply operation which does the calculation prior to the join in your query:
create table t(a int);
create table u(b int);
select t.a,
t2.calculatedColumn,
u.b
from t
cross apply (select t.a * 2) as t2 (calculatedColumn)
left join u on u.b = t2.calculatedColumn
As Panagiotis observed, this may result in a slow join because the join predicate will not be able to use an index. But if the nature of your query demands it, the language supports it.
If you need this to be fast, create an indexed computed column on the table you have aliased as sa instead of calculating it in the query. Since your column will be of type float, you will need to mark the computed column as persisted before you can index it.
Hi i want to create a query which does the following. When the paramter 25 is selected it only runs part A of the query, if any other number is selected run both Table A and B select queries.
Example Below:
DECLARE #Type varchar (200)
select * from
(SELECT sort_code FROM dbo.Test 1
WHERE FUNDING_YEAR = 26)
union
(SELECT sort_code FROM dbo.Test 2
WHERE FUNDING_YEAR = 26)
Where case when #Type = 25 then select * from table2 else table 1
You just need to reference the variable in the WHERE clause
SELECT *
FROM TableA
WHERE #Type = 25
UNION
SELECT *
FROM TableB
The query above will always select everything in TableB and will only select everything in TableA when the variable is equal to 25.
Since you are using SSRS, what I would do is write the query to return all of the rows and then apply a filter in the SSRS report when the Paramater is 25. I wouldn't pass a paramater value to the SQL side unless it greatly reduces the run time of the query.
(I would have put this in a comment.)
I have 2 user defined functions which return a table:
Lets say them UDF1 and UDF2
select * from UDF1(param1) -> returns 1 result
select * from UDF2(param2) -> returns 1 result
The problem is when i do
select * from UDF1(param1) union all select * from UDF2(param2) -returns only 1 result.
Ideally it should return 2 results as its a union all.
Can someone help me why this behaviour is observed in sybase?
The exact code is as follows:
Created function as below:
EXEC SQL.
CREATE FUNCTION "ZCHECK_4" (
#COL3_VAL smallint
)
RETURNS TABLE (
"COL1" varchar(000030),
"COL2" varchar(000030),
"COL3" smallint
) AS RETURN SELECT
"ZTESTFUNC"."COL1",
"ZTESTFUNC"."COL2",
"ZTESTFUNC"."COL3"
FROM "ZTESTFUNC" "ZTESTFUNC"
WHERE "ZTESTFUNC"."COL3" = #COL3_VAL
ENDEXEC.
Final Sql view ->Which is returing only 1 row
CREATE VIEW "ZCHECK_5" AS SELECT
"ZCHECK_4"."COL1",
"ZCHECK_4"."COL2",
"ZCHECK_4"."COL3"
FROM "ZCHECK_4"(
CAST(
20 AS TINYINT
)
) "ZCHECK_4"
UNION ALL SELECT
"ZCHECK_4"."COL1",
"ZCHECK_4"."COL2",
"ZCHECK_4"."COL3"
FROM "ZCHECK_4"(
CAST(
10 AS TINYINT
)
) "ZCHECK_4"
Note : the underlying table(ZTESTFUNC) has 2 records which I cross validated.
Apparently for a UDF(User Defined Function) ,syntax after a select statement of a function would be ignored by the Sybase compiler.
Consider the below sceanrio:
Select F1 UNION ALL F2.
F1 and F2 being UDF with parameters, the highlighted text wouldn't be compiled in Sybase.
It might be a limitation of Sybase.
Note :This is not a case with tables or views where Union all works perfectly fine.
I am wondering if anyone can optimize following code:
LOOP AT dispinstock.
SELECT matnr SUM( gesme ) SUM( verme ) SUM( einme ) SUM( ausme )
INTO (dispinstock-matnr, dispinstock-gesme,
dispinstock-verme, dispinstock-einme, dispinstock-ausme)
FROM lqua
WHERE matnr = dispinstock-matnr
AND lgnum = 'xxxxx'
AND ( lgtyp IN zsd_t301_n
OR ( lgtyp >= '900' AND lgtyp <= '903' ) )
GROUP BY matnr.
MODIFY dispinstock.
ENDSELECT.
ENDLOOP.
dispinstock 170.000 records,
LQUA 210.000 records (will be larger > 1.500.000 records soon)
This loop take more than 3 minutes.
Would it be better to use hashed table instead?
Any help or idea would be appreciated.
Take the select out of the loop - pull all the data you need from lqua into a separate internal table in a single select statement. Then do a read on the second table inside the loop. Use a hash/sorted table or use a binary search.
You should also think about using a field symbol rather than using the modify.
field-symbols: <dispinstock> like line of dispinstock.
loop at dispinstock assigning <dispinstock>.
" some work
<dispinstock>-gesme = new value..
"...
endloop
This way you reduce the number of times you read the dispinstock table and change the value directly.
I´m sure that your internal table dispinstock does not contain 170.000 distinct materials!
So I suggest to build a table of distinct MATNRs and start the select with a FOR ALL ENTRIES IN ...
...
AND ( lgtyp IN zsd_t301_n OR ( lgtyp >= '900' AND lgtyp <= '903' ) )
Insert one row into your range object zsd_t301_n and delete the OR statement
Sign OPTION LOW HIGH
I BT 900 903
If the line that says "MODIFY dispinstock" means "update a row in the dispinstock table with the values that were just acquired from the SELECT", then you could probably replace the LOOP and the SELECT with a single MERGE statement.
Something like
MERGE INTO dispinstock
USING ( SELECT matnr, SUM( gesme ) gesme, SUM( verme ) verme, SUM( einme ) einme, SUM( ausme ) ausme
FROM lqua
WHERE lgnum = 'xxxxx'
AND ( lgtyp IN zsd_t301_n
OR ( lgtyp >= '900' AND lgtyp <= '903' ) )
GROUP BY matnr
) lqua
ON lqua.matnr = dispinstock.matnr
WHEN MATCHED THEN UPDATE SET
gesme = l.gesme, verme = l.verme, einme = l.einme, ausme = l.ausme
I've declared a table variable '#t', and have correctly performed the 'INSERT-INTO-SELECT'.
When I was trying to query the table variable with some additional computation for per-group row numbering, I got error either "Must declare the variable" when using '#t' directly or "invalid object name" while using alias of '#t'. Please kindly advise.
SELECT
*,
(SELECT COUNT(*) FROM "LTV" "COUNTER"
WHERE
"COUNTER"."Collateral_ID" = "LTV"."Collateral_ID"
AND
"COUNTER"."m_il_no" = "LTV"."m_il_no"
AND
"COUNTER"."Ref_Key" <= "LTV"."Ref_Key"
GROUP BY "COUNTER"."Collateral_ID", "COUNTER"."m_il_no"
) "MIL_IDX"
FROM #t AS LTV
Use:
SELECT x.*,
y.num
FROM #t x
JOIN (SELECT t.collateral_id,
t.m_il_no,
COUNT(*) AS num
FROM #t t
GROUP BY t.collateral_id, t.m_il_no) y ON y.collateral_id = x.collateral_id
AND y.m_il_no = x.m_il_no