位置: 编程技术 - 正文

有用的SQL语句(删除重复记录,收缩日志)(sql经常用的语句)

编辑:rootadmin
删除重复记录,将TABLE_NAME中的不重复记录保存到#TABLE_NAME中 select distinct * into #table_name from table_name delete from table_name select * into table_name from #table_name drop table #table_name 与此相关的是“select into”选项,可以在数据库属性 对话框中,勾起来此项,或者在Query Analyzer中执行 execute sp_dboption 'db_name','select into','true' 开启。默认值是关闭的。 ******************************************************* 收缩事务日志(多次执行) backup log register with NO_LOG backup log register with TRUNCATE_ONLY DBCC SHRINKDATABASE(register) 更多有用的sql语句/*sql 语法学习*/ /*函数的学习---------------------------------------*/ 获取当前时间(时/分/秒):select convert(varchar(),getdate(),8) 获取当前年月日:select convert(varchar(),getdate(),) 获取当前年月:select convert(varchar(7),getdate(),) 获取当前年月:select convert(varchar(),year(getdate())) + '-' + convert(varchar(),month(getDate())) select cast(b as integer) as bb from table1 where b = '' select a,case b when '' then '细细' when '' then '呵呵' else '哈哈' end as 转换,c from table1 select a,b,case when c = '' then '细细' when c = '' then '呵呵' else '哈哈' end as 转换1 from table1 获取当前时间:print current_timestamp /*---------------------------------------------*/ -----------------将sql查询输出到txt文本文件中------------------------------------------- EXEC master..xp_cmdshell 'bcp 数据库名.dbo.表名 out d:1.txt -c -q -U"sa" -P"password"' --------------------------------------------------------------------------------------- ---------------------------round的用法beigin------------------------------ declare @s float set @s = 0. print round(@s,3) ---------------------------round的用法end--------------------------------- --------------------------------自动收缩数据库begin----------------------------- EXEC [master]..sp_dboption [Database Name], 'autoshrink', 'TRUE' --------------------------------自动收缩数据库end----------------------------- -------------------------------去除首尾无效的字符begin-------------------------- declare @s varchar() set @s=',,,1->1,' while(left(@s,1)=',') set @s=stuff(@s,1,1,'') while(right(@s,1)=',') set @s=stuff(reverse(@s),1,1,'') select @s -------------------------------去除首尾无效的字符end-------------------------- ------------删除数据库中的重复记录(且仅保留一条有效记录)示例----------------- create table A ( userID int identity(1,1), userName varchar(), userPwd varchar(), userEmail varchar() ) insert into A(userName,userpwd) select 'qin','qin' union all select 'qin','qin1' union all select 'qin','qin1' select * from A --method one delete from A where userid not in(select min(userid) as userid from A group by username ,userpwd) --method two delete from A where exists (select * from A b where a.username = b.username and a.userpwd = b.userpwd and a.userid < b.userid) --method three delete from a where userid not in(select min(userid) from A b where a.username = b.username and a.userpwd = b.userpwd and a.userid > b.userID) select * from A drop table A ------------删除数据库中的重复记录(且仅保留一条有效记录)示例----------------- -------------------------------迭归的应用(找起点和终点之间的路径----------------------------- create table t (st varchar(),ed varchar(),km int) go insert t values ('A','B',) insert t values ('A','C',) insert t values ('A','D',) insert t values ('A','E',) insert t values ('B','D',) insert t values ('D','F',) insert t values ('E','A',) insert t values ('F','G',) insert t values ('C','B',) go --显示插入值 select * from t go --创建函数 --函数返回一个表,根据实际情况的不同一层一层的插入,可以充分利用生成的表 create function f_go(@col varchar()) returns @t table(col varchar(),st varchar(),ed varchar(),km int,level int) as begin declare @i int set @i=1 insert @t select st+'-'+ed,*,@i from t where st=@col while exists (select * from t a,@t b where b.ed=a.st and b.level=@i and b.ed<>@col ) begin set @i=@i+1 insert @t select b.col+'-'+a.ed,a.st,a.ed,b.km+a.km,@i from t a,@t b where b.level=@i-1 and b.ed=a.st and b.ed<>@col end return end go --调用 --select * from dbo.f_go('A') select col,km from dbo.f_go('a') --删除环境 drop function f_go drop table t -------------------------------迭归的应用(找起点和终点之间的路径----------------------------- --------按类别去最新的前N条记录,把同一类的放在一起,统计同一类的项的个数等------------- create table t ( ClassName varchar(), ClassCode varchar(), ClassID int identity(1,1) ) insert into t select 'cccc1','' union all select 'aaaa','' union all select 'bbbb','' union all select 'aaaa1','' union all select 'cccc','' union all select 'dddd','' union all select 'bbbb1','' union all select 'dddd1','' select * from t select ClassCode = (case when exists(select 1 from t t1 where classCode = t1.ClassCode and ClassID < t1.ClassID) then '' else ClassCode end),ClassName from t order by ClassCode,ClassID desc select count(*),classCode from (select top percent ClassCode = (case when exists(select 1 from t t1 where classCode = t1.ClassCode and ClassID < t1.ClassID) then '' else ClassCode end),ClassName from t order by ClassCode,ClassID desc)a group by classcode select classCode,className from t order by classCode,classID desc drop table t --------按类别去最新的前N条记录,把同一类的放在一起,统计同一类的项的个数等------------- -------------同上,按类别进行统计,把同一类的项的其他内容进行相加并发在一个字段中------------------ create table tb(ProductID varchar(),PositionID varchar()) insert into tb select '','A1' union all select '','B2' union all select '','C3' union all select '','D4' union all select '','E5' go create function dbo.fc_str(@ProductID varchar()) returns varchar() as begin declare @sql varchar() set @sql='' select @sql=@sql+','+cast(PositionID as varchar()) from tb where ProductID=@ProductID return stuff(@sql,1,1,'') end go select ProductID,dbo.fc_str(ProductID) as PositionID from tb group by ProductID drop table tb drop function dbo.fc_str -------------按类别进行统计,把同一类的项的其他内容进行相加并发在一个字段中------------------ --取各个类的前n条记录(每个类都取top n条) --如果有数据库中有多个类,现在要取每个类的前n条记录,可用以下语句 Create Table TEST (ID Int Identity(1,1), h_id Int) Insert TEST Select Union All Select Union All Select Union All Select Union All Select Union All Select Union All Select GO --方法一: Select * From TEST A Where Id In(Select TOP 3 ID From TEST Where h_id=A.h_id) --方法二: Select * From TEST A Where Not Exists (Select 1 From TEST Where h_id=A.h_id And ID<A.ID Having Count(*)>2) --方法三: Select * From TEST A Where (Select Count(*) From TEST Where h_id=A.h_id And ID<A.ID)<3 GO Drop Table TEST GO --分组统计,统计每个段中数据的个数 --一般成绩统计可以用到这个 declare @t table(id int,weight int) insert into @t select 1, insert into @t select 2, insert into @t select 3, 5 insert into @t select 4, insert into @t select 5, insert into @t select 6, insert into @t select 7, insert into @t select 8, insert into @t select 9, insert into @t select , declare @p int set @p= select rtrim(p*@p)+'-'+rtrim((p+1)*@p">p*@p)+'-'+rtrim((p+1)*@p) as p, num from (select (weight/@p">weight/@p) as p,count(*) as num from @t where weight between and group by (weight/@p">weight/@p)) a ----------------------------在in语句中只用自定义排序begin-------------------------------- declare @t table(id int,weight int) insert into @t select 1, insert into @t select 2, insert into @t select 3, 5 insert into @t select 4, insert into @t select 5, insert into @t select 6, insert into @t select 7, insert into @t select 8, insert into @t select 9, insert into @t select , --默认in语句中sql会按照id进行排序 select * from @t where id in(2,4,3) --用此方法可以按照我们传入的id顺序进行显示数据 select * from @t where id in(2,4,3) order by charindex(rtrim(id),',2,4,3,') ----------------------------在in语句中只用自定义排序end--------------------------------

推荐整理分享有用的SQL语句(删除重复记录,收缩日志)(sql经常用的语句),希望有所帮助,仅作参考,欢迎阅读内容。

有用的SQL语句(删除重复记录,收缩日志)(sql经常用的语句)

文章相关热门搜索词:sql常用的语句大全,sql常用的语句大全,sql常用,sql语句的用法,sql用处,sql常用的语句大全,sql常用的语句大全,sql常用语句大全简书,内容如对您有帮助,希望把文章链接给更多的朋友!

Access 数据类型与 MS SQL 数据类型的相应 文本nvarchar(n)备注ntext数字(长整型)int数字(整型)smallint数字(单精度)real数字(双精度)float数字(字节)tinyint货币money日期smalldatetime布尔bit

<font color=red>SQL语句示例 SQL的意思是结构化查询语言,其主要功能是同各种数据库建立联系,进行沟通.查询指的是对存储于SQL的数据的请求。查询要完成的任务是:将Select语句的

SQL数据类型详解 (1)二进制数据类型二进制数据包括Binary、Varbinary和ImageBinary数据类型既可以是固定长度的(Binary),也可以是变长度的。Binary[(n)]是n位固定的二进制数据。其

标签: sql经常用的语句

本文链接地址:https://www.jiuchutong.com/biancheng/348078.html 转载请保留说明!

上一篇:MSSQL经典语句(sql 常用语录)

下一篇:Access 数据类型与 MS SQL 数据类型的相应(access数据类型是什么)

  • 证书挂靠要申报个人所得税年度汇算吗
  • 加油站的成品油是石油公司配送吗
  • 股东如何收回投资
  • 小规模纳税季收入怎么算
  • 国家电子产品投诉电话
  • 预缴企业所得税怎么做会计分录
  • 金税盘开完票后怎么报税一下
  • 行政单位上年度费用做多了怎么调整成本
  • 融资租赁会计核算的一般原则是什么
  • 固定资产怎么确认是不是投入使用了
  • 商誉转销会计分录
  • 行政事业单位专用材料费列支范围
  • 公司美元账户收到美元要交税吗
  • 因产品不合格退回报废会计处理
  • 其他业务收入记账凭证怎么写
  • 免税的农业企业可以抵扣专票吗
  • 营改增之后餐饮业的税收政策
  • 公司团建活动奖状模板
  • 为什么有的单位没有住房公积金
  • 职工因公出差伙食补助标准
  • 什么是季初值和季末值
  • 未按规定期限办理身份证
  • 收到的劳务费发票进什么科目
  • 根据规定签发汇票凭证必须记载的事项有
  • 小规模纳税人别人开了专票怎样处理
  • 出口退税的钱退到哪里了
  • 合同签订后税率调整
  • 进口货物如何缴增值税
  • 电脑开机黑屏没信号怎么回事
  • win10家庭最新版本
  • 天猫公司不开银行卡吗
  • 利润为负数怎么调
  • 软件和硬件的成本按销售占比做分摊怎么算
  • 支付航天的代理公司
  • 公司和个人分别交税一部分吗
  • PHP:session_unregister()的用法_Session函数
  • php curl_multi_init
  • win10桌面图标怎么随意摆放
  • vue3怎么用
  • java web中的转发和重定向
  • 高新技术企业认定八大条件
  • 建安企业确认收入的依据
  • 保证增信行通俗理解
  • 原始凭证可以外带吗
  • 航天信息服务费280元会计处理
  • 三证合一后个体交税吗
  • 资本公积的用途主要用于
  • 普惠性幼儿园是公立还是私立的?
  • 蔬菜批发成本怎么核算
  • 应缴财政专户款年末有余额吗
  • 转让专利技术使用权是什么收入
  • 其他货币资金科目主要指
  • 增值税期末留抵税额是什么意思
  • 收到跨月的费用发票怎么入账
  • 员工 成本
  • 应付票据和应付债券的区别
  • 应收应付可以相互冲销吗
  • 公司借款流程
  • mysql参数表
  • 怎么在电脑上设置快捷图标
  • ubuntu怎么用
  • linux ln命令 -s
  • win8系统磁盘清理在哪里
  • mac视频预览图不显示
  • win7旗舰版64位系统无法进入安全模式怎么办?
  • centos6.5升级到7.2
  • win7如何设置屏幕保护时间
  • 微软禁用windows
  • perl中\s+
  • Android OpenGL ES(七)----理解纹理与纹理过滤
  • script_tool_for_linux.bash: Linux 环境下的 hosts 一键部署脚本
  • html5图文混排
  • 读取fasta文件
  • js简单代码
  • android中handler机制
  • 台湾 游戏论坛
  • 用js自动判断浏览记录
  • python中的全局变量
  • 税务系统福利
  • 年休假期间工资支付标准
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设