Posts

Showing posts from October, 2024

How to Insert Data Into a Table in SQL Server

Once you have created your table, the next step is to insert data into it. The basic syntax to insert a single row is: INSERT INTO dbo.BRIAN (a, b) VALUES (1, 21); This inserts one record into the BRIAN table, setting column a to 1 and column b to 21. Inserting Multiple Rows Efficiently Instead of writing many separate INSERT statements like this: INSERT INTO dbo.BRIAN (a, b) VALUES (1, 21); INSERT INTO dbo.BRIAN (a, b) VALUES (39, 21); INSERT INTO dbo.BRIAN (a, b) VALUES (1998, 2001); -- and so on You can insert multiple rows with one statement like this: INSERT INTO dbo.BRIAN (a, b) VALUES (1, 21), (39, 21), (1998, 2001), (26, 2817), (21, 2938), (12, 21), (3939, 4848); This is cleaner, more efficient, and easier to maintain. Inserting Multiple Rows Using a Loop (T-SQL) Sometimes you might want to insert many rows dynamically, for example, inside a stored procedure or a script, or generate test data. You can do this with a loop using T-SQL'...