First of all, I would like to say that I am 100% in agreement with John Saunders that you should avoid loops in SQL in most cases, especially in production.
But sometimes, as one time, to fill out a table with hundreds of records for testing purposes, IMHO it's just OK to allow yourself to use a loop.
For example, in your case, to fill out a table using records with hospital identifiers from 16 to 100 and make individual letters and descriptions different, which you could use
CREATE PROCEDURE populateHospitals AS DECLARE @hid INT; SET @hid=16; WHILE @hid < 100 BEGIN INSERT hospitals ([Hospital ID], Email, Description) VALUES(@hid, 'user' + LTRIM(STR(@hid)) + '@mail.com', 'Sample Description' + LTRIM(STR(@hid))); SET @hid = @hid + 1; END
And the result will be
ID Hospital ID Email Description ---- ----------- ---------------- --------------------- 1 16 user16@mail.com Sample Description16 2 17 user17@mail.com Sample Description17 ... 84 99 user99@mail.com Sample Description99
source share