SQL温故系列两篇(一)

1.不允许保存更改。您所做的更改要求删除并重新创建以下表

关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。

打开SQL SERVER 2008 工具-->选项-->Designers-->表设计器和数据库设计器,把“阻止保存要求重新创建表的更改”的勾去掉然后点“确定”即可。

 

2.同比环比

今年本月统计

select COUNT(0) from [jcms_normal_content_dbvisitcount] where dbid=91 and datediff(d,VisitTime,getdate())>=0 and datediff(d,VisitTime,getdate())<=datepart(dd, getdate()) and datepart(mm, VisitTime) =datepart(mm, getdate()) and datepart(yy, VisitTime) =datepart(yy, getdate())

 

select COUNT(0) from [jcms_normal_content_dbvisitcount] where dbid=91 and VisitTime between dateadd(day,-datepart(dd,getdate()),getdate()) and getdate() 

 

去年本月统计

select COUNT(0) from [jcms_normal_content_dbvisitcount] where dbid=91 and datediff(d,VisitTime,dateadd(year,-1,getdate()))>=0 and datediff(d,VisitTime,dateadd(year,-1,getdate()))<=datepart(dd, dateadd(year,-1,getdate())) and datepart(mm, VisitTime) =datepart(mm, dateadd(year,-1,getdate())) and datepart(yy, VisitTime) =datepart(yy, dateadd(year,-1,getdate()))

 

select COUNT(0) from [jcms_normal_content_dbvisitcount] where dbid=91 and VisitTime between dateadd(day,-datepart(dd,dateadd(year,-1,getdate())),dateadd(year,-1,getdate())) and dateadd(year,-1,getdate()) 

 

今年上个月

select COUNT(0) from [jcms_normal_content_dbvisitcount] where dbid=91 and datediff(d,VisitTime,dateadd(month,-1,getdate()))>=0 and datediff(d,VisitTime,dateadd(month,-1,getdate()))<=datepart(dd, dateadd(month,-1,getdate())) and datepart(mm, VisitTime) =datepart(mm, dateadd(month,-1,getdate())) and datepart(yy, VisitTime) =datepart(yy, dateadd(month,-1,getdate()))

 

select COUNT(0) from [jcms_normal_content_dbvisitcount] where dbid=91 and VisitTime between dateadd(day,-datepart(dd,dateadd(month,-1,getdate())),dateadd(month,-1,getdate())) and dateadd(month,-1,getdate())

 

保留两位小数:

select cast( cast(50 as  decimal(8,2) ) /  cast(49 as  decimal(8,2) ) as  decimal(8,2) ) 

 

删除一条数据:

select * from jcms_normal_content where Id=732;

delete from jcms_normal_content where Id=732;

 

将数据库的一张表数据复制到另一张表数据

insert into chaoxing_zgdzdx.dbo.jcms_normal_content (classid,AddDate,title,content) select classid,AddDate,title,content  from chaoxingpms_zgdzdx.dbo.jcms_normal_content where classid=1190 

 

语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

 Insert into Table2(a, c, d) select a,c,5 from Table1

 

语句形式为:SELECT vale1, value2 into Table2 from Table1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中

将数据库一张表的数据更新到另一张表中

update a set a.Content=b.Content ,a.UpdateTime=b.UpdateTime from chaoxing_zgdzdx.dbo.jcms_normal_class a,chaoxingpms_zgdzdx.dbo.jcms_normal_class b where b.title='部门设置' and a.title=b.title

 

UPDATE SELECT 语句
第一种:UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A LEFT JOIN B ON A.ID = B.ID
第二钟:UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A, B WHERE A.ID = B.ID

 

禁用外键约束  

exec   sp_msforeachtable   'alter   table   ?   nocheck   constraint   all '  

 

清空数据  

truncate   table   表名

 

启用外键约束  

exec   sp_msforeachtable   'alter   table   ?   check   constraint   all '

 

.读取10条到第20条记录

SELECT TOP 10 * FROM (SELECT TOP 20 * FROM tblORDER order by id) as tbl2 ORDER BY tbl2.id DESC

 

循环插入数据

declare @Id bigint
set @Id=28
while @Id<1000
begin
insert into jcms_normal_content_dbvisitcount (VisitIp,VisitTime,VisitIplocal,Module,dbID,Device,Browser) values('::1','2014-02-03 13:08:11.833',null,'link','91','pc',null)
set @Id=@Id+1
end

 

查询字段是否包含有这值的数据

SELECT * FROM jcms_normal_content WHERE charindex('http://boao.libsou.com/webpage/',customfield04)>0 

 

排序按多个字段

当需要做一些数据统计或者业务比较复杂的时候,我们往往需要使用多个字段进行排序。如:
select * from Score order by Cno asc, Degree desc

 

ORDER BY _column1, _column2; /* _column1升序,_column2升序 */
 
ORDER BY _column1, _column2 DESC; /* _column1升序,_column2降序 */
 
ORDER BY _column1 DESC, _column2 ; /* _column1降序,_column2升序 */
 
ORDER BY _column1 DESC, _column2 DESC; /* _column1降序,_column2降序 */

 

语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

 

怎样取表里的第N条记录啊?

n 為要取的字段

select * 
from (select top n * from students) aa 
where not exists(select * from (select top n-1 * from students) bb where aa.id=bb.id)

 

Update Top 1 record in table sql server

 

UPDATE TX_Master_PCBA
SET TIMESTAMP2 = '2013-12-12 15:40:31.593',
G_FIELD='0000'
WHERE TIMESTAMP2 IN 
(
   SELECT TOP 1 TIMESTAMP2
   FROM TX_Master_PCBA WHERE SERIAL_NO='0500030309'
   ORDER BY TIMESTAMP2 DESC   -- You need to decide what column you want to sort on
)

参考:https://stackoverflow.com/questions/20539095/update-top-1-record-in-table-sql-server  

更新子查询

更新子查询
UPDATE  Fct_Order 
SET     SelfCollectionIsPrint = 2
FROM Fct_Order fo
WITH(ROWLOCK )
        LEFT JOIN[dbo].[Rel_OrderDetails]
        rod WITH(ROWLOCK) ON fo.OrderId = rod.OrderId
LEFT JOIN[dbo].[Fct_Commodity] fc WITH (ROWLOCK) ON rod.CommodityId = fc.CommodityId 
WHERE fo.AreaCode = '1' AND fo.DeliveryDate >= '2017/8/4 0:00:00' AND fo.DeliveryDate <'2017/9/5 0:00:00' AND fo.OrderState = 2
       -- AND fo.SelfCollectionIsPrint = 2
        AND fo.Disabled = 0
        AND rod.Disabled = 0
        AND fc.WebId = 1
        AND fc.Disabled = 0

 

posted @ 2016-03-13 16:15  BloggerSb  阅读(327)  评论(0编辑  收藏  举报