大家帮帮忙,看看这个存储过程怎么写。

mihi1978 2003-10-17 05:07:36
现在有一个sql 语句。
select s200 for 数据表
where [time]>='2003-8-23' and [time]<'2003-8-24' and
(s200 >100 or s200<50)
现在想改成存储过程。由于时间间隔和,数据取值范围都是变量,还有列名也是变量。我现在写成:
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper double,
@lower double,

as
select @Count =count(@colName) from 密集历史数据表
where [time]< @timeEnd and [time]>= @timeStart and ( @colname< @lower or @colName> @upper)

可是出错。请问大虾们,正确的应该怎么写。
...全文
48 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yujohny 2003-10-17
  • 打赏
  • 举报
回复
不要加,一加你语句就是错的了,代码就是下面这样,你可以试试就知道了:
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper float,
@lower float

as
exec('select @Count =count('+@colName+') from 密集历史数据表 where [time]< @timeEnd and [time]>= @timeStart and ( '+@colname+'< @lower or '+@colName+'> @upper)')
mihi1978 2003-10-17
  • 打赏
  • 举报
回复
解决了,大家说得都很对,给了我很多帮助。但是有一个问题没有考虑到。即引号问题。
@count,@timestart,@timeEnd旁边需要加三个单引号。呵呵。
谢谢大家,下面加分
HenanBoy 2003-10-17
  • 打赏
  • 举报
回复
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper float,
@lower float
as
exec('select @Count =count('+ltrim(rtirm(@colName))+') from 密集历史数据表 where [time]< @timeEnd and [time]>= @timeStart and ( '+@colname+'< @lower or '+@colName+'> @upper)')

mihi1978 2003-10-17
  • 打赏
  • 举报
回复
,这个我看到了。可是那么多参数,除了@colname好像其他的都放到引号里边了,这样是不是不对?
LoveSQL 2003-10-17
  • 打赏
  • 举报
回复
写成动态的sql语句就可以了。
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper float,
@lower float,

as
declare @sql nvarchar(8000)
set @sql='select @Count =count('+@colName+') from 密集历史数据表 where [time]< '+@timeEnd +'and [time]>='+ @timeStart+' and ( '+@colname+'< '+@lower+' or '+@colName+'>'+ @upper+')')
exec sp_executesql @sql,n'@Count int output','@Count output'
select @Count
mihi1978 2003-10-17
  • 打赏
  • 举报
回复
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper float,
@lower float
也是参数,不需要放在小引号外边吗?
yoki 2003-10-17
  • 打赏
  • 举报
回复
注意
as前多了一个","
yoki 2003-10-17
  • 打赏
  • 举报
回复
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper float,
@lower float
as
exec('select @Count =count('+@colName+') from 密集历史数据表 where [time]< @timeEnd and [time]>= @timeStart and ( '+@colname+'< @lower or '+@colName+'> @upper)')
yujohny 2003-10-17
  • 打赏
  • 举报
回复
--SQL中没有double数据类型,改为Float
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper float,
@lower float,

as
exec('select @Count =count('+@colName+') from 密集历史数据表 where [time]< @timeEnd and [time]>= @timeStart and ( '+@colname+'< @lower or '+@colName+'> @upper)')
qdubit 2003-10-17
  • 打赏
  • 举报
回复
关注!
yujohny 2003-10-17
  • 打赏
  • 举报
回复
create procedure sp_LoadExceedCount
@Count int OUTPUT,
@colName varchar(30),
@timeStart smalldatetime,
@timeEnd smalldatetime,
@upper double,
@lower double,

as
exec('select @Count =count('+@colName+') from 密集历史数据表 where [time]< @timeEnd and [time]>= @timeStart and ( '+@colname+'< @lower or '+@colName+'> @upper)')

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧