I have this code. Can I insert values into a temp table multiple times? I have list of ids (ie 1000 ids). I want to insert this by loop.
DECLARE @tempTable TABLE(col1 INT)
FOR (@ids IS NOT NULL) {
INSERT INTO @tempTable VALUES(@ids)
}
SELECT * FROM @tempTable
Even though it is possible using WHILE
loop I will suggest you to go with SET Based approach
Why cant you just do this
INSERT INTO @tempTable (ID)
select Id from yourlist
SQL Server is not support FOR loop. You need to use WHILE loop instead.
You can use a while statement as said Jitendra Tiwari:
DECLARE @tempTable TABLE(col1 INT)
DECLARE @num int
SET @num = 1
WHILE @num <= 1000
BEGIN
INSERT INTO @tempTable VALUES (@num) --(@ids)
SET @num = @num + 1
END
SELECT * FROM @tempTable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.