易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 :

SQL 优化技巧

1 避免无计划的全表扫描
  如下情况进行全表扫描:
-          该表无索引
-          对返回的行无人和限制条件(无Where子句)
-          对于索引主列(索引的第一列)无限制条件
-          对索引主列的条件含在表达式中
-          对索引主列的限制条件是is (not) null或!=
-          对索引主列的限制条件是like操作且值是一个bind variable或%打头的值
2 只使用选择性索引
   索引的选择性是指索引列中不同值得数目和标志中记录数的比,选择性最好的是非空列的唯一索引为1.0。
复合索引中列的次序的问题:
  1 在限定条件里最频繁使用的列应该是主列
  2 最具有选择性的列(即最清晰的列)应该是主列
  如果1和2 不一致,可以考虑建立多个索引。
在复合索引和多个单个索引中作选择:
  考虑选择性 考虑读取索引的次数&nb ......

一次插入多条信息(sql)

注释:只适合单表单列数据,
create database test
go
use test
go
create table users
(
:id int identity(1,1) primary key not null,
:name nvarchar(20)
)
go
create proc sp_Inserts
@Names nvarchar(4000)
as
declare @Name nvarchar(20),@ErrorSum int
:set @ErrorSum = 0
:begin tran
:while(len(@Names)>0)
:begin
::if(charindex(',',@Names)<>len(@Names))
::begin
:::set @Name = substring(@Names,0,charindex(',',@Names))
:::insert into users(name) values(@Name)
:::set @ErrorSum = @ErrorSum + @@error
:::set @Names = substring(@Names,(charindex(',',@Names)+1),(len(@Names)-(charindex(',',@Names))))
::end
::else
::begin
:::set @Name = substring(@Names,0,charindex(',',@Names))
:::insert into users(name) values(@Name)
:::set @ErrorSum = @ErrorSum + @@error
:::set @Names = ''
::end
:end:
:if(@ErrorSum<>0)
::rollback tran
:else
::commit tran
go
sp_Inserts 'Tom,Jack,Bob,' ......

Merge SQL 2008

merge [target] t
using [source] s on t.id = s.id
when matched then update t.name = s.name, t.age = s.age -- use "rowset1"
when not matched then insert values(id,name,age) -- use "rowset2"
when source not matched then delete; -- use "rowset3"
MERGE dbo.table AS im --对比表
USING (SELECT * from dbo.table_MID with(nolock) ) AS src --源
ON im.ProductID = src. ProductID
WHEN MATCHED THEN
UPDATE SET im.ProductID = src. ProductID
,im.BrandName=src.BrandName
,im.ProductName = src.ProductName
,im.ProductType=src.ProductType
,indate=getdate()
WHEN NOT MATCHED THEN
INSERT (ProductID,BrandName,Series,ModelNumber,ProductName,ProductType,Category,indate)
VALUES (src.ProductID,src.BrandName,src.Series,src.ModelNumber,src.ProductName,src.ProductType,src.Category,src.indate)
WHEN NOT MATCHED BY SOURCE then delete; ......

SQL 使用 CONVERT

使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT( ......

sql 各种字符串合并方法 第二种方法好(利用函数)

--3.3.1 使用游标法进行字符串合并处理的示例。
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
--合并处理
--定义结果集表变量
DECLARE @t TABLE(col1 varchar(10),col2 varchar(100))
--定义游标并进行合并处理
DECLARE tb CURSOR LOCAL
FOR
SELECT col1,col2 from tb ORDER BY col1,col2
DECLARE @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100)
OPEN tb
FETCH tb INTO @col1,@col2
SELECT @col1_old=@col1,@s=''
WHILE @@FETCH_STATUS=0
BEGIN
IF @col1=@col1_old
SELECT @s=@s+','+CAST(@col2 as varchar)
ELSE
BEGIN
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
SELECT @s=','+CAST(@col2 as varchar),@col1_old=@col1
END
FETCH tb INTO @col1,@col2
END
INSERT @t VALUES(@col1_old,STUFF(@s,1,1,''))
CLOSE tb
DEALLOCATE tb
--显示结果并删除测试数据
SELECT * from @t
DROP TABLE tb
/*--结果
col1 col2
---------- -----------
a 1,2
b 1,2,3
--*/
GO
/*=== ......

SQL日期格式大全

CONVERT
将某种数据类型的表达式显式转换为另一种数据类型。由于某些需求经常用到取日期格式的不同.现以下可在
SQL Server中 将日期格式化.
SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。
在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。
不带世纪数位 (yy)    带世纪数位 (yyyy)   
标准   
输入/输出**
-    0 或 100 (*)     默认值    mon dd yyyy hh:miAM(或 PM)
1    101    美国    mm/dd/yyyy
2    102    ANSI    yy.mm.dd
3    103    英国/法国    dd/mm/yy
4    104    德国    dd.mm.yy
5    105    意大利    dd-mm-yy
6    106    - &nbs ......
总记录数:40319; 总页数:6720; 每页6 条; 首页 上一页 [1443] [1444] [1445] [1446] 1447 [1448] [1449] [1450] [1451] [1452]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号