PostgreSQL WITH .. AS () difficulties - sql

I am trying to use postgresql WITH AS () construction but I got error:
Even on simple queries like:
WITH a AS (
SELECT '2'
)
SELECT a
I got:
-->> ERROR: column "a" does not exist LINE 4: SELECT a
Where am i wrong? Thanks.

The easiest and most useful is to declare the column name:
with a(a) as (select '2')
select a from a;
But if you just select the table:
with a as (select '2')
select a from a;
a
-----
(2)
It will return the row valued type a which may or not be useful for you.

Try this sql code:
WITH a AS (
SELECT '2'
)
SELECT * FROM a;

Related

Why CONTAINS method doesn't work with REPLACE

I'm using SQL Server 2014 and I'm trying to execute a query with REPLACE method within a CONTAINS method like this:
SELECT *
FROM A
WHERE CONTAINS(Name, REPLACE('abcd', 'a', 'b'))
But the query returns an error
Incorrect syntax near 'REPLACE'.
How can I do it correctly?
You can write as below:
declare #param nvarchar(100)='abcd';
set #param= REPLACE(#param,'a','b');
SELECT *
FROM PersonAddress
WHERE CONTAINS(FullName, #param)
But first, You need to do this
Try with cte
with cte as
(
select
*
from A
where REPLACE('abcd', 'a', 'b') as col
)
select
*
from cte
where contains(col, name)

SQL Table Valued Function - Return certain data from 'WITH' based on parameter value

I am trying to do a "with" to loop through some data (which its doing fine). But after that with, I want to return data dependent on a bit parameter. Its important that this is inside a function. Below is basically what my code is doing.
WITH StuffChain
AS (
//initial
union all
//more
)
After this, I am trying to do something like
CASE WHEN #MyParamVal = 1 THEN
SELECT TOP (1) * FROM StuffChain
ELSE
SELECT * FROM StuffChain
END
RETURN
SQL is not my strength and I am still learning sorry. I am also unsure whether or not to use inline or multi statement function
EDIT: When I am giving the case, I am using it to explain what I am after to return, not necessarily what I will use. I use it to just describe what I need using what little I know if that makes sense.
First of all, using TOP without ORDER BY is somewhat meaningless, because it provides no order against which to select the first few rows. In this case, we can try using ROW_NUMBER to control the ordering:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM StuffChain
)
SELECT *
FROM cte
WHERE
(rn = #MyParamVal) OR (#MyParamVal <> 1);
You can do as follows. This is just one of the solution. you can do with many other ways also.
WITH StuffChain
AS (
//initial
union all
//more
)
After creation of CTE, try with following
SELECT TOP (CASE WHEN #MyParamVal = 1 THEN 1 ELSE
(SELECT COUNT(1) FROM StuffChain) END *
FROM StuffChain
order by <column> <ASC/DESC>;
We can use a Table Variable e.g. Declare #TEMP Table (intcol int,...) inside a function.
Declare #TEMP Table (intcol int,...)
WITH StuffChain
AS (
//initial
union all
//more
)
SELECT * INTO #TEMP FROM StuffChain;
--do what ever you want with temp table

Database HANA Error : cannot use parameter variable

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.

select specific records using IN

I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)

Return a constant table from Inline-valued table

Is it possible to return a constant table from an IVT? Now i'm using a regular table, and function is just returning data from this table with several additional operations.
So i want to return a table
Value
-----
0,265433
0,0629412
0,671626
I tried something like this:
ALTER FUNCTION [dbo].[GetEigenvector]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT Value = (SELECT 0,265433 UNION ALL
SELECT 0,0629412 UNION ALL
SELECT 0,671626)
)
but it throws an error:
Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.
You are messing up the syntax for giving an alias to your column:
ALTER FUNCTION [dbo].[GetEigenvector]
(
)
RETURNS TABLE
AS
RETURN
(
SELECT 0.265433 AS Value
UNION ALL
SELECT 0.0629412
UNION ALL
SELECT 0.671626
)
Another syntax:
alter function [dbo].[GetEigenvector]()
returns table as return
select * from
( values (0.265433), (0.0629412), (0.671626) )
as Eigenvector( Value );