SQLSERVER 一些经典问题总结
SQLSERVER--一些经典问题总结
2007-04-01 01:04:06
大中小
/**********************************/
--获得某一天所在年的第一天
declare @a datetime,@b datetime,@sum int,@num int,@res varchar(20)
select @a='1-6-1968'
select @b='2006-4-8'
select @sum=year(@a)
select @res=ltrim(cast(@sum as varchar(20)))+'-1-1'
print @res
/*********************************/
--获得某一天所在月的最后一天
declare @a datetime,@b int,@c int,@res varchar(20)
select @a='2569-8-31'
select @b=month(dateadd(month,1,@a))
select @c=year(@a)
select @res=dateadd(day,-1,cast(@c as varchar(5))+'-'+cast(@b as varchar(20))+'-1')
print @res
/*********************************/
--获得某一天所在年的最后一天
declare @a datetime,@b int,@res varchar(20)
select @a='2026-1-5'
select @b=year(@a)+1
select @res=dateadd(day,-1,cast(@b as varchar(20))+'-1-1')
print @res
/*********************************/
/*返回当前年的最后一天*/
select dateadd(year,datediff(year,'1900-12-31',getdate()),'1900-12-31')
/*返回当月的最后一天*/
declare @a int,@b int,@c varchar(123)
select @a=datepart(year,dateadd(month,1,getdate()))
select @b=datepart(month,dateadd(month,1,getdate()))
select @c=str(@a)+'-'+ltrim(str(@b))+'-1'
print dateadd(day,datediff(day,'1900-1-1',@c)-1,'1900-1-1')
/*打印所有字母*/
if exists (select name from sysobjects where name='ff' and type='fn')
drop function ff
go
create function ff(@a varchar(20),@b int)
returns varchar(20)
as
begin
declare @res varchar(20)
select @res=substring(@a,@b,1)
return @res
end
--先写个函数获得对应位的字符,再打印
declare @a varchar(20),@i int,@len int
select @a='asdfgh'
select @len=len(@a)
select @i=1
while (@i<@len)
begin
print dbo.ff(@a,@i)
select @i=@i+1
end
/******************************************************************/
/******************************************************************************/
/******************************************************************************/
相关文档:
if (object_id ('t' ) is not null ) drop table t
go
create table t (id int identity (1 , 1 ), name varchar (40 ))
go
insert into t (name ) select newid ()
go 10
select * from t
/*
1 18C1C418-9029-4599-8D5E-616354A113C8
2 A0FE1177-09D8-4C56-9FB5-C2FA ......
SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入。
1. SQLServer 自增主键创建语法:
identity(seed, increment)
其中
seed 起始值
increment 增量
示例:
create table student(
id int identity(1,1),
name varcha ......
在sqlserver(应该说在目前所有数据库产品)中创建一个资源如表,视图,存储过程中都要判断与创建的资源是否已经存在
在sqlserver中一般可通过查询sys.objects系统表来得知结果,不过可以有更方便的方法
如下:
if object_id('tb_table') is not null
......
原文转自:http://dev.csdn.net/develop/article/71/71778.shtm
大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server要进行表格扫描读取表中的每一个记录才能找到索要的数据。索引可以分为簇索引和非簇索引,簇索引通过重排表中的数据来提高数据的访问速度,而非簇索引则通过维护表中的数 ......
SQLServer2005通过intersect,union,except和三个关键字对应交、并、差三种集合运算。
他们的对应关系可以参考下面图示
相关测试实例如下:
use tempdb
go
if (object_id ('t1' ) is not null ) drop table t1
if (object_id ('t2' ) is not null ) drop table t2
go ......