I have a request with a select statement that works in Oracle, but when I execute it in SQL Server it throws an exception; the request is :
SELECT non_existant FROM
(SELECT rownum AS non_existant ,cab, validite FROM tmp_rapprochement)
WHERE validite like '%non_existant%'
The error is :
Msg 207, Niveau 16, État 1, Ligne 2 Nom de colonne non valide : 'rownum'.
Thank you.
The error says that rownum is not a valid column name (I'm glad Google translate is there to help)
Msg 207, Level 16, State 1, Line 2 Invalid column name 'rownum'.
You need to fix that error first - make sure that all columns from the select's list are defined in the table.
Once you get that out of your way, you would need to provide an alias for the inner select, like this:
SELECT non_existant FROM
(SELECT rownum AS non_existant ,cab, validite FROM tmp_rapprochement) x
-- x above is an alias, it is mandatory in SQL Server syntax.
WHERE validite like '%non_existant%'
EDIT It appears that you are porting this query from Oracle, so rownum is not a real column. In this case you should replace it with a row_number function, like this:
SELECT non_existant FROM
(SELECT row_number() OVER (Order by cab) AS non_existant
, cab -- ^^^ Put rapprochement's primary key there
, validite FROM tmp_rapprochement
) x -- x is an alias, it is mandatory in SQL Server syntax.
WHERE validite like '%non_existant%'
Related
While using minus clause in between two statements giving some error. Can someone help me with this?
Error is Msg 102, Level 15, State 1, Line 101
Incorrect syntax near 'MINUS'.
SELECT a from (SELECT DISTINCT(name) as a FROM hack WHERE name LIKE '%') a
MINUS
SELECT b from (SELECT DISTINCT(name) as b FROM hack WHERE name LIKE '[aeiou]%[aeiou]') b
MINUS is exist in Oracle. By seeing your error message, I hope you are looking in SQL Server.
In SQL Server, EXCEPT is the correct replacement for MINUS.
SELECT DISTINCT name
FROM hack
WHERE name LIKE '%'
EXCEPT
SELECT DISTINCT name
FROM hack
WHERE name LIKE '[aeiou]%[aeiou]'
You can simplify the logic to:
SELECT DISTINCT name
FROM hac
WHERE name NOT LIKE '[aeiou]%[aeiou]'
A simple comparison should be much more efficient that multiple comparisons along with set operators.
with ticket_orders AS(
select REGEXP_SUBSTR(st.ORDER_ID, '\d{3}-\d{7}-\d{7}') "new_order_id"
from
(select
REGEXP_SUBSTR(details, 'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}') AS "ORDER_ID"
from o_remedy ort
) st)
select new_t.new_order_id from ticket_orders new_t;
I'm trying to execute the following sql statement but it keeps showing error as "NEW_T"."NEW_ORDER_ID": invalid identifier
How to get this "new_order_id" because I need to join it further.
Either remove double quotes from the derived column name, or use double quotes in select clause as well.
select new_t."new_order_id" from ticket_orders new_t;
By default, Oracle convert every object name to upper case, unless you specify it in double quotes. So "new_order_id" will not be converted to upper case, but while selecting new_t.new_order_id will be converted to upper case, which doesn't exist.
To resolve it, either remove double quotes from the derived column name, or use double quotes in select clause as well.
I replicated it like this on my local machine.
This will not work.
with tbl as
(select empno "new_emp_no" from emp)
select t.new_emp_no From tbl t
And give
ORA-00904: "T"."NEW_EMP_NO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 116 Column: 10
But this will work.
with tbl as
(select empno "new_emp_no" from emp)
select t."new_emp_no" From tbl t
WITH ticket_orders AS
(
SELECT
REGEXP_SUBSTR(st.ORDER_ID, '\d{3}-\d{7}-\d{7}') new_order_id
FROM
(
SELECT
REGEXP_SUBSTR(details, 'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}') AS ORDER_ID
FROM o_remedy ort
) st
)
SELECT new_t.new_order_id FROM ticket_orders new_t;
I am trying to create a Pivot in Oracle. I keep getting the error message
ORA-00904: "VALUEZ": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 18 Column: 6
Any thoughts?
SELECT * FROM
(
SELECT ACC.NBR,CTA.NAMEZ
FROM ACCS ACC
JOIN CARS CAR ON CAR.CAR_AAD_ID = ACC.ACC_AAD_ID
JOIN CTAS CTA ON CAR_CUS_ID = CTA_CUS_ID
)
PIVOT
(
MAX(VALUEZ) --comes from table CTAS (ERROR LINE)
FOR NAMEZ IN ('1','2','3') --from table CTAS
)
ORDER BY ACC.NBR;
As a side note, I would love it if it was possible to turn the ('1','2','3') into a subquery, but it looks like that is not possible from other post i have read. If it was easy it would be (select distinct namez from CTAS)
The columns referenced in your PIVOT clause must exist in the row source that is being pivoted. You select ACC.NBR,CTA.NAMEZ from the table; it looks like you need to expand that to ACC.NBR,CTA.NAMEZ,CTA.VALUEZ.
You cannot use a subquery to replace the list of pivot values. I believe the underlying reason for this is that the parser must be able to figure out the columns that the query will produce prior to executing it; so the pivot values must be hardcoded.
What you might be able to do, if it is appropriate to wrap this query up in a procedure or function, is to first execute a query to get the list of pivot values, then build the pivot query string using that information and execute it via dynamic SQL.
ora-01748: only simple column names allowed here pivot
select* from (SELECT TRUNC(I.POST_DATE) DATES FROM INVOICE I
INNER JOIN INVC_TENDER_V T ON T.INVC_SID=I.INVC_SID)
PIVOT
(COUNT(*)
FOR T.TENDER_TYPE IN(0,1) )
While implementing this new idea Common Table Expressions in my query:
SELECT ..... FROM .... WHERE ... IN
(
;with CTEName as
(
CTE syntax goes here
)
SELECT .... FROM CTEName
)
GROUP BY ....
still getting the following query errors:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near ')'.
is this a valid usage of CTE?
Thanks.
WITH CTEName as
(
-- CTE syntax goes here
)
SELECT *
FROM mytable
WHERE myfield IN
(
SELECT ctefield
FROM CTEName
)
GROUP BY
myotherfield
In other words, the CTE should be defined before all other commands (as if they were actual tables).
Also note that the semicolon (;) is normally not required. However, the CTE syntax was implemented in SQL Server after it had already introduced the keyword WITH for its own purposes, so implicit statement breaking does not work with CTE anymore:
SELECT *
FROM mytable
WITH q AS (SELECT 1)
SELECT *
FROM q
It's hard to define where the WITH is used in the first or second statement here.
So it is considered a best practice to always prepend WITH with a semicolon so that you could easily cut and paste it anywhere in your code without having to worry about whether it is a first statement in a batch or not.
I am facing a problem in executing queries with CASE statement.
Based on my condition,(for eg. length), I want to execute different SQL statement.
Problematic sample query is as follows:
select case
when char_length('19480821') = 8
then select count(1) from Patient
when char_length('19480821')=10
then select count(1) from Doctor
end
Exception:
[Error] Script lines: 1-5 --------------------------
Incorrect syntax near the keyword 'select'.
Msg: 156, Level: 15, State: 2
Server: sunsrv4z7, Line: 2
I am not able to correct the syntax. I am getting the string for char_length as input from the user.
How can I fire queries based on certain condition?
Is CASE the right choice ? Or do I have to use any other thing.
Just put opening and closing bracket around select statement resolve you problem
select
case when
char_length('19480821')=8 then
(select count(1) from Patient )
when
char_length('19480821')=10 then
(select count(1) from Doctor )
end
select
case when char_length('19480821')=8 then (select count(1) from Patient)
when char_length('19480821')=10 then (select count(1) from Doctor)
end
The problem is that you are missing opening and closing brackets in your nested 'Select' statements :)
Please do note that it is not a case STATEMENT, it is a case EXPRESSION. By enclosing the queries in parentheses, you are converting them (syntactically) to values.
This is similar in principle to a subquery, such as
" select name from Doctor where salary = (select max(salary) from Doctor)"
select
case when
LEN('1948082100')=8 then
(select 'HELLO' )
when
LEN('194808210')=10 then
(select 'GOODBYE')
end
Change the values to test results.