SQL Server的存储过程怎么写?

现在有一张表,我的存储过程中要处理的就是获取那张表中的数据,求出平均值,最大值,最小值,再写到另一张表中。请问应该怎么写?

SQL server中如何存储:

首先准备数据,测试存储过程

use ssqadm;

创建测试books表

create table books_test ( book_id int identity(1,1) primary key, 

book_name varchar(20),book_price float,book_auth varchar(10));

插入测试数据

insert into books_test (book_name,book_price,book_auth)values 

('论语',25.6,'孔子'),

('天龙八部',25.6,'金庸'),

('雪山飞狐',32.7,'金庸'),

('平凡的世界',35.8,'路遥'),

('史记',54.8,'司马迁');

select * from books_test;*/

创建无参存储过程

if (exists (select * from sys.objects where name = 'getAllBooks'))

drop proc getAllBooks

go

create procedure getAllBooks

as

begin

select * from books_test;

调用,执行存储过程

exec getAllBooks;

end

go

修改存储过程

alter procedure getallbooks

as 

select book_name from books_test;

修改存储过程的名称

sp_rename getallbooks,proc_get_allbooks;

go

exec proc_get_allbooks;

go

创建带参数的存储过程

use ssqadm

go

if (exists (select * from sys.objects where name = 'searchbooks'))

drop proc searchbooks

exec searchbooks

执行存储searchbooks得到如下结果:

go

create procedure searchbooks (@bookid int)--括号里面是

as 

begin

declare  @book_id int;定义一个标量变量,只是保证存储过程的完整性,在本存储是多此一举的。

set @book_id = @bookid;

select* from books_test where book_id = @book_id;

end;

go

-- exec searchbooks  

执行存储searchbooks得到如下结果:

创建带两个参数的存储过程

use ssqadm

go

if (exists (select * from sys.objects where name = 'book_test2'))

drop proc book_test2

exec book_test2  

执行存储book_test2得到如下结果:

go

create procedure book_test2 

(@bookid int,@bookname varchar(20))括号里面是

as 

begin

declare  @book_id int;

定义一个标量变量,只是保证存储过程的完整性,在本存储是多此一举的。

declare  @book_name varchar(20);

set @book_id = @bookid;

set @book_name = @bookname;

select* from books_test where book_id =

@book_id and book_name = @book_name;

end;

go

exec book_test2  

扩展资料:

SQL Server中查询存储命令子句:

USE [SSQADM]  

Use 是跳转到哪个数据库,对这个数据库进行操作。

GO       

GO向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号,相当于提交上面的SQL语句。

GO是把t-sql语句分批次执行

(一步成功了才会执行下一步,即一步一个GO)

/****** Object:  StoredProcedure [dbo].[PROC_four_five_hr]  

Script Date: 07/30/2018 13:44:55 ******/

SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ON

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-02
第一步:点击数据库下的“可编程性”,选择“存储过程”,点击鼠标右键,选择“新建存储过程”
第二步:在create PROCEDURE 后 输入存储过程的名字,紧跟着的就是定义存储过程的参数,接下来就可以去编写自己所需要组装的存储过程语句了
第三步: 编译存储过程,在工具栏上按下执行按钮,如果没有错误,就编写成功了。
第四步:调用:在sqlserver的语句查询框中,输入exec 存储过程名 参数,执行就可以了。

基本语法格式如下:中括号带的是可选项
create proc | procedure pro_name
[{@参数数据类型} [=默认值] [output],
{@参数数据类型} [=默认值] [output],
....
]
as
begin
SQL_statements
--业务处理
end
第2个回答  2012-04-04
create proc xxx as
begin
insert into tableb(colmax,colmin,colavg)
select max(col1),min(col1),sum(col1)/count(1) from tablea;
end
--tablea 就是要取数据的表,如果有条件后面加where...
--col1就是包含数据的某栏位
--tableb就是要写入的表
--xxx是存储过程的名字追问

首先感谢一下你能这么快就回复我。再问下,如何让这个存储过程每天定时执行?还有就是我的数据是实时的,一直在变,没什么影响吧?这个col可以用“select data from DataTable where ID='Tetminal1'” 替换吗?

追答

数据库服务器都有定时器,oracle里叫job,sql server叫作业,通过sql server agent来执行

可以

第3个回答  推荐于2017-11-24
这个:
CREATE PROCEDURE p_1
AS
insert into ta ( col_max,col_min,col_avg)
select max(col1),min(col1),sum(col1)/count(1) from tb where id='Tetminal1 ;
GO
,另外,朋友,你说你一个存储过程目的何在?就是为了节省流量,在查询的时候不必把这么一大串的字符传来传去,你不直接放在数据库里面,你放哪?在vs里面建?你觉得还有必要否?那还不如直接sql算了,或者NHibernate更好。

如有冒昧,请见谅! 清明已逝,工作重启,愿阁下的生活 蒸蒸日上 心想事成...!追问

那你给我稍微介绍一下存储过程的主要用处吧。我现在是想用它来实现每天定时自动地去整理一张表中的数据到另外一张表中,不想用代码中的sql语句去实现,听人介绍可以考虑用存储过程,才用的。

本回答被网友采纳
第4个回答  推荐于2017-10-14
创建过程
CREATE PROCEDURE p_1
AS
insert into ta ( col_max,col_min,col_avg)
select max(col1),min(col1),sum(col1)/count(1) from tb where id='Tetminal1 ;
GO
--------------------------------------------------------------------------------------------------------------------
创建作业,定时执行过程

1:在“对象资源管理器”中,连接到 SQL Server 数据库引擎实例,再展开该实例。
2.展开“SQL Server 代理”,创建一个新作业或右键单击一个现有作业,再单击“属性”。
3.在“作业属性”对话框中,单击“步骤”页,再单击“新建”。
4.在“新建作业步骤”对话框中,键入作业的“步骤名称”。追问

这个创建过程要在哪里创建?我想在代码中控制这个定时过程,行吗?就是不去SQL Server数据库去新建。能在vs里面吗?

本回答被提问者采纳