Detailed SQL SERVER stored procedures and related examples

SQL SERVER stored procedure

** relative advantages of view (Why use stored procedures): ** Sql Server in view of a simple Select query to resolve complex queries many times, but the view can not provide the business logic functions, and stored procedures. ** What is a stored procedure: **
  • Stored procedure (Procedure) is set in order to complete a specific function Sql statement set, corresponds to the method in C #,Compiled only once, after compiled stored in the database, Required by the user can be given the name of the stored procedure parameters established by the Executive
  • Stored procedure may comprise logic control statements and data manipulation statements, it can receive parameters, output parameters, returns a single, a plurality of result sets and return values.
  • It is because the stored procedure compilation only once, so it should quickly than a single Sql statement block, so to some extent, reduce network traffic, reduce the network load.

Stored procedures advantages:

Modular programming Write a stored procedure multiple times from different parts of the application to call, re-use
performance Stored procedures provide faster code execution, reducing network traffic burdens.
Safety Users do not need to write any Sql statement to execute a stored procedure to prevent the Sql Injection Attacks
Maintainability A set of requirements change, modify the stored procedure can be repeated calls again

Stored Procedures Disadvantages:

No portability Internal programming syntax of each database are not the same when your system is compatible with multiple databases need best not to use stored procedures.
High learning costs, DBA usually good at writing stored procedures, but not every programmer can write stored procedures, unless your team has more developers familiar with writing stored procedures, or post-system maintenance can cause problems. Stored procedures have complex calculations, or complex operations too much, then, will increase the burden on the database is running.

SQL SERVER write a stored procedure:

CREATE PROC MyPage
(
@name nvarchar(10),
@page decimal output
)
AS
BEGIN
select * from students
END

Execute stored procedures:

declare @p decimal--创建Sql变量
declare @n nvarchar(5)
set @n='张三'--为Sql变量赋值
EXEC MyPage @n,@p out--调用存储过程
select @p

Modify (delete) the stored procedure

alter proc proc_name
as
begin
  --sql语句
end  
--删除存储过程
drop proc proc_name

(Relevant examples) stored paging procedure

--存储过程分页
alter PROC NesList
(
@a int,--第几页
@b int,--每页行数
@sum int output,--总行数
@str int output,--总页数
@result nvarchar(100) output--结果
)
AS
BEGIN 

--总行数
Select @sum=count(*) from NewsListTable
--总页数
set @str=CEILING(@sum*1.0/@b)
if(@a<0 or @a>@str)
begin
set @result='页码错误!'
end
--分页数据
select t.NewsId as '新闻编号',t.NewsTitle as '新闻类别',t.NewsDate  as '新闻发布时间' from 
( SELECT  *,RN=ROW_NUMBER() OVER(order by NewsId) FROM NewsListTable) as t where t.RN between ((@a-1)*@b)+1 and @a*@b --between and 包括两边值

END


-------测试存储过程
--测试
declare @nub int
set @nub=-1
declare @nm int
set @nm=2
declare @countsum int
declare @countye int
declare @re nvarchar(100)
exec NesList @nub,@nm,@countsum out,@countye out,@re out
select @countsum,@countye,@re
  • VS call a stored procedure to implement paging:
 private int a = 1;//第几页
 private int b = 7;//每页数据行数
 private int sum;//总数据行数
 private int str;//总页数
 //利用SqlDataAdapter进行数据读取绑定
            string connstr = "*****************";
            DataTable u = new DataTable();
            using (SqlDataAdapter con = new SqlDataAdapter("USP_Students", connstr))
            {
                con.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter[] pms = {
                        new SqlParameter ("@A",SqlDbType.Int) {Value=a },
                        new SqlParameter ("@B",SqlDbType.Int) { Value=b},
                        new SqlParameter ("@SUM",SqlDbType.Int) { Direction=ParameterDirection.Output},
                        new SqlParameter ("STR",SqlDbType.Int) { Direction=ParameterDirection.Output}
                    };
                con.SelectCommand.Parameters.AddRange(pms);
                con.Fill(u);
                lblsum.Text = pms[2].Value.ToString();//获取输出参数
                lblye.Text = pms[3].Value.ToString();
                label1.Text = a.ToString();

                this.DGVI.DataSource = u;
            }
Published 21 original articles · won praise 3 · Views 349

Guess you like

Origin blog.csdn.net/MrLsss/article/details/104082972