Comparing SQL and Prolog

后端 未结 8 644
深忆病人
深忆病人 2021-01-29 18:34

I\'ve started learning Prolog and wondering about the theoretical difference from the SQL language.

For example:

  • both are declarative languages
  • b
8条回答
  •  伪装坚强ぢ
    2021-01-29 19:17

    xonix, you need more development experience to say whether something can be done in sql or not.

    Below are at least 2 solutions for your fibo series. One using Stored Procedure and the other using CTE. Take your pick.

    METHOD 1

    declare @a int, @b int, @c int, @i int, @N int = 10
    select @a=0, @b=1, @i=0, @c=0
    print @a
    print @b
    while @i < @N 
    Begin
    set @c=@a+@b
    print @c
    set @i=@i+1
    set @a=@b
    set @b=@c
    end
    

    METHOD 2

    WITH FibonacciNumbers (RecursionLevel, FibonacciNumber, NextNumber) 
    AS (
    -- Anchor member definition
    SELECT  
    0  AS RecursionLevel,
    0  AS FibonacciNumber,
    1  AS NextNumber
    UNION ALL
    -- Recursive member definition
    SELECT  a.RecursionLevel + 1             AS RecursionLevel,
    a.NextNumber                     AS FibonacciNumber,
    a.FibonacciNumber + a.NextNumber AS NextNumber
    FROM FibonacciNumbers a
    WHERE a.RecursionLevel < 10
    )
    -- Statement that executes the CTE
    SELECT 
    'F' + CAST( fn.RecursionLevel AS VARCHAR) AS FibonacciOrdinal, 
    fn.FibonacciNumber,
    fn.NextNumber
    FROM FibonacciNumbers fn; 
    GO
    

提交回复
热议问题