July 7, 2023 at 6:20 pm
WITH RECURSIVE factorial(F,n) AS (
SELECT 1 F, 3 n
UNION ALL
SELECT F*n F, n-1 n from factorial where n>1
)
SELECT F from factorial where n=1
Error
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'factorial'.
Completion time: 2023-07-07T14:16:59.5379702-04:00
If i remove RECURSIVE option it works fine.
July 7, 2023 at 7:18 pm
with recursive is not a MS SQL option. It is available on other DBMS like Postgres.
July 7, 2023 at 7:20 pm
Did you mean to use OPTION (MAXRECURSION)?
i.e., something like
WITH factorial(F,n) AS (
SELECT 1 F, 3 n
UNION ALL
SELECT F*n F, n-1 n from factorial where n>1
)
SELECT F from factorial where n=1
OPTION (MAXRECURSION n) -- where n is a value between 0 and 32,767
In this case, there's no reason for n to be larger than 2.
July 10, 2023 at 3:10 pm
I think you might want a recursive common table expression.
Something like this:
DECLARE @n int = 3;
WITH Factorial(F,n) AS
(
SELECT 1 AS F,
@n AS n
UNION ALL
SELECT F*n AS F,
n - 1 AS n
FROM Factorial
WHERE n > 1
)
SELECT F
FROM Factorial
WHERE n = 1
;
July 10, 2023 at 8:46 pm
I think you might want a recursive common table expression.
Something like this:
DECLARE @n int = 3;
WITH Factorial(F,n) AS
(
SELECT 1 AS F,
@n AS n
UNION ALL
SELECT F*n AS F,
n - 1 AS n
FROM Factorial
WHERE n > 1
)
SELECT F
FROM Factorial
WHERE n = 1
;
I could be wrong but it looks like they were already using a Recursive CTE.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 10, 2023 at 8:57 pm
Jonathan AC Roberts wrote:I think you might want a recursive common table expression.
Something like this:
DECLARE @n int = 3;
WITH Factorial(F,n) AS
(
SELECT 1 AS F,
@n AS n
UNION ALL
SELECT F*n AS F,
n - 1 AS n
FROM Factorial
WHERE n > 1
)
SELECT F
FROM Factorial
WHERE n = 1
;I could be wrong but it looks like they were already using a Recursive CTE.
Yes, but it wouldn't work with the word "RECURSIVE" in it.
July 10, 2023 at 9:06 pm
Ah... I see my confusion here. The op posted a recursive CTE where he said it wouldn't work with the word RECURSIVE and Frederico said that RECURSIVE is not an SQL option. My brain short-cut that to meaning that's the way SQL Server does things and the word RECURSIVE isn't necessary so do it that way.
Then, Ratbak posted a recursive CTE and I took that as the right way to do it but, up to your post, no one told the OP was it was called in SQL Server.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 10, 2023 at 9:13 pm
Ah... I see my confusion here. The op posted a recursive CTE where he said it wouldn't work with the word RECURSIVE and Frederico said that RECURSIVE is not an SQL option. My brain short-cut that to meaning that's the way SQL Server does things and the word RECURSIVE isn't necessary so do it that way.
Then, Ratbak posted a recursive CTE and I took that as the right way to do it but, up to your post, no one told the OP was it was called in SQL Server.
Yes, I don't think I looked at ratbak's answer. Too often I just type an answer without reading the previous responses.
July 11, 2023 at 3:04 pm
Yes, I don't think I looked at ratbak's answer. Too often I just type an answer without reading the previous responses.
Got it. I've had the same problem in the past especially if the discussion is across several pages.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply