易截截图软件、单文件、免安装、纯绿色、仅160KB

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
/******************************************************************/
/******************************************************************************/
/******************************************************************************/


相关文档:

SQLServer获取Excel中所有Sheet

E盘根目录新建一个Excel文件aa.xls后测试如下代码
use tempdb
go
if (object_id ('udf_getExcelTableNames' ) is not null )
    drop function dbo .udf_getExcelTableNames
go
create function udf_getExcelTableNames (@filename varchar (1000 ))
returns @t table (id int , name varchar ( ......

SQLServer2005分解并导入xml文件

  SQLServer2005分解并导入xml文件 收藏
测试环境SQL2005,windows2003
DECLARE @idoc int;
DECLARE @doc xml;
 
SELECT @doc=bulkcolumn from OPENROWSET(
   BULK 'D: \test.xml',
   SINGLE_BLOB) AS x
 
EXEC sp_xml_preparedocument @Idoc OUTPUT, @doc
 
  ......

SQLSERVER自增主键

SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入。
1. SQLServer 自增主键创建语法:
identity(seed, increment)
其中
seed 起始值
increment 增量
示例:
create table student(
      id int identity(1,1),
      name varcha ......

重建 SQLServer 索引的重要性!


原文转自:http://dev.csdn.net/develop/article/71/71778.shtm
 
大多数SQL Server表需要索引来提高数据的访问速度,如果没有索引,SQL Server要进行表格扫描读取表中的每一个记录才能找到索要的数据。索引可以分为簇索引和非簇索引,簇索引通过重排表中的数据来提高数据的访问速度,而非簇索引则通过维护表中的数 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号