SQLServer存储过程基本操作

存储过程通常被使用在重数据交互的场合,它能使服务端的处理逻辑更简单,最极端的情况是,服务端只管和数据库进行交互,而不需要根据业务逻辑对数据进行过多处理。使用存储过程的另外一个好处是方便数据测试,直接执行存储过程查看结果,就能根据结果判断业务逻辑是否正确。
查看表是否存在

if object_id(N'表',N'U') is not null   
begin
drop table integration_list
end

查看存储过程是否存在

if object_id(N'存储过程',N'P') is not null
begin
drop procedure 存储过程
end

或者

if exists(select name from sysobjects where name=N'存储过程名字' type=N'p')
drop procedure 存储过程名字 

判断空

--判断NULL
select * from table where 列名 is null
--判断空串
select * from table where 列名 = ''
--判断空串或NULL
select * from table where isnull(列名,'') = ''

返回所有的表名

select name from sysobjects where type=N'U'

返回所有存储过程的名字

select name from sysobjects where type=N'P'

例子:

use temp --使用库
if exists(select name from sysobjects where name=N'integration_details' and type=N'p')
drop procedure integration_details
go
create procedure integration_details
as

--如果存在临时表就删除重新创建
if object_id(N'#integration_top',N'U')is not null  
begin 
drop table #integration_top
end
--建立临时表
create table #integration_top
       (
        customer_id nvarchar(20),
        integration int
         )


insert into #integration_top(customer_id,integration)select top 200 cuno,sum(jifen) as integration from [xa_wechat].[dbo].[tbwx_jifen] where jftype !='活期存款积分' and jftype !='定期存款积分' group by cuno order by integration desc

select customer_id,integration from #integration_top    --积分前一百的客户

select cuno,jftype,jifen,integration from [xa_wechat].[dbo].[tbwx_jifen],#integration_top where cuno in (select customer_id from #integration_top) and cuno=customer_id and jftype !='活期存款积分' and jftype !='定期存款积分'  order by integration desc  --积分前一百客户详情
--删除临时表
drop table #integration_top

注意:SQLServer的语法和MySql有区别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值