

Sometimes we need to insert numbers of data(multiple) in a single table. Usually we use transaction for executing multiple sql-commands. But if we want to insert data using a single command then we can use the UNION operator to get the multiple rows a view and then insert it into our desired table.
Here I am giving a simple example to insert multiple rows form a single INSERT command.
Create table script :
CREATE TABLE tbl_test
(
id varchar(2),
email varchar(100)
)
/*a very simple table having only two column*/
Now insert multiple rows in this table using a single INSERT command :
insert into tbl_test
select 'a1', 'ras.ewu@gmail.com'
union all
select 'a2', 'Kiser01360@gmail.com'
union all
select 'a3', 'rasel_21@hotmail.com'
union all
select 'a4', 'mr_islam_2002@yahoo.com'
union all
select 'a5', 'bdsarwar@gmail.com'
union all
select 'a6', 'raj.aryans@gmail.com'
Now If the select all command executed the result will show all the 6 rows inserted in the table.
SELECT * from tbl_test
The output screen shot image is attached.
Now we can make this script generate in the upper layer, i.e: we can make the script as a string in our C# page and pas this as a parameter into stored procedure and then execute that string and easily solve our purpose.
No comments:
Post a Comment