sql中的游标
declare @id varchar(10)
declare @nm varchar(10)
declare @table as table(emp varchar(10),empname varchar(10))
declare CurEmp cursor for select top 6 empid,empname from employee
open CurEmp
fetch next from CurEmp into @id,@nm
While @@fetch_status=0
begin
insert @table(emp,empname) values(@id,@nm)
fetch next from CurEmp into @id,@nm
end
close CurEmp
deallocate CurEmp
select * from @table
如果需要从某一结果集中逐一地读取一条记录,可以用游标实现,上面是个简单的例子
相关文档:
一、这是这个系列的最后一节了,自定义复杂数据类型目前只能通过CLR来实现。为了在 SQL Server 中运行,您的 UDT 必须实现 UDT 定义中的以下要求:
1.该 UDT 必须指定 Microsoft.SqlServer.Server.SqlUserDefinedTypeAttribute。System.SerializableAttribute 可选用,但建议使用。
2.UDT 必须通过创建公共的 static(M ......
SQL中的时间函数非常有用,特别是在我们进行初始赋值、复杂查询的时候,就显得特别方便。
1、获得系统当前时间
select getdate()
2、DateName ( datepart , date )返回表示指定日期的指定日期部分的字符串。
--今天是2009-2-24--星期二
SELECT DATENAME(year, getdate()) AS 'Year&nbs ......
select case when c.colid=1 then object_name(c.id) else '' end as 表名
,c.name as 字段名
,t.name 数据类型
,c.prec as 长度 ......
第一部分
单表查询
例一:查询全体学生的学号与姓名
SELECT Sno,Sname
from Student;
例二:查询全体学生的姓名、学号、所在系
SELECT Sname,Sno,Sdept
from Student;
例三:查询全体学生的详细记录
SELECT *
from Student;
等价于:
SELECT *
from Student;
例四:查询全体学生的姓名及其出生年份
......
where 1=1有什么用?在SQL语言中,写这么一句话就跟没写一样。
select * from table1 where 1=1与select * from table1完全没有区别,甚至还有其他许多写法,1<>2,'a'='a','a'<>'b',其目的就只有一个,where的条件为永真,得到的结果就是未加约束条件的。
在SQL注入时会用到这个,例如select * from table1 ......