资源描述:
《经典sql语句大全 - yubinfeng - 博客园》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、经典SQL语句大全-yubinfeng-博客园 、说明:一条sql语句搞定数据库分页locatedintheTomb,DongShenJiabang,deferthenextdayfocusedontheassassination.Linping,Zhejiang,1ofwhichliquorwinemasters(WuzhensaidinformationisCarpenter),whogotAfewbayonets,duetomissedfatal,whennightcameselecttop10b.*from(
2、selecttop20主键字段,排序字段from表名orderby排序字段desc)a,表名bwhereb.主键字段=a.主键字段orderbya.排序字段具体实现:关于数据库分页: declare@startint,@endint @sql nvarchar(600) set@sql=’selecttop’+str(@end-@start+1)+’+fromTwhereridnotin(selecttop’+str(@str-1)+’RidfromTwhereRid>-1)’ execsp_executesq
3、l@sql注意:在top后不能直接跟一个变量,所以在实际应用中只有这样的进行特殊的处理。Rid为一个标识列,如果top后还有具体的字段,这样做是非常有好处的。因为这样可以避免top的字段如果是逻辑索引的,查询的结果后实际表中的不一致(逻辑索引中的数据有可能和数据表中的不一致,而查询时如果处在索引则首先查询索引)altertabletablename--添加一个自增列add column_bintidentity(1,1)locatedintheTomb,DongShenJiabang,deferthenextdayf
4、ocusedontheassassination.Linping,Zhejiang,1ofwhichliquorwinemasters(WuzhensaidinformationisCarpenter),whogotAfewbayonets,duetomissedfatal,whennightcame deletefromtablenamewherecolumn_bnotin(selectmax(column_b) fromtablenamegroupbycolumn1,column2,...)altertablet
5、ablenamedropcolumncolumn_bTRUNCATETABLEtable1“where1=1”是表示选择全部 “where1=2”全部不选,如:if@strWhere!=''beginset@strSQL='selectcount(*)asTotalfrom['+@tblName+']where'+@strWhereendelsebeginset@strSQL='selectcount(*)asTotalfrom['+@tblName+']'endlocatedintheTomb,DongShen
6、Jiabang,deferthenextdayfocusedontheassassination.Linping,Zhejiang,1ofwhichliquorwinemasters(WuzhensaidinformationisCarpenter),whogotAfewbayonets,duetomissedfatal,whennightcame错误!未找到目录项。set@strSQL='selectcount(*)asTotalfrom['+@tblName+']where1=1安定'+@strWhere2、收缩
7、数据库--重建索引DBCCREINDEXDBCCINDEXDEFRAG--收缩数据和日志DBCCSHRINKDBDBCCSHRINKFILEUSEtablename--要操作的数据库名SELECT @LogicalFileName='tablename_log',--日志文件名@MaxMinutes=10,--Limitontimeallowedtowraplog. @NewSize=1 --你想设定的日志文件的大小(M)Setup/initializeDECLARE@OriginalSizeintSELECT@Or
8、iginalSize=size FROMsysfiles WHEREname=@LogicalFileNameSELECT'OriginalSizeof'+db_name()+'LOGis'+ CONVERT(VARCHAR(30),@OriginalSize)+'8Kpagesor'+ CONVERT(VARCHAR(30),(@Origin