这sql 语句写的真的很不错!
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([col1] bigint,[col2] varchar(6),[col3] varchar(6))
insert [tb]
select 130126200201000275,'王文霞','靳川川' union all
select 130126200201000275,'王文霞','靳澤' union all
select 130126200201000275,'王文霞','aaaa' union all
select 130126200201000276,'周書娥','程思超' union all
select 130126200201000276,'周書娥','程佼佼' union all
select 130126200201000277,'風菊','程青青' union all
select 130126200201000277,'風菊','程二小' union all
select 130126200201000278,'張英桃','程大慶' union all
select 130126200201000278,'張英桃','程小花'
--------------开始查询--------------------------
select * from tb
select col2,col3 from
(
select col1,col2,col3 from [tb] t
where not exists(select * from tb where col1=t.col1 and col2=t.col2 and col3>t.col3)
union all
select col1,'',col3 from [tb] t
where exists(select 1 from tb whe ......
SQL时间函数
--日期转换参数,值得收藏
select CONVERT(varchar, getdate(), 120 )2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')20040912110608
select CONVERT(varchar(12) , getdate(), 111 )2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )20040912
select CONVERT(varchar(12) , getdate(), 102 )2004.09.12
其它我不常用的日期格式转换方法:
select CONVERT(varchar(12) , getdate(), 101 )09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )11:06:08
select CONVERT(varchar(12) , getdate(), 109 )09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )12 09 2004 1
select CONVERT ......
在存储过程中经常需要执行程序组成的SQL语句,可以使用exec(@sql),
也可以使用exec sp_executesql @sql。但使用sp_executesql要优于exec,建议使用 sp_executesql 而不要使用 EXECUTE 语句执行字符串。支持参数替换不仅使 sp_executesql 比 EXECUTE 更通用,而且 还使 sp_executesql 更有效,因为它生成的执行计划更有可能被 SQL Server 重新使用。
但如果使用sp_executesql ,全局变量比如@@rowcount将没有正确值,而exec有,如果想返回该SQL中某一字段的值可使用他的参数,例如:
@sql='select @id=id from table'
exec sp_executesql @sql N'@id int out ',@id out
print @id ......
【SQL SERVER 数据库实用SQL语句】
1.按姓氏笔画排序:
Select * from TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分页SQL语句
select * from(select (row_number() OVER (ORDER BY tab.ID Desc)) as rownum,tab.* from 表名 As tab) As t where rownum between 起始位置 And 结束位置
3.获取当前数据库中的所有用户表
select * from sysobjects where xtype='U' and category=0
4.获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')
5.查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
6.查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='P'
7.查询用户创建的所有数据库
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
或者
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
8.查询某一个表的字段和数据类型
select column_name,data_type from infor ......
课程 一 PL/SQL 基本查询与排序
本课重点:
1、写SELECT语句进行数据库查询
2、进行数学运算
3、处理空值
4、使用别名ALIASES
5、连接列
6、在SQL PLUS中编辑缓冲,修改SQL SCRIPTS
7、ORDER BY进行排序输出。
8、使用WHERE 字段。
一、写SQL 命令:
不区分大小写。
SQL 语句用数字分行,在SQL PLUS中被称为缓冲区。
最后以;或 / 结束语句。
也可以用RUN来执行语句
二、例1:SQL> SELECT dept_id, last_name, manager_id
2 from s_emp;
2:SQL> SELECT last_name, salary * 12, commission_pct
2 from s_emp;
对于数值或日期型的字段,可以进行相应的四则运算,优先级与标准的高级语言相同。
SQL> SELECT last_name, salary, 12 * (salary + 100)
2 from s_emp;
三、列的别名ALIASES:
计算的时候特别有用;
紧跟着列名,或在列名与别名之间加“AS”;
如果别 ......
课程 一 PL/SQL 基本查询与排序
本课重点:
1、写SELECT语句进行数据库查询
2、进行数学运算
3、处理空值
4、使用别名ALIASES
5、连接列
6、在SQL PLUS中编辑缓冲,修改SQL SCRIPTS
7、ORDER BY进行排序输出。
8、使用WHERE 字段。
一、写SQL 命令:
不区分大小写。
SQL 语句用数字分行,在SQL PLUS中被称为缓冲区。
最后以;或 / 结束语句。
也可以用RUN来执行语句
二、例1:SQL> SELECT dept_id, last_name, manager_id
2 from s_emp;
2:SQL> SELECT last_name, salary * 12, commission_pct
2 from s_emp;
对于数值或日期型的字段,可以进行相应的四则运算,优先级与标准的高级语言相同。
SQL> SELECT last_name, salary, 12 * (salary + 100)
2 from s_emp;
三、列的别名ALIASES:
计算的时候特别有用;
紧跟着列名,或在列名与别名之间加“AS”;
如果别 ......
error:40错误 原因是因为 SQL Server2005 默认的设置为不允许远程连接,就会导致此失败。 提示:provider:命名管道提供程序, error:40-无法打开到 SQL Server 的连接)。实例(默认是“SQLEXPRESS” 方法: A:开始->配置工具->SQL Server 外围应用配置器->服务和连接外围应用配置器 B:选database Engine的“远程连接”-》本地连接和远程连接 。设置为使用 TCP/IP即可。 ......