SQL While do loops
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2022 02:45 AM
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2025 05:10 AM
hi, In Databricks SQL stored procedures (Unity Catalog, DBR ≥ 17.0), the WHILE ... DO loop is supported,
equivalent code for this,
WHILE LSTART > 0 DO
SET LSTRING = CONCAT(LSTRING, VSTRING2);
END WHILE;Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2025 05:48 AM
Hi, yes it's possbile. Refer to below thread:
SQL Stored Procedure in Databricks - Databricks Community - 133371
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2025 07:01 AM
@elgeo Here are two alternatives.
1. Use a recursive CTE
WITH RECURSIVE loop_cte (lstart, lstring) AS (
SELECT 5 AS lstart, '' AS lstring
UNION ALL
SELECT lstart - 1, CONCAT(lstring, 'VSTRING2')
FROM loop_cte
WHERE lstart > 1
)
SELECT * FROM loop_cte;2. you can use pyspark. If you are running the code in notebook.
lstart = 5
lstring = ""
vstring2 = "VSTRING2"
while lstart > 0:
lstring += vstring2
lstart -= 1