AllInWorld99 provides a reference manual covering many aspects of web programming, including technologies such as HTML, XHTML, CSS, XML, JavaScript, PHP, ASP, SQL,FLASH, jQuery, java, for loop, switch case, if, if else, for...of, for...in, for...each,while loop, blogger tips, blogger meta tag generator, blogger tricks, blogger pagination, client side script, html code editor, javascript editor with instant output, css editor, online html editor, materialize css tutorial, materialize css dropdown list,break, continue statement, label,array, json, get day and month dropdown list using c# code, CSS button,protect cd or pendrive from virus, cordova, android example, html and css to make android app, html code play,telerik show hide column, Transparent image convertor, copy to clipboard using javascript without using any swf file, simple animation using css, SQL etc. AllInWorld99 presents thousands of code examples (accompanied with source code) which can be copied/downloaded independantly. By using the online editor provided,readers can edit the examples and execute the code experimentally.


SQL Bulk Insert in a single line

     Bulk insert is possible in SQL, every set data data's are passed inside the "()" (brackets), every set have should separate with "," (comma).

Bulk insert in sql
Example
insert into tbl1(col1,col2) values(val1,val2), (val3,val4), (val5,val6), (val7,val8);

Where to use

     In some situation we need to run loop to save the same column data row by row, so there we can use this bulk insert.
   
     In below example if you are using single insert query you need to contact server 10 times.

<script>
for(int i=0;i<10;i++)
{
 PageMethods.web_fun(i);  //Passing the value to server in asp.net, other also same problem
}
</script>

In SQL

alter proc sv_in_sql
@value int
as
begin
 insert into Tbl1(id) values(@value);
end

From the above example, the PageMethod will connect to the server 10 times and it will execute the same insert query 10 times repeatedly.

But if you are used bulk insert query just you need to connect the server only once.

<script>
var ar_variable=[];
for(int i;i<10;i++)
{
  ar_variable.push(i);
}
var strvalue= ar_variable.join("),(");
strvalue = "(" + strvalue + ")";
PageMethods.web_fun(strvalue);
</script>

In SQL

alter proc sv_in_sql
 @value varchar(max)
as
begin
  declare @strQuery varchar(max);
  set @strQuery= 'insert into Tbl1(id) values'+@TypeSetQuery
 exec(@strQuery);
 end

In the above methods, the server will call only once and all the values are insert in a single query.

ID
Name
0
NULL
1
NULL
2
NULL
3
NULL
4
NULL
5
NULL
6
NULL
7
NULL
8
NULL
9
NULL

0 comments:

Post a Comment

Total Pageviews