declare @x intset @x=10if @x>100 begin print 'a' endif @x<60 begin print 'b' if @x<40 begin print 'c' if @x<30 begin print 'd' if @x<20 begin print 'e' end end end end不会有错啊。把你的代码贴出来
IF 1>2 BEGIN SELECT 2 END ELSE BEGIN SELECT 3 IF 2>3 BEGIN SELECT 4 END ELSE BEGIN SELECT 5 IF 3>4 BEGIN SELECT 6 END ELSE BEGIN SELECT 7 END END END ----------- 3(所影响的行数为 1 行) ----------- 5(所影响的行数为 1 行) ----------- 7(所影响的行数为 1 行)没错啊
还是测试中的代码 set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[test] -- Add the parameters for the stored procedure here @rno int, @date1 datetime, @date2 datetime AS BEGIN SET NOCOUNT ON;--局部变量 declare @volume float,@subvolume float, @maxnow datetime,@minstime datetime,@minnow datetime, @nextdate datetime,@pastdate datetime select rno ,status, now ,stime,volume into #temptable from psc_rain where rno=@rno and now between @date1 and @date2--temtable中没有记录时操作 if not exists(select 1 from #temptable) begin set @volume=0 --设定返回的值 select @rno as rno, @volume as volumes,CONVERT(CHAR(10),@date1,120)date end--含有记录的时候执行 if exists(select 1 from #temptable) begin set @volume=(select isnull(SUM(volume) ,0) from #temptable where status=0) --没有status=0状态时 if @volume=0 begin set @maxnow=(select max(now) from #temptable)--当天最晚的记录时间 set @volume=(select volume from #temptable where now=@maxnow)--获得降水量 set @minnow=(select min(now) from #temptable)--当天最早的记录时间 set @minstime=(select min(stime) from #temptable)--判断降水时间是否在前一天开始 if @minstime<@date1 begin set @pastdate=(select max(now) from psc_rain where rno=@rno and now between DATEADD(day,-1,@date1) and @date1)--前一天的最大时间 set @subvolume=(select volume from #temptable where now=@minnow)-(select volume from psc_rain where rno=@rno and now=@pastdate)+0.0--降水量的差值 set @volume=@volume+@subvolume*DATEDIFF(minute,@date1,@minnow)/(DATEDIFF(minute,@minnow,@pastdate)) select @rno as rno,@volume as volumes, @minstime,@pastdate,@subvolume end
【 set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录开始时间 if @minstime<@date2 begin set @nextdate=(select min(now) from psc_rain where rno=@rno and stime=@minstime
end 】--这里报的错 end select @rno as rno,@volume as volumes, @minstime
end END
【 set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录开始时间 你这句属于哪个BEGIN END,呵呵,错了
if @minstime<@date1 begin set @pastdate=(select max(now) from psc_rain where rno=@rno and now between DATEADD(day,-1,@date1) and @date1)--前一天的最大时间 set @subvolume=(select volume from #temptable where now=@minnow)-(select volume from psc_rain where rno=@rno and now=@pastdate)+0.0--降水量的差值 set @volume=@volume+@subvolume*DATEDIFF(minute,@date1,@minnow)/(DATEDIFF(minute,@minnow,@pastdate)) select @rno as rno,@volume as volumes, @minstime,@pastdate,@subvolume set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录 end 你加在上面的BEGIN END中试试
if @minstime<@date1 begin set @pastdate=(select max(now) from psc_rain where rno=@rno and now between DATEADD(day,-1,@date1) and @date1)--前一天的最大时间 set @subvolume=(select volume from #temptable where now=@minnow)-(select volume from psc_rain where rno=@rno and now=@pastdate)+0.0--降水量的差值 set @volume=@volume+@subvolume*DATEDIFF(minute,@date1,@minnow)/(DATEDIFF(minute,@minnow,@pastdate)) select @rno as rno,@volume as volumes, @minstime,@pastdate,@subvolume end ELSE BEGIN 【 set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录 END不知道楼主是不是这个意思,如果前面的条件不成立就执行你的SET 上面的是成立就执行
对的,但是不应该有else 前面先判断,在判断后面的(也就是这两个都需要判断一次的)
set @minstime =(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录开始时间应该是这句话错了,但是不知道错哪里了,,,
你常识改成
if 1=1
begin
select 1
end
报不报错呢?
关键字 'if' 附近有语法错误。
消息 156,级别 15,状态 1,过程 test,第 57 行
关键字 'end' 附近有语法错误。
declare @x intset @x=10if @x>100
begin
print 'a'
endif @x<60
begin
print 'b'
if @x<40
begin
print 'c'
if @x<30
begin
print 'd'
if @x<20
begin
print 'e'
end
end
end end不会有错啊。把你的代码贴出来
BEGIN
SELECT 2
END
ELSE
BEGIN
SELECT 3
IF 2>3
BEGIN
SELECT 4
END
ELSE
BEGIN
SELECT 5
IF 3>4
BEGIN
SELECT 6
END
ELSE
BEGIN
SELECT 7
END
END
END
-----------
3(所影响的行数为 1 行)
-----------
5(所影响的行数为 1 行)
-----------
7(所影响的行数为 1 行)没错啊
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[test]
-- Add the parameters for the stored procedure here
@rno int,
@date1 datetime,
@date2 datetime
AS
BEGIN SET NOCOUNT ON;--局部变量
declare @volume float,@subvolume float,
@maxnow datetime,@minstime datetime,@minnow datetime,
@nextdate datetime,@pastdate datetime
select rno ,status, now ,stime,volume into #temptable
from psc_rain where rno=@rno and now between @date1 and @date2--temtable中没有记录时操作
if not exists(select 1 from #temptable)
begin
set @volume=0
--设定返回的值
select @rno as rno, @volume as volumes,CONVERT(CHAR(10),@date1,120)date
end--含有记录的时候执行
if exists(select 1 from #temptable)
begin
set @volume=(select isnull(SUM(volume) ,0) from #temptable where status=0)
--没有status=0状态时
if @volume=0
begin
set @maxnow=(select max(now) from #temptable)--当天最晚的记录时间
set @volume=(select volume from #temptable where now=@maxnow)--获得降水量
set @minnow=(select min(now) from #temptable)--当天最早的记录时间
set @minstime=(select min(stime) from #temptable)--判断降水时间是否在前一天开始
if @minstime<@date1
begin
set @pastdate=(select max(now) from psc_rain where rno=@rno and now between DATEADD(day,-1,@date1) and @date1)--前一天的最大时间
set @subvolume=(select volume from #temptable where now=@minnow)-(select volume from psc_rain where rno=@rno and now=@pastdate)+0.0--降水量的差值
set @volume=@volume+@subvolume*DATEDIFF(minute,@date1,@minnow)/(DATEDIFF(minute,@minnow,@pastdate))
select @rno as rno,@volume as volumes, @minstime,@pastdate,@subvolume
end
【 set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录开始时间
if @minstime<@date2
begin
set @nextdate=(select min(now) from psc_rain where rno=@rno and stime=@minstime
end 】--这里报的错 end
select @rno as rno,@volume as volumes, @minstime
end
END
你这句属于哪个BEGIN END,呵呵,错了
begin
set @pastdate=(select max(now) from psc_rain where rno=@rno and now between DATEADD(day,-1,@date1) and @date1)--前一天的最大时间
set @subvolume=(select volume from #temptable where now=@minnow)-(select volume from psc_rain where rno=@rno and now=@pastdate)+0.0--降水量的差值
set @volume=@volume+@subvolume*DATEDIFF(minute,@date1,@minnow)/(DATEDIFF(minute,@minnow,@pastdate))
select @rno as rno,@volume as volumes, @minstime,@pastdate,@subvolume set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录
end 你加在上面的BEGIN END中试试
begin
set @pastdate=(select max(now) from psc_rain where rno=@rno and now between DATEADD(day,-1,@date1) and @date1)--前一天的最大时间
set @subvolume=(select volume from #temptable where now=@minnow)-(select volume from psc_rain where rno=@rno and now=@pastdate)+0.0--降水量的差值
set @volume=@volume+@subvolume*DATEDIFF(minute,@date1,@minnow)/(DATEDIFF(minute,@minnow,@pastdate))
select @rno as rno,@volume as volumes, @minstime,@pastdate,@subvolume
end
ELSE
BEGIN
【 set @minstime=(select min(stime) from psc_rain where rno=@rno and now between @date2 and DATEADD(day,1,@date2) --下一天中降水记录
END不知道楼主是不是这个意思,如果前面的条件不成立就执行你的SET
上面的是成立就执行
如果重写一次OK的话,,只能说明你复制到全角空白了 - -