declare @current datetime set @current = getdate() if datepart(hour, @current) / 8 = 0 begin insert a... end else if datepart(hour, @current) / 8 = 1 begin insert b... end else begin insert c... end
字符串数据类型1: declare @test char(10) set @test = 'a' /* @test = 'a ' 因为char(10)是定长的,后面补空格,补足10字节 */字符串数据类型2: declare @test varchar(10) set @test = 'a' /* @test = 'a' 因为varchar(10)是不定长的,你输入什么是什么 */
晕,CSDN把空格全给扔掉了,第一个例子中,@test = 'a' + 9个空格
我的过程是这样的 CREATE PROCEDURE insert_into_pingjun ASdeclare @riqip datetime declare @jtsfp float declare @jtzlp float declare @jtsjzlp float declare @a datetime declare @b datetime declare @xiaoshi varchar(5)set @a=(select getdate())――系统时间 set @b=(select dateadd(hh,-8,getdate()))――系统小时之前的8个小时 set @riqip=(select convert(char(10),getdate(),120)) set @jtsfp=(select avg(jtsf) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)--计算在这点之前的8个小时的平均值 set @ jtzlp=(select avg(jtzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b) set @jtsjzlp=(select avg(jtsjzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)begin insert into pingjun values (@riqip,@xiaoshi,1,@jtsfp,@jtzlp,@jtsjzlp) end go 这个过程是求8个小时的平均值,一天求3次,0-8点的平均值我要将字母a附给@xiaoshi,8-16点的我要将字母b附给@xiaoshi,16-24点的平均值我要将字母c附给@xiaoshi。
字符串数据类型3: declare @test nchar(10) set @test = 'a' /* @test = 'a ' + 9个空格 因为nchar(10)是定长的,后面补空格,补足10个字符 */字符串数据类型2: declare @test nvarchar(10) set @test = 'a' /* @test = 'a' 因为nvarchar(10)是不定长的,你输入什么是什么 */
----------------------------测试用表 create table #a (a int,b varchar(2)) create table #b (a int,b varchar(2)) create table #c (a int,b varchar(2)) ---------------------------------------开始 create proc protest @a int,@b varchar(2) as declare @tb varchar(10) select @tb=(case when DATEPART(hh,getdate()) between 0 and 7 then '#a' when DATEPART(hh,getdate()) between 8 and 15 then '#b' else '#c' end) exec('insert '+@tb+' select '+@a+','''+@b+'''') ------------------------------结 exec protest 1,'abc'--调用 -------------------------------验证 select * from #a select * from #b select * from #c 用varchar比较好,不用考虑空格,空间上也好控制(例中abc只保留了ab)
看看我这样 写对不对 CREATE PROCEDURE insert_into_pingjun ASdeclare @riqip datetime declare @jtsfp float declare @jtzlp float declare @jtsjzlp float declare @a datetime declare @b datetime declare @xiaoshi varchar(5) declare @pingjun varchar(5)set @a=(select getdate())――系统时间 set @b=(select dateadd(hh,-8,getdate()))――系统小时之前的8个小时 set @riqip=(select convert(char(10),getdate(),120)) set @xiaoshi=substring((select convert(char(20),getdate(),120)),12,2)――取小时 if @xiaoshi=08 set @pingjun ='a' if @xiaoshi=16 set @pingjun ='b' if @xiaoshi=24 set @pingjun ='c' set @jtsfp=(select avg(jtsf) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)--计算在这点之前的8个小时的平均值 set @ jtzlp=(select avg(jtzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b) set @jtsjzlp=(select avg(jtsjzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)begin insert into pingjun values (@riqip,@pingjun,1,@jtsfp,@jtzlp,@jtsjzlp) end go
你这个存储过程,依赖Job调度,万一被人修改了调度,你的数据还准吗?
set @xiaoshi=substring((select convert(char(20),getdate(),120)),12,2)――取小时 if @xiaoshi=08 set @pingjun ='a' if @xiaoshi=16 set @pingjun ='b' if @xiaoshi=24 set @pingjun ='c' 这样可以实现将a,b,c存入表中吗?
取系统如果当前时间中的小时可以用 datepart(hh,GETDATE()),所以写起来就很简单了什么样的数据类型能存储英文字母? ------------------------------------ char varchar 都能存 定义一个表,存放字母 create table [表](abc char(1)) GO--下面开始判断 DECLARE @hour int SET @hour = datepart(hh,GETDATE())if (@hour >= 0 OR (@hour <= 8) --0点到8点 BEGIN insert into [表] values('a') --将a存入表中 END else if (@hour > 8 OR (@hour <= 16) --8点到16点 BEGIN insert into [表] values('b') --将b存入表中 END else --16点到24点 BEGIN insert into [表] values('c') --将c存入表中 END--查看数据 select * from [表]
-- 我改了一下,修正了一些内容 CREATE PROCEDURE insert_into_pingjun AS SET NOCOUNT ON declare @riqip datetime declare @jtsfp float declare @jtzlp float declare @jtsjzlp float declare @a datetime declare @b datetime declare @xiaoshi varchar(5)set @a=getdate()--系统时间 -- 00:00至08:00执行的这一次,统计昨天16:00至00:00的数据: if datepart(hour, @a) / 8 = 0 begin set @a = convert(varchar(10), getdate(), 120) + ' 00:00.000'--设置为绝对0点 /* 既然是昨天的数据,@riqip应该是昨天,你原来@riqip应该有问题——你的调度应该是:每天->间隔8小时->开始时间00:00,开始时间不可能是08:00,如果08:00开始,那么每天只调度了2次,不是3次。 */ set @riqip=dateadd(day, -1, @a) set @xiaoshi = 'c' /* 这里可以判断一下pingjun,如果有日期=@riqip and xiaoshi = 'c'的数据,证明已经做过了,不需要重复,直接return,我不知道pingjun的字段名,就不写了。严格执行调度当然不会有问题,但如果我在查询分析器运行10次,那就有10条重复的数据,总得考虑周详一些。下同。 */ end -- 08:00至16:00执行的这一次,统计昨天00:00至08:00的数据: else if datepart(hour, @a) / 8 = 1 begin set @a = convert(varchar(10), getdate(), 120) + ' 08:00.000'--设置为绝对8点 set @riqip=convert(char(10),getdate(),120) set @xiaoshi = 'a' end -- 16:00至00:00执行的这一次,统计昨天08:00至16:00的数据: else begin set @a = convert(varchar(10), getdate(), 120) + ' 16:00.000'--设置为绝对16点 set @riqip=convert(char(10),getdate(),120) set @xiaoshi = 'b' end /* 为什么要取绝对正点,因为即使你00:00启动Job,getdate()并不等于'00:00:00.000',而是有一定滞后,根据SQL的忙与空闲,可能滞后时间不同,导致有的重复参与AVG,有的没有AVG。 */ set @b=(select dateadd(hh,-8,getdate())) --set @riqip=convert(char(10),getdate(),120) select @jtsfp = avg(jtsf) from ceshi where guanhao = 1 and tmdt < @a and tmdt >= @b select @jtzlp = avg(jtzl) from ceshi where guanhao = 1 and tmdt < @a and tmdt >= @b select @jtsjzlp = avg(jtsjzl) from ceshi where guanhao = 1 and tmdt < @a and tmdt >= @b /* 不需要set ??=(select ...) 直接set或select就可以了 @a和@b,总得有一边要用>=,要不然会漏掉整点的数据,虽然可能N年都不出现一条绝对整点的数据。 */--begin 这里不需要begin insert into pingjun values (@riqip,@xiaoshi,1,@jtsfp,@jtzlp,@jtsjzlp) --end 这里不需要end SET NOCOUNT OFF go
set @current = getdate()
if datepart(hour, @current) / 8 = 0
begin
insert a...
end
else if datepart(hour, @current) / 8 = 1
begin
insert b...
end
else
begin
insert c...
end
declare @test char(10)
set @test = 'a'
/*
@test = 'a '
因为char(10)是定长的,后面补空格,补足10字节
*/字符串数据类型2:
declare @test varchar(10)
set @test = 'a'
/*
@test = 'a'
因为varchar(10)是不定长的,你输入什么是什么
*/
CREATE PROCEDURE insert_into_pingjun ASdeclare @riqip datetime
declare @jtsfp float
declare @jtzlp float
declare @jtsjzlp float
declare @a datetime
declare @b datetime
declare @xiaoshi varchar(5)set @a=(select getdate())――系统时间
set @b=(select dateadd(hh,-8,getdate()))――系统小时之前的8个小时
set @riqip=(select convert(char(10),getdate(),120))
set @jtsfp=(select avg(jtsf) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)--计算在这点之前的8个小时的平均值
set @ jtzlp=(select avg(jtzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)
set @jtsjzlp=(select avg(jtsjzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)begin
insert into pingjun values (@riqip,@xiaoshi,1,@jtsfp,@jtzlp,@jtsjzlp)
end
go
这个过程是求8个小时的平均值,一天求3次,0-8点的平均值我要将字母a附给@xiaoshi,8-16点的我要将字母b附给@xiaoshi,16-24点的平均值我要将字母c附给@xiaoshi。
declare @test nchar(10)
set @test = 'a'
/*
@test = 'a ' + 9个空格
因为nchar(10)是定长的,后面补空格,补足10个字符
*/字符串数据类型2:
declare @test nvarchar(10)
set @test = 'a'
/*
@test = 'a'
因为nvarchar(10)是不定长的,你输入什么是什么
*/
create table #a (a int,b varchar(2))
create table #b (a int,b varchar(2))
create table #c (a int,b varchar(2))
---------------------------------------开始
create proc protest @a int,@b varchar(2) as
declare @tb varchar(10)
select @tb=(case when DATEPART(hh,getdate()) between 0 and 7 then '#a' when DATEPART(hh,getdate()) between 8 and 15 then '#b' else '#c' end)
exec('insert '+@tb+' select '+@a+','''+@b+'''')
------------------------------结
exec protest 1,'abc'--调用
-------------------------------验证
select * from #a
select * from #b
select * from #c
用varchar比较好,不用考虑空格,空间上也好控制(例中abc只保留了ab)
CREATE PROCEDURE insert_into_pingjun ASdeclare @riqip datetime
declare @jtsfp float
declare @jtzlp float
declare @jtsjzlp float
declare @a datetime
declare @b datetime
declare @xiaoshi varchar(5)
declare @pingjun varchar(5)set @a=(select getdate())――系统时间
set @b=(select dateadd(hh,-8,getdate()))――系统小时之前的8个小时
set @riqip=(select convert(char(10),getdate(),120))
set @xiaoshi=substring((select convert(char(20),getdate(),120)),12,2)――取小时
if @xiaoshi=08
set @pingjun ='a'
if @xiaoshi=16
set @pingjun ='b'
if @xiaoshi=24
set @pingjun ='c'
set @jtsfp=(select avg(jtsf) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)--计算在这点之前的8个小时的平均值
set @ jtzlp=(select avg(jtzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)
set @jtsjzlp=(select avg(jtsjzl) from ceshi where guanhao=1 and tmdt < @a and tmdt>@b)begin
insert into pingjun values (@riqip,@pingjun,1,@jtsfp,@jtzlp,@jtsjzlp)
end
go
if @xiaoshi=08
set @pingjun ='a'
if @xiaoshi=16
set @pingjun ='b'
if @xiaoshi=24
set @pingjun ='c'
这样可以实现将a,b,c存入表中吗?
------------------------------------
char varchar 都能存
定义一个表,存放字母
create table [表](abc char(1))
GO--下面开始判断
DECLARE @hour int
SET @hour = datepart(hh,GETDATE())if (@hour >= 0 OR (@hour <= 8) --0点到8点
BEGIN
insert into [表] values('a') --将a存入表中
END
else if (@hour > 8 OR (@hour <= 16) --8点到16点
BEGIN
insert into [表] values('b') --将b存入表中
END
else --16点到24点
BEGIN
insert into [表] values('c') --将c存入表中
END--查看数据
select * from [表]
CREATE PROCEDURE insert_into_pingjun AS
SET NOCOUNT ON
declare @riqip datetime
declare @jtsfp float
declare @jtzlp float
declare @jtsjzlp float
declare @a datetime
declare @b datetime
declare @xiaoshi varchar(5)set @a=getdate()--系统时间
-- 00:00至08:00执行的这一次,统计昨天16:00至00:00的数据:
if datepart(hour, @a) / 8 = 0
begin
set @a = convert(varchar(10), getdate(), 120) + ' 00:00.000'--设置为绝对0点
/*
既然是昨天的数据,@riqip应该是昨天,你原来@riqip应该有问题——你的调度应该是:每天->间隔8小时->开始时间00:00,开始时间不可能是08:00,如果08:00开始,那么每天只调度了2次,不是3次。
*/
set @riqip=dateadd(day, -1, @a)
set @xiaoshi = 'c'
/*
这里可以判断一下pingjun,如果有日期=@riqip and xiaoshi = 'c'的数据,证明已经做过了,不需要重复,直接return,我不知道pingjun的字段名,就不写了。严格执行调度当然不会有问题,但如果我在查询分析器运行10次,那就有10条重复的数据,总得考虑周详一些。下同。
*/
end
-- 08:00至16:00执行的这一次,统计昨天00:00至08:00的数据:
else if datepart(hour, @a) / 8 = 1
begin
set @a = convert(varchar(10), getdate(), 120) + ' 08:00.000'--设置为绝对8点
set @riqip=convert(char(10),getdate(),120)
set @xiaoshi = 'a'
end
-- 16:00至00:00执行的这一次,统计昨天08:00至16:00的数据:
else
begin
set @a = convert(varchar(10), getdate(), 120) + ' 16:00.000'--设置为绝对16点
set @riqip=convert(char(10),getdate(),120)
set @xiaoshi = 'b'
end
/*
为什么要取绝对正点,因为即使你00:00启动Job,getdate()并不等于'00:00:00.000',而是有一定滞后,根据SQL的忙与空闲,可能滞后时间不同,导致有的重复参与AVG,有的没有AVG。
*/
set @b=(select dateadd(hh,-8,getdate()))
--set @riqip=convert(char(10),getdate(),120)
select @jtsfp = avg(jtsf) from ceshi where guanhao = 1 and tmdt < @a and tmdt >= @b
select @jtzlp = avg(jtzl) from ceshi where guanhao = 1 and tmdt < @a and tmdt >= @b
select @jtsjzlp = avg(jtsjzl) from ceshi where guanhao = 1 and tmdt < @a and tmdt >= @b
/*
不需要set ??=(select ...)
直接set或select就可以了
@a和@b,总得有一边要用>=,要不然会漏掉整点的数据,虽然可能N年都不出现一条绝对整点的数据。
*/--begin 这里不需要begin
insert into pingjun values (@riqip,@xiaoshi,1,@jtsfp,@jtzlp,@jtsjzlp)
--end 这里不需要end
SET NOCOUNT OFF
go
set @a = convert(varchar(10), getdate(), 120) + ' 08:00:00.000'--设置为绝对8点
set @a = convert(varchar(10), getdate(), 120) + ' 16:00:00.000'--设置为绝对16点