SQl使用方法总结(2)

发表于:2012-02-14来源:未知作者:娃娃点击数: 标签:sql
例子 ELECT DATEDIFF(day,2008-12-29,2008-12-30) 结果是1 ELECT DATEDIFF(day,2008-12-30,2008-12-29) 结果是-1 7.相同结构表去重复后进行复制 insert into 新表名select DomainInFo_FIp.D

  例子

  ELECT DATEDIFF(day,'2008-12-29','2008-12-30')

  结果是1

  ELECT DATEDIFF(day,'2008-12-30','2008-12-29')

  结果是-1

  7.相同结构表去重复后进行复制

  insert into 新表名select DomainInFo_FIp.D_Domain,源表名.D_ID,源表名.D_IP,源表名.stat from 源表名

  inner join (select min(D_ID) D_ID,distinct(D_Domain) as D_Domain from 源表名) TempTab on 源表名.D_ID = TempTab.D_ID

  复制代码

  8.一个取一张表中域名第一个“.”之前部分,并写入另外一张表的游标实现

  declare @D_Domain nvarchar(200); Declare Mycursor cursor for select D_Domain FROM dbo.DomainInFo_FIp Open Mycursor Fetch next from Mycursor into @D_Domain --开始抓数据 while @@FETCH_STATUS = 0 begin set @D_Domain=Left(@D_Domain,Charindex('.',@D_Domain)-1) if(Len(@D_Domain)>0 and @D_Domain<>'www') begin insert into dbo.DomainInFo_FIp_log (D_Domain)values(@D_Domain) end Fetch next from Mycursor into @D_Domain end Close Mycursor --关闭游标 Deallocate Mycursor --删除游标

  复制代码

  9.计算一表中某个字段的重复次数,游标实现

  declare @D_Domain nvarchar(200);declare @count int; Declare Mycursor cursor for select D_Domain FROM dbo.DomainInFo_FIp_log Open Mycursor Fetch next from Mycursor into @D_Domain --开始抓数据 while @@FETCH_STATUS = 0 begin select @count=count(*) from DomainInFo_FIp_log where D_Domain=@D_Domain update DomainInFo_FIp_log set numindex=@count where D_Domain=@D_Domain Fetch next from Mycursor into @D_Domain end Close Mycursor --关闭游标 Deallocate Mycursor --删除游标

  复制代码

  10.Replace函数的使用方法

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'=','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'<','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'-','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'~','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,']','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'[','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'$','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'{','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'}','')

  update dbo.DomainInFo_FIp set D_Domain=Replace(D_Domain,'·','')

  复制代码

  11.Substring函数与Len以及Left的配合使用(字段D_Domain值第一个字符是点的就清除这个点)

  update dbo.DomainInFo_FIp set D_Domain=Substring(D_Domain,2,Len(D_Domain)) where Left(D_Domain,1) ='.'

  12.一个实现怎么样查询IP所在段的函数代码如下

  set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER FUNCTION [dbo].[GetIP](@ip varchar(20)) RETURNS varchar(20) AS BEGIN declare @IPre varchar(20) SET @IPre='' select @IPre = right('00'+ParseName(@IP,4),3)+'.'+ right('00'+ParseName(@IP,3),3)+'.'+ right('00'+ParseName(@IP,2),3)+'.'+ right('00'+ParseName(@IP,1),3) RETURN @IPre END

  复制代码

  如果大家想快速查询的话我建议大家先把自己数据的IP段执行一下这个函数,然后每次查询的时候直接转要查询的IP就行了,这样速度会很快的。

  例子如下:

  -- =============================================-- Author: -- Create date: -- Description: -- =============================================ALTER PROCEDURE [dbo].[GetIPInfoByIP]@ip varchar(200) outputASBEGIN SET @ip =dbo.GetIP(@ip) SELECT iptitle FROM Qqwry WHERE GetIP(@ip) BETWEEN ipst AND ipend END

  复制代码

  大家一定不要傻的这样写语句

  SELECT iptitle FROM Qqwry WHERE GetIP(@ip) BETWEEN ipst AND ipend

  因为这样的速度是极慢的,那是因为每一次检查都要转化。

  在这里也提示大家在写Sql语句时不要把函数直接写在查询语句内,当然 如果是必须的那就没有办法了只能那样了。

  13.解决数据库正在使用无法分离的存储过程

View Code

  set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER proc [dbo].[p_killspid] @dbname varchar(200) --要关闭进程的数据库名 as declare @sql nvarchar(500) declare @spid nvarchar(20) declare #tb cursor for select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) open #tb fetch next from #tb into @spid while @@fetch_status=0 begin exec( 'kill '+@spid) fetch next from #tb into @spid end close #tb deallocate #tb

原文转自:http://www.ltesting.net