存储过程

–判断存储过程是否存在
if exists(select * from sysobjects where name=’up_CCGCName’)
drop proc up_CCGCName
go
添加存储过程
–创建添加产品信息的存储过程up_AddProductInfos
create proc [dbo].[up_AddProductInfos]
@proname varchar(30),
@proprice money,
@proimage varchar(max),
@pmdid varchar(30),
@prodate datetime
as
insert into ProductInfos values(@proname,@proprice,@proimage,@pmdid,@prodate)
GO
删除存储过程
–创建删除产品信息的存储过程up_DeleteProductInfos
create proc up_DeleteProductInfos
@id int
as
delete from ProductInfos where ID=@id
GO
批量删除存储过程
–创建批量删除产品信息的存储过程up_PLDeleteProductInfos
create proc up_PLDeleteProductInfos
@ids varchar(20)
as
exec(‘delete from ProductInfos where ID in(‘+@ids+’)’)
GO
修改存储过程
–创建修改存储过程up_UpdateProductInfos
create proc [dbo].[up_UpdateProductInfos]
@pid int,
@proname varchar(30),
@proprice money,
@proimage varchar(max),
@paid int,
@prodate datetime
as
update ProductInfos set ProductName=@proname,ProductPrice=@proprice,ProductImage=@proimage,PId=@paid,ProductDate=@prodate where ID=@pid
GO

–创建显示,分页,多条件查询存储过程up_paging
create proc up_paging
@tableName varchar(8000), –表名、视图名
@indexCol varchar(50) = ‘id’, –标识列名(如:比如主键、标识,推荐使用索引列)
@pageSize int = 10, –页面大小
@pageIndex int = 0, –当前页
@orderCol varchar(100) = ‘id desc’,–排序 (如:id)
@where varchar(max) = ”, –条件
@columns varchar(500) = ‘*’ –要显示的列
as
declare @sql varchar(max)
declare @sql2 varchar(max)
declare @where2 varchar(max)

if @where <> ”
begin
select @where2 = ’ And ’ + @where
select @where = ’ Where ’ + @where
end
else
select @where2 = ”

select @sql = ‘Select Top ’ + Convert(varchar(10),@pageSize) + ’ ’ + @columns + ’

From ’ + @tableName
select @sql2 = @sql + @where
select @sql = @sql + ’ Where ’ + ‘(’ + @indexCol + ’ Not In (Select Top ’ +

Convert(varchar(10), @pageSize * @pageIndex) + ’ ’ + @indexCol + ’ From ’ +

@tableName + @where + ’ Order by ‘+ @orderCol +’))’
select @sql = @sql + @where2
select @sql = @sql + ’ Order by ’ + @orderCol
–获取数据集
exec (@sql)
PRINT @sql
select @sql2 = Replace(@sql2,’Top ’ + Convert(varchar(10), @pageSize) + ’ ’ +

@columns, ‘count(1)’)
–获取总数据条数
exec(@sql2)

GO
–创建显示,查询信息存储过程up_SelectInfos
create proc up_SelectInfos
@tableName nvarchar(max),
@condition nvarchar(max)
as
declare @sql nvarchar(max)
if @condition<>”
begin
select @sql=’select * from ’ +@tableName+ ’ where ‘+@condition
end

else
begin
select @sql=’select * from ’ +@tableName+ ”
end
exec (@sql)
print @sql

GO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值