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

相关文章:

  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
猜你喜欢
  • 2022-01-18
  • 2021-06-21
  • 2021-11-20
  • 2021-08-19
  • 2021-07-25
  • 2022-12-23
  • 2021-08-14
相关资源
相似解决方案