I have a temp table with CustomerNumber data. I want to loop through these values and insert this and some additional data into another table. The following is my code.
CREATE TABLE #Temp1 (CustomerNumber BIGINT)
INSERT INTO #Temp1(CustomerNumber)
SELECT DISTINCT CustomerNumber
FROM PURCHASE_HISTORY
WHERE ORDERTOTAL > 100000
--inserted this value to be used in the while loop condition
INSERT INTO #Temp1 (CustomerNumber) values(9999999999)
WHILE (SELECT CustomerNumber FROM #Temp1 ) < 9999999999
BEGIN
INSERT INTO SELECTED_CUSTOMER (CustomerID, DATE) VALUES(CustomerNumber , GETDATE())
END
DROP TABLE #Temp1
The line containing the while statement throws the following error: “Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.”
I was following the sample code given here: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/while-transact-sql?view=sql-server-ver16