Bird
0
0

You want to create a function that returns the factorial of a number using recursion. Which of the following is a correct approach in SQL Server?

hard📝 Application Q8 of 15
SQL - Stored Procedures and Functions
You want to create a function that returns the factorial of a number using recursion. Which of the following is a correct approach in SQL Server?
AUse a stored procedure with OUTPUT parameter
BCreate a table-valued function that loops through numbers
CUse a scalar UDF that only returns 1
DCreate a scalar UDF that calls itself with @n-1 until @n=1
Step-by-Step Solution
Solution:
  1. Step 1: Understand recursion in scalar UDFs

    Scalar UDFs can call themselves recursively to compute factorial by reducing input until base case.
  2. Step 2: Evaluate options

    Create a scalar UDF that calls itself with @n-1 until @n=1 correctly describes recursive scalar UDF; others do not implement recursion or return factorial.
  3. Final Answer:

    Create a scalar UDF that calls itself with @n-1 until @n=1 -> Option D
  4. Quick Check:

    Recursion in scalar UDFs = Create a scalar UDF that calls itself with @n-1 until @n=1 [OK]
Quick Trick: Use recursion in scalar UDFs for factorial [OK]
Common Mistakes:
  • Confusing table-valued functions with recursion
  • Using stored procedures instead of functions
  • Returning constant values only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes