随笔分类 -  SQL数据库

摘要:本来只是想解决怎么把数据的行和列进行转换的,但最近觉得一些数据库SQL语句的操作,很久没用了,有点陌生。所以也就随笔记录一些简单但很基本的操作。我的数据库是MSSQL2005.第一部分主要的操作包含:数据库的创建、删除,表的增、删、改,表中数据的增、删、改、查,视图的操作。 1 --查询数据库是否存在 2 if exists ( select * from sysdatabases where [name]='TestDB') 3 print 'Yes, the DB exists' 4 else 5 print 'No, need a new one? 阅读全文
posted @ 2012-02-16 11:29 One Ivan 阅读(54326) 评论(3) 推荐(7)
摘要:游标中用到的函数,就是前一篇文章中创建的那个函数。另外,为了方便使用,把游标放在存储过程中,这样就可以方便地直接使用存储过程来执行游标了。 1 create procedure UpdateHKUNo --存储过程里面放置游标 2 as 3 begin 4 5 declare UpdateHKUNoCursor cursor --声明一个游标,查询满足条件的数据 6 for select psn_code from person where type='E' and hku_no is null 7 8 open UpdateHKUNoC... 阅读全文
posted @ 2012-01-13 10:07 One Ivan 阅读(20763) 评论(0) 推荐(1)
摘要:一、有返回值的函数View Code create function GetNo()returns varchar(20) --定义返回类型asbegin declare @maxNo int --声明一个整形的变量 declare @temp varchar(20) --声明一个与表中字段类型相同的变量 declare @result varchar(20) --声明一个返回的变量 declare @length int --声明一个变量,用于下面的截取字符串 set @temp = (select max(hku_no) fro... 阅读全文
posted @ 2012-01-12 17:12 One Ivan 阅读(576) 评论(0) 推荐(0)
摘要:【Cite:http://www.jb51.net/article/20832.htm】1. 当前系统日期、时间 select getdate()2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 例如:向日期加上2天 select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000 例如:查询目前时间最近三天的内容降序排列 select * from table where time between dateadd(day,-3,getdate()) and getdate() 阅读全文
posted @ 2011-09-27 01:27 One Ivan 阅读(725) 评论(0) 推荐(0)