统计记录数Count()、DISTINCT
select COUNT(name)
from [DemoDB2008].[dbo].[tem]
use master
go
select COUNT( distinct name)
from [DemoDB2008].[dbo].[tem]
where name='jim'
统计字段不同值分别是什么
select distinct ss_id from dbo.Url_Base with(nolock)
查询的总量(无意义)
------------查询导进fff库的数量---------------
declare @c1 int,@c2 int,@sum int,@jt varchar(40),@wxj varchar(40)
set @jt='佳通'
set @wxj ='无限极'
select @c1= count(*) from
((select SN from ViewContentAndExt where SN like @jt+'%')
except
(select originalid from [12.111.0.1].Giti_VoC.dbo.article where originalid like @jt+'%')) as a
select @sum =COUNT(*) from ViewContentAndExt where SN like @jt+'%'
print 'meta库总计:'
print @sum
set @c2=@sum-@c1
print '传输成功的:'
print @c2
print '传输失败的:'
print @c1
SELECT COUNT(Url)
FROM [Url_Base] with(nolock)
SELECT C_Id
FROM [Url_Base] with(nolock)
GROUP BY
C_Id
SELECT SS_Id
FROM [Url_Base]
with(nolock) where C_Id = 3
GROUP BY
SS_Id
SELECT
Url,
COUNT( Url )
FROM
[Url_Base]
with(nolock) where (C_Id = 3 and SS_Id =17 )
GROUP BY
Url
HAVING
COUNT( Url) > 1
平均值计算函数AVG()
select AVG(age)
from [DemoDB2008].[dbo].[tem]
内容统计函数SUM()
select SUM(age)
from [DemoDB2008].[dbo].[tem]
修改
Update [Url_Base] set SN = 'Exchange_20111226_'+SN
where C_Id = 30
最大值MAX()和最小值计算MIN()
select MAX(age)
from [DemoDB2008].[dbo].[tem]
select MIN(age)
from [DemoDB2008].[dbo].[tem]
功能将字符串中的小写字母转换为大写字母。
--upper
select upper('he is a good person')
定时查询数据库
--例 等待1 小时2 分零3 秒后才执行SELECT 语句
waitfor delay '01:02:03'
select * from employee
--例 等到晚上11 点零8 分后才执行SELECT 语句
waitfor time '23:08:00'
select * from employee
创建临时表
declare @temp_table table
(
bookID VARCHAR(10) primary key,
book_price float default null,
bookName varchar(50)
)
insert into @temp_table values('1',50,'c#')
insert into @temp_table values('2',null ,'c')
select bookID AS '书的编号',isnull(book_price,0) as '书的价格'
from @temp_table
删除特定数据,除**之外的数据
delete from [MetaData].[dbo].[Url_Base] where SN not in('白沙','北京')
delete from 表名 where 字段名 <>'白沙' and 字段名<>'北京'
delete from [MataData_Office].[dbo].[Url_Base] where (SN >'00000000' and SN <'0000041100')
删除重复数据
http://www.cnblogs.com/zuoqs/archive/2009/12/06/1618226.html
方法一:还是2000年的时候一位Oracle DBA叫罗敏发给技术部全体的(可惜原始邮件找不到了,要不然我当文物发给大家):
delete from temp
where id not in (
select min(id)
from temp
group by column1,column2,...,columnn)
个人感觉这个方法思路比较清晰。不过效率相对来说不高。
方法二:赵贲在网络上搜出来的:
--删除相同城市下的相同行政区
delete a from area a where a.id>(select min(id) from area b where a.area_id=b.area_id and a.city_id=b.city_id)
方法三:使用sql 2005新增的row_number()功能和with关键字,我是从赵立东那里学来的。
print('删除PriceInfo表中重复的记录')
;WITH a AS (
SELECT ROW_NUMBER()OVER (PARTITION BY hotel_id,room_type_id,start_date,end_date
ORDER BY hotel_id,room_type_id,start_date,end_date) AS rn ,*
FROM hotel_price
)
delete from a WHERE a.rn>1
游标
declare cursor_c cursor
for select * from [DemoDB2008].[dbo].[tem]
open cursor_c fetch next from cursor_c
while @@fetch_status=0
begin
fetch next from cursor_c
end
close cursor_c
存储过程
USE DemoDB2008;//数据库名称
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE dbo.sp_who//create 创建存储过程,alter 用于执行查询
AS
SELECT departmentName, [departmentRemark] FROM [DemoDB2008].[dbo].[Departments];//查询语句
GO
sp_who;//执行
execute sp_who; //执行
EXEC dbo.sp_who;GO//执行
DROP PROCEDURE dbo.sp_who;//删除存储过程
GO
带函数的存储过程
USE DemoDB2008;
GO
--IF OBJECT_ID ( 'dbo.usp', 'P' ) IS NOT NULL
-- DROP PROCEDURE dbo.usp;
--GO
alter PROCEDURE dbo.usp
@FirstName nvarchar(50), //定义两个变量
@LastName nvarchar(50)
AS
SET NOCOUNT ON;
SELECT [departmentID]
,[departmentName]
,[departmentRemark]
FROM [DemoDB2008].[dbo].[Departments]
WHERE [departmentID] = @FirstName and [departmentRemark]=@LastName ;
GO
新建查询语句
dbo.usp 5,df;//执行时必须两个变量
EXEC dbo.usp @LastName=df,@FirstName = 5;
新建存储过程和视图
检测各Job是否正常运行,为各Job提供依据
服务器名:222.85.28.1
库名:MetaData_1
表名:url_base url_ext url_content
存储过程名:P_CheckJob
创建存储过程:
USE [MetaData_Win7]
GO
/****** Object: StoredProcedure [dbo].[P_CheckJob] Script Date: 12/01/2011 15:11:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[P_CheckJob]
@SS_Id smallint,
@c nvarchar(20),
@day datetime
as
declare @count int,@sn varchar(50)
set @sn=@c+'_'+CONVERT(nvarchar(8),@day,112)+'%'
set @count=0
select @count=count(*) from url_base with(nolock) where SS_Id=@SS_Id and SN like @sn
print 'Base条数:'+cast(@count as varchar)
select @count=count(*) from Url_Ext with(nolock) where sn in (select SN from url_base with(nolock) where SS_Id=@SS_Id and SN like @sn)
print 'Ext条数:'+cast(@count as varchar)
select @count=count(*) from Url_Content with(nolock) where sn in (select SN from url_base with(nolock) where SS_Id=@SS_Id and SN like @sn)
print 'Content条数:'+cast(@count as varchar)
print '--------------'
示例:创建存储过程后执行下面语句
exec dbo.[P_CheckJob] 12,'**','20110914' -------------------soso
exec dbo.[P_CheckJob] 17,'**','20110914' ------------------qihoo
exec dbo.[P_CheckJob] 7,'**','20110914' ------------------ baidunews
exec dbo.[P_CheckJob] 2,'**','20110914' -----------------------baidu
exec dbo.[P_CheckJob] 20,'**','20110914' -----------------------google
结果示例:
Base条数:55 |
Ext条数:55 |
Content条数:55 |
-------------- |
Base条数:7 |
Ext条数:7 |
Content条数:7 |
-------------- |
Base条数:1758 |
Ext条数:1758 |
Content条数:1758 |
-------------- |
Base条数:4881 |
Ext条数:4881 |
Content条数:3391 |
-------------- |
Base条数:6153 |
Ext条数:6153 |
Content条数:6153 |
-------------- |
检测数据传输是否有问题(检测)
服务器名:2.85.28.1
库名:MetaData_1
视图:ViewContentAndExt
创建视图:
USE [MetaData_1]
GO
/****** Object: View [dbo].[ViewContentAndExt] Script Date: 12/01/2011 15:19:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[ViewContentAndExt]
as
select Url_Ext.SN,Url_Ext.Page_Title,R_Id,Url,Author,Category_Value,Page_Desc,Page_DT,Page_Source,View_Count,Reply_Count,Same_Count,Page_Rank,Ext_1,Ext_2,Status_1,Status_2,Url_Content.Html_Source
from Url_Content with(nolock) inner join Url_Ext with(nolock) on Url_Content.SN=Url_Ext.SN
GO
创建存储过程:CREATE是创建, ALTER是执行
USE [MetaData_1]
GO
/****** Object: StoredProcedure [dbo].[procDayStatistics] Script Date: 12/01/2011 15:17:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[procDayStatistics] (@C nvarchar(30), @start datetime,@end datetime)
as
declare @sql nvarchar(4000),@append nvarchar(200),@colname nvarchar(8)
set @sql='select '
while(@start<@end)
begin
set @colname=CONVERT(nvarchar(8),@start,112)
set @append=@C+'_'+ @colname+'%'
set @sql=@sql+'count(case when sn like '''+@append+''' then sn end) as '''+@colname+''','
select @start=DATEADD(day,1,@start)
end
set @colname=CONVERT(nvarchar(8),@start,112)
set @append=@C+'_'+ @colname+'%'
set @sql=@sql+'count(case when sn like '''+@append+''' then sn end) as '''+@colname+''' from ViewContentAndExt with(nolock)'
print @sql
exec(@sql)
存储过程创建完成后执行:
exec procDayStatistics '佳通','20110909','20110915'
exec [192.111.0.16].Giti_VoC.dbo.procDayStatistics '佳通','20110909','20110915'
exec dbo.procDayStatistics 'IE9','20111128','20111201'
exec IE9_VOC.dbo.procDayStatistics 'IE9','20111128','20111201'
结果示例:
复制表结构
1,右击数据表->编写表脚本为(S)->CREATE到(C)->剪贴板
2,右击目标数据库->新建查询(Q),粘贴复制到的表结构->不要忘记修改数据库名称