已测试过的语句,希望能方便大家使用
- select * into student2 from student where 1<>1 --复制表(只复制结构,源student,新表名student2
- select * into student5 from student where 11=1 ---连同数据全部复制
- insert into student2(name,class) select class,name from student
- insert into student2(sex) select class from student
- insert into student2(sex) select id from student
- --以上三条总结 insert into B(a,b,c)select d,e,f from A 拷贝表数据
- --外连接查询
- select a.class ,a.id ,A.name,a.sex,b.class,b.id,b.name,b.sex from student2 A left outer join student B on a.class =b.name
- --提前5分钟提醒
- select * from student where DATEDIFF(minute,inserttime ,getdate())>5
- ---这两个为互补条件
- select * from student3 where exists(select * from student where student.name=student3.name)--列出student3中在student出现过name 的记录
- union all
- select * from student3 where not exists(select * from student where student.name=student3.name)--列出student3中没有在student出现过name的记录
- --获取n到m条记录的方法
- --select top (m-n+1) * from
- --(select top m * from tablename order by columnname)A
- --order by columnname desc
- select top (16-10+1) * from
- (select top 16 * from student2 order by id )A
- order by id desc
- --如果表中有identity属性,则
- --select * from tablename where identitycol between n and m
- select * from student2 where id between 2 and 6
- --如果表中没有identity属性,则
- --select identity(int)id0,* into #temp from tablename
- --select * from #temp where id0>=n and id0<m
- select IDENTITY (int)id0,* into #temp3 from student4 ---id0为新增的identity 列
- select * from #temp3 where id0>=2 and id0<=6
- ----获取一个数据表的所有列名
- declare @objid int,@objname char(40)
- set @objname ='student2'
- select @objid=id from sysobjects where id=OBJECT_ID(@objname)
- select 'Column_name'=name from syscolumns where id=@objid order by colid
- ---通过sql 语句来更改用户的密码
- --修改别人的需要sysadmin role
- exec sp_password null,'newpassword','user'
- --如果账号为sa
- exec sp_password null,'newpassword',sa
- --如何判断一个表的那些字段不允许为空
- select column_name from INFORMATION_SCHEMA.COLUMNS where IS_NULLABLE='NO' and TABLE_NAME ='student2'
- --在数据库中找到含有相同字段的表
- --a .查已知列名的情况
- select b.name as tablename,a.name as columnname
- from syscolumns a inner join sysobjects b
- on a.id =b.id
- and b.type='U'
- and a.name='sex'
- --b. 未知列名查所有在不同表出现过的列名
- select o.name as tablename,s1.name as columnname
- from syscolumns s1,sysobjects o
- where s1.id=o.id
- and o.type='U'
- and exists(
- select 1 from syscolumns s2
- where s1.name=s2.name
- and s1.id<>s2.id
- )
- --查询第xxx行数据
- select *
- from (select top 17 * from student2)aa
- where not exists (select 1 from (select top (17-1) * from student2)b where aa.id=b.id)
- --PS : select 1 和select anycol 和select * 作用上没有差别,都是查看是否有记录,一般是做条件查询用的
- --1可以为任意常量,查到的所有行的值都是它 .效率上 1>anycol>* (不用查字典表)
- --使用游标查询
- --fetch absolute [number] from [cursor_name]
- --以下是SQLserver日期计算
- --一个月的第一天
- select DATEADD (mm,datediff(mm,0,getdate()),0)
- --本周的星期一
- select DATEADD (wk,datediff(wk,0,getdate()),0)
- --季度的以一天
- select DATEADD (qq,datediff(qq,0,getdate()),0)
- --上个月的最后一天
- select DATEADD (ms,-3,dateadd (mm,datediff(mm,0,getdate()),0))
- --去年的最后一天
- select DATEADD(ms,-3,dateadd(yy,datediff(yy,0,GETDATE()),0))
- --本月的最后一天
- select DATEADD(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))
- --本月的第一个星期一
- select DATEADD (wk,datediff(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())),0)
- --本年的最后一天
- select DATEADD (ms,-3,dateadd(yy,datediff(yy,0,getdate())+1,0))
- --快速获取表的记录总数(对大容量表非常有效)
- select rows from sysindexes where id=OBJECT_ID ('student2') and indid in (0,1)