常用sql个人札记 共享之

上传人:wt****50 文档编号:35772571 上传时间:2018-03-20 格式:DOC 页数:34 大小:59.58KB
返回 下载 相关 举报
常用sql个人札记 共享之_第1页
第1页 / 共34页
常用sql个人札记 共享之_第2页
第2页 / 共34页
常用sql个人札记 共享之_第3页
第3页 / 共34页
常用sql个人札记 共享之_第4页
第4页 / 共34页
常用sql个人札记 共享之_第5页
第5页 / 共34页
点击查看更多>>
资源描述

《常用sql个人札记 共享之》由会员分享,可在线阅读,更多相关《常用sql个人札记 共享之(34页珍藏版)》请在金锄头文库上搜索。

1、Sql 执行顺序1 执行 where xx 对全表数据做筛选,返回第 1 个结果集。2.针对第 1 个结果集使用 group by 分组,返回第 2 个结果集。3.针对第 2 个结果集中的每 1 组数据执行 select xx,有几组就执行几次,返回第 3 个结 果集。4.针对第 3 个结集执行 having xx 进行筛选,返回第 4 个结果集。5.针对第 4 个结果集排序。存储过程举例:output 就是这个存储过程返回的值 也可以说输出的值 -创建存储过程 求最大值 CREATE PROCEDURE dbo.P_Max a int, - 输入 b int, - 输入 Returnc in

2、t output -输出 AS if (ab) set Returnc =a elseset Returnc =b- 调用 declare Returnc int exec P_Max 2,3,Returnc output select Returnc查询 abc 数据库中含有 fmachineid 字段字段的表名字USE abcGOSELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME IN(SELECT TABLE_NAME from INFORMATION_SCHEMA.TABLES )AND COLUMN_N

3、AME =fmachineid累积求和累积求和%用自定义函数create FUNCTION getsum(月份 int) RETURNS intASbegindeclare sum intselect sum=sum(个数) from 表名 where 月份= begin_time and date 0这种方法理论上比上一种方法多了一个判断语句,即0, 但这个判断过程是最快的, 我相信 80%以上的运算都是花在查找字符串及其它的运算上, 所以运用 charindex 函数也没什么大不了。用这种方法也有好处, 那就是对%,|等在不能直接用 like 查找到的字符中可以直接在这 charindex

4、 中运用, 如下:use mydatabase select * from table1 where charindex(%,username)0大家还可以写成:use mydatabase select * from table1 where charindex(char(37),username)0ASCII 的字符即为%sqlserver sql 常用函数常用函数,语法语法 - 返回一个表中所有的字段select name from syscolumns where id=object_id(tb_usertable_online)- 获取最近添加的标识列的值set rs = ident

5、ity- print len(abcdef)- 大小写转换print lower(ABCDEF)print upper(abcdef)- 去空格print ltrim( abcd dfd df )print rtrim( abcd dfd df )- 求绝对值print abs(-12)- 幂- 3 的 2 次方print power(3,2)print power(3,3)- 随机数- 0 - 1000 之间的随机数print convert(int,rand() * 10 )- 获取圆周率print pi()- 获取系统时间print getdate()- 获取 3 天前的时间print

6、dateadd(day, -3 , getdate()- 获取 3 天后的时间print dateadd(day, 3 , getdate()- 获取 3 年前的时间print dateadd(year, -3 , getdate()- 获取 3 年后的时间print dateadd(year, 3 , getdate()- 获取 3 月后的时间print dateadd(month, 3 , getdate()- 获取 9 小时后的时间print dateadd(hour, 9 , getdate()- 获取 9 分钟后的时间print dateadd(minute, 9 , getdate

7、()- 获取指定时间之间相隔多少年print datediff(year, 2005-01-01, 2008-01-01)- 获取指定时间之间相隔多少月print datediff(month, 2005-01-01, 2008-01-01)- 获取指定时间之间相隔多少天print datediff(day, 2005-01-01, 2008-01-01)- 字符串合并print abc + defprint abcderprint abc + 456print abc + 456- 类型转换print abc + convert(varchar(10), 456)select title_i

8、d, type, price from titles- 字符串连接必须保证类型一致(以下语句执行将会出错)- 类型转换select title_id + type + price from titles- 正确select title_id + type + convert(varchar(10), price) from titlesprint 123 + convert(varchar(3), 123)print 123 + 123- 是否可以定义一个函数- 将作者编号作为参数统计其作品数量并将其返回select au_id, au_lname, dbo.GetTitleCountByAu

9、ID(au_id) as TitleCount from authorsorder by TitleCount- 根据给定的作者编号获取其相应的作品数量create function GetTitleCountByAuID(au_id varchar(12)returns intbeginreturn (select count(title_id) from titleauthorwhere au_id = au_id)end- 查看表结构sp_help titles- 查看存储过程的定义内容sp_helptext GetRankByTitleIdsp_helptext sp_helptext

10、 sp_helptext xp_cmdshell- 声明declare cur_titles cursorfor select title, price from titles- 打开open cur_titlesdeclare title varchar(80)declare price numeric(9,4)declare title_temp varchar(80)declare price_temp numeric(9,4)- 提取fetch cur_titles into title, pricefetch cur_titles into title_temp, price_tem

11、pwhile fetch_status = 0beginif price =012:查看当前数据库中所有存储过程select name as 存储过程名称 from sysobjects where xtype=P14:查询某一个表的字段和数据类型select column_name,data_type from information_schema.columnswhere table_name = 表名 SQL 字符函数:返回字符串中从左边开始指定个数的字符。LEFT ( character_expression , integer_expression )Select LEFT(Name

12、, 5) FROM Production.Product orDER BY ProductID;RIGHT:返回字符表达式中从起始位置(从右端开始)到指定字符位置(从右端开始计数)的部分。RIGHT(character_expression,integer_expression)RIGHT(“Mountain Bike“, 4) 返回结果 BikeLEN:返回给定字符串表达式的字符数(不包括尾随空格),而不是返回字节数。LEN ( string_expression ) Select CompanyName, LEN(CompanyName)FROM CustomersREPLICATE:按指

13、定次数重复字符表达式。REPLICATE ( character_expression, integer_expression) 下面的示例将 Employee 表中每一名雇员的姓氏复制两次:Select REPLICATE (LastName, 2) AS “LastName Twice“FROM EmployeesSUBSTRING:返回 $sourceString 的子串,从 $startingLoc 指定的位置开始,长度为 $length 指定的字符数。 SUBSTRING ( expression, start, length ) 下面的示例返回 Employees 表中每位雇员的名

14、字首字母及完整姓氏:Select SUBSTRING(First Name,1,1) AS Initial, Last NameFROM EmployeesSTUFF:删除指定长度的字符并在指定的起始点插入另一组字符。-替换STUFF ( character_expression, start, length, character_expression ) Select STUFF(ProductID, 2,1, 000)FROM ProductsPATINDEX:对于所有有效的文本和字符数据类型,返回指定表达式中模式第一次出现的起始位置,如果未找到模式,则返回零。PATINDEX ( %pa

15、ttern%, expression ) 下面的示例搜索其名称包含单词“Anton”的产品的列表。Select ProductName, PATINDEX(%Anton%, ProductName)FROM ProductsCHARINDEX:返回字符串中指定表达式的起始位置。CHARINDEX ( expression1 , expression2 , start_location ) 下面的示例从数据库中的员工姓氏中搜索表达式“an”:Select Last Name, CHARINDEX(an, Last Name) AS PositionFROM Employeessqlserver sql 常用函数常用函数,语法语法 - 返回一个表中所有的字段select name from syscolumns where id=object_id(tb_usertable_online)- 获取最近添加的标识列的值set rs = identity- print len(abcdef)- 大小写转换print lower(ABCDEF)print upper(abcdef)- 去空格print lt

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号