如何将 select top 4 id from table1 赋值 给 declare @id1 int,@id2 int,@id3 int,@id4 int
declare @id1 int,@id2 int,@id3 int,@id4 int
declare @sickcode varchar(20),@sfrq datetime ,@count int,@str varchar(200)
select @sickcode = sickcode,@sfrq =sfrq from tablenamewhere objid=@objid
select @count=COUNT(*) from tablename
where sickcode=@sickcode and YEAR (sfrq)= YEAR(@sfrq) if @count>4
begin
select @str=@str+cast(ta.[objid] as varchar(10))+',' from(
--select ta.[objid] a from(
select top 4 [objid] ,sfrq from tablename
where sickcode=@sickcode and YEAR (sfrq)= YEAR(@sfrq)
order by abs(datediff(day,sfrq,@sfrq)) asc ) ta order by ta.sfrq
end
else
begin
select @str=@str+cast(tb.[objid] as varchar(10))+',' from(
select [objid],sfrq from tablename where sickcode=@sickcode and YEAR (sfrq)= YEAR(@sfrq)
) tb order by tb.sfrq
end set @str=substring(@str,1,len(@str)-1)
select tab1.a ,IDENTITY(int,1,1) xh into #temp1
from cwf.dbo.fn_split(@str,',') tab1 select @id1=a from #temp1 where xh=1
select @id2=a from #temp1 where xh=2
select @id3=a from #temp1 where xh=3
select @id4=a from #temp1 where xh=4
===========================
拼接字符串。
declare @str varchar(200)
set @str=''
select @str=@str+cast(tb.[objid] as varchar(10))+',' from(
select [objid],sfrq from tablename ) tb order by tb.sfrq
print @str 分割字符串
create FUNCTION [dbo].[fn_split] ( @inputstr varchar(8000), @seprator varchar(10) )
returns @temp table (a varchar(200))
as begin
declare @i int set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator, @inputstr) while @i >= 1
begin
insert @temp values(left(@inputstr, @i - 1)) set @inputstr = substring(@inputstr, @i +1, len(@inputstr) - @i)
set @i = charindex(@seprator, @inputstr)
end if @inputstr <> ''
insert @temp values(@inputstr) return
end
如何将 select top 4 id from table1 赋值 给 declare @id1 int,@id2 int,@id3 int,@id4 int的更多相关文章
- MySQL中如何实现select top n ----Limit
Mysql中limit的用法详解 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. LIMIT 子句可以被用于强制 S ...
- “取出数据表中第10条到第20条记录”的sql语句+select top 使用方法
1.首先.select top使用方法: 參考问题 select top n * from和select * from的差别 select * from table -- 取全部数据.返回无序集合 ...
- SELECT TOP 1 比不加TOP 1 慢的原因分析以及SELECT TOP 1语句执行计划预估原理
本文出处:http://www.cnblogs.com/wy123/p/6082338.html 现实中遇到过到这么一种情况: 在某些特殊场景下:进行查询的时候,加了TOP 1比不加TOP 1要慢(而 ...
- Select Top在七种数据库中的使用方法(包含mysql)
1. Oracle数据库 SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. Infomix数据库 SELECT FIRST N * FROM TABLE1 3. D ...
- Select Top在不同数据库中的使用
1. oracle数据库 SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. Infomix数据库 SELECT FIRST N * FROM TABLE1 3. D ...
- MySQL中如何实现select top n
用惯了access mssql server的朋友,可能在用mysql查询前N条记录时,习惯的使用select top n 形式的语句,在这里说明一下,mysql没有此语法,mysql用limit来实 ...
- Oracle学习笔记:实现select top N的方法
由于Oracle不支持select top N语句,所以在Oracle中需要利用order by和rownum的组合来实现select top N的查询. rownum是记录表中数据编号的一个隐藏字段 ...
- 【转】ibatis 中使用select top #pagesize# * from tablename
ibatis中使用select top #num# * from tableName出现错误.由于初次用ibatis还不知道在它里边拼写SQL语句的一些规则,导致一些自认为很平常的SQL语句,在它这里 ...
- SQL SELECT TOP, LIMIT, ROWNUM 子句
SQL SELECT TOP, LIMIT, ROWNUM 子句 SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP 子句对于拥有数千条记 ...
随机推荐
- Ioc容器Autofac系列(3)-- 三种注册组件的方式
简单来说,所谓注册组件,就是注册类并映射为接口,然后根据接口获取对应类,Autofac将被注册的类称为组件. 虽然可像上篇提到的一次性注册程序集中所有类,但AutoFac使用最多的还是单个注册.这种注 ...
- IIS下的身份验证方式管理
设置.查看身份验证方式 #导航到某站点下: cd IIS:\Sites\DemoSite\DemoApp #启用站点test01下的Windows身份验证 Set-WebConfigurationPr ...
- cmp排序hdoj 1106排序
上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下cmp排序 /*标题还是比拟的水吧,但是花的时间还是比拟的多,心不够静*/ #include <iostrea ...
- cocos2dx 3.0 触摸机制
在cocos2dx 3.0版本号中,废弃了以往2.x版本号的写法,我们先来看一下Layer.h中的一段代码 /* Callback function should not be deprecated, ...
- C#实现对Word文件读写[转]
手头上的一个项目报表相对比较简单,所以报表打印采用VBA引擎,通过定制Word模版,然后根据模版需要填充数据,然后OK,打印即可. 实现方法:首先需要引用VBA组建,我用的是Office2003 Pr ...
- 怎样在osg中动态的设置drawable的最近最远裁剪面
// draw callback that will tweak the far clipping plane just // before rendering a drawable. s ...
- C++ CheckBox_Porerty
主题 1. s Caption属性 CheckBox CheckDlgButton BOOL CheckDlgButton( HWND hDlg, // handl ...
- 【VBA研究】怎样将单元格数据赋给数组
作者:iamlaosong 将工作表中的数据赋给数组或者将数组的数据赋给工作表,一般有两种.一种是循环的方法,一个一个的传,这样的方法一般用于须要对每一个数据特别处理的场合,还有一种是一次性用赋值语句 ...
- Servlet---JavaWeb技术的核心基础,JavaWeb框架的基石(一)
初学JavaWeb开发,请远离各种框架,从Servlet开始. Web框架是开发者在使用某种语言编写Web应用服务端是关于架构的最佳实践.很多Web框架是从实际的Web项目抽取出来的, ...
- MySQL 高可用MHA安装部署以及故障转移详细资料汇总 转
http://blog.itpub.net/26230597/cid-87082-list-2/ 1,简介 .1mha简介 MHA,即MasterHigh Availability Manager a ...