I want to insert records by date through loop. This is what I tried.
DECLARE @str VARCHAR(500) = '1400,2001,2400,1201,1001,1302'
DECLARE @startDate datetime = '2015-01-01 00:00:00'
DECLARE @endDate datetime = '2015-01-01 23:59:59'
WHILE (@startDate<='2015-01-31 00:00:00')
BEGIN
WHILE LEN(@str) > 0
BEGIN
DECLARE @storeid VARCHAR(100)
IF CHARINDEX(',',@str) > 0
SET @storeid = SUBSTRING(@str,0,CHARINDEX(',',@str))
ELSE
BEGIN
SET @storeid = @str
SET @str = ''
END
Print @storeid
Print @startDate
SET @str = REPLACE(@str,@storeid + ',' , '')
END
SET @startDate = @startDate+1;
END
OUTPUT
1400
Jan 1 2015 12:00AM
2001
Jan 1 2015 12:00AM
2400
Jan 1 2015 12:00AM
1201
Jan 1 2015 12:00AM
1001
Jan 1 2015 12:00AM
1302
Jan 1 2015 12:00AM
But it's not working. Simply it inserts only one time. It doesn't continue. Thanks