存储过程的一些小技巧

有时可能要在存储过程中根据输入的参数,构造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 +'''')

posted @ 2007-09-29 21:57  杰客  阅读(192)  评论(0)    收藏  举报