您的位置:首页 > 其它

存储过程的一些小技巧

2007-09-29 21:57 204 查看
有时可能要在存储过程中根据输入的参数,构造SQL,并执行产生结果,可能还会碰到返回参数的问题,下面的一个存储过程中就用临时表来得到记录数,并赋值给返回参数

create proc [Test_CreatSQL_EXEC]

(

@FieldName varchar(50),

@StartTime DateTime,

@EndTime datetime,

@PageSize int,

@RecordCount int output

)

as

declare @SQL varchar(1000)

create table #tb

(

rows int

)

set @sql = 'insert into #tb select count(*) from Products where DateCreated between '''+ convert(varchar(20),@StartTime) + ''' and '''+convert(varchar(20),@endTime)+''''

exec (@sql)

select @RecordCount=rows from #tb

print @sql

drop table #tb

exec ('select top '+ @PageSize + ' * from Products where DateCreated between '''+ @startTime + ''' and '''+ @endtime +'''')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: