Pages

Tuesday, May 12, 2009

Generate and Insert the data into a table in SQL Server

Today, I would like to give you a very simple tip to generate and insert the data into a table in few seconds.

Let’s create a table:

--Create demo table
CREATE TABLE Employee
(Emp_ID INT IDENTITY(1,1),
Name VARCHAR(30),
Date_Of_Join DATETIME,
Dept_id INT
)


Lets write simple code to generate and insert the data using WHILE loop.

DECLARE @i INT
SET @i = 10000
WHILE @i > 0
BEGIN
INSERT INTO Employee(Name,Date_Of_Join,Dept_id)
VALUES ('TIM' + CAST(@i as varchar),GETDATE(),@i%10)
SET @i = @i - 1
END

Explanation:
1) Created a variable and assigned value 10000.
2) Used while to loop insert the data.

That’s it :)

No comments: