135-1821-9792

sqlServer根据月份循环查询结果信息

-- 1.定义变量
declare @n int,@dateTime varchar(255),@ymd varchar(255)
-- 2.设置变量
set @n=1
set @dateTime='2016-'
-- 3.设置循环1次数
while @n<=12
begin
        -- 4.判断月份(convert(varchar,@n)是将int转换成varchar类型)
        if @n <10
                begin
                        set @ymd = @dateTime+'0'+convert(varchar,@n)+'%'
                end
     else
                begin
                        set @ymd = @dateTime+convert(varchar,@n)+'%'
                end
        -- 5.循环标志+1
        set @n = @n+1
        -- 6.打印变量值
        print(@ymd)
        -- 7.执行sql语句
        select * from user where addTime like @ymd;
end

本文名称:sqlServer根据月份循环查询结果信息
转载源于:http://wtcwzsj.com/article/pdpoio.html

其他资讯



Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号