sql 动态存储过程

 

分页存储过程:

CREATE PROCEDURE [dbo].[getData]
-- Add the parameters for the stored procedure here
@tb varchar(150),
@status varchar(800),
@indexpage int,
@endpage int
AS
declare @str varchar(5000)
if @status is null
BEGIN

set @str=‘SELECT * FROM (SELECT ROW_NUMBER() OVER (order by T.ID desc)AS Row, T.* from ‘+@tb+‘ T where ‘+@status+‘ ) TT WHERE TT.Row between ‘+str(@indexpage)+‘ and ‘+str(@endpage)
exec(@str)
END
else
BEGIN
set @str=‘SELECT * FROM (SELECT ROW_NUMBER() OVER (order by T.ID desc)AS Row, T.* from ‘+@tb+‘ T where ‘+@status+‘ ) TT WHERE TT.Row between ‘+str(@indexpage)+‘ and ‘+str(@endpage)
exec(@str)
END

 

 

CREATE PROCEDURE [dbo].[getExport]
@status varchar(5000),
@tb varchar(80),
@s nchar(2)
AS
declare @sql varchar(5000)
if @status is null
BEGIN
set @sql=‘select * from ‘+@tb+‘‘
exec(@sql)
END
else
BEGIN
if @tb=‘OutPutComponet_View‘
if @s=‘入库‘
set @sql=‘select t.ComponentName as 配件名称, t.TypeName as 类型,t.Unit1 as 单位,t.Count as 数量,t.Price as 价格,(t.Count*t.Price) as 总金额,t.Supplier1 as 供应商,t.IsOld as 是否旧件,t.WareHouse1 as 仓库,t.AddDate as ‘+@s+‘ from ‘+@tb+‘ as t where ‘+@status+‘‘
else
set @sql=‘select t.ComponentName as 配件名称, t.TypeName as 类型,t.Unit1 as 单位,t.Count as 数量,t.Price as 价格,(t.Count*t.Price) as 总金额,t.Company as 借用公司,t.IsOld as 是否旧件,t.WareHouse1 as 仓库,t.AddDate as ‘+@s+‘ from ‘+@tb+‘ as t where ‘+@status+‘‘
else if @tb=‘BorrowDetail_View‘
if @s=‘借出‘
set @sql=‘select t.ComponentName as 配件名称,t.TypeName as 类型,t.Unit1 as 单位,t.Count as 数量,t.Price as 价格,t.Borrower 借用人,t.Company 借用公司,t.Department 借用部门,t.IsOld 是否旧件,t.AddDate as ‘+@s+‘ from ‘+@tb+‘ as t where ‘+@status+‘‘
else
set @sql=‘select t.ComponentName as 配件名称,t.TypeName as 类型,t.Unit1 as 单位,t.Count as 数量,t.Price as 价格,t.Borrower 归还人,t.Company 归还公司,t.Department 归还部门,t.IsOld 是否旧件,t.AddDate as ‘+@s+‘ from ‘+@tb+‘ as t where ‘+@status+‘‘
else set @sql=‘select * from ‘+@tb+‘‘
exec(@sql)

END

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。