日期:2014-05-18 浏览次数:20475 次
/*---------------------------- -- Author :feixianxxx(poofly) -- Date :2010-04-20 20:10:41 -- Version: -- Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) Mar 29 2009 10:27:29 Copyright (c) 1988-2008 Microsoft Corporation Enterprise Evaluation Edition on Windows NT 6.1 <X86> (Build 7600: ) -- CONTENT:SQL SERVER中一些特别地方的特别解法2 ----------------------------*/
--环境 create table test_1 ( id int, value varchar(10), t_time datetime ) insert test_1 select 1,'a','2009-04-19' union select 2,'b','2009-04-20' union select 3,'c','2009-04-21' union select 4,'d','2009-04-22' union select 5,'e','2009-04-23' union select 6,'f','2009-04-24' union select 7,'g','2009-04-25' go 我们一般通过 datepart(weekday )进行求解,比如求解星期2的记录 select * from test_1 where DATEPART(WEEKDAY,t_time+@@DATEFIRST-1)=2 /* id value t_time ----------- ---------- ----------------------- 3 c 2009-04-21 00:00:00.000 */ 这里涉及到 @@datefirst 这个系统变量,一般我们用来调节不同地方的日期习惯。 如果你觉得关于这个变量很难也懒得去依赖它调节,这里还有一种方法 你可以使用一个参照日期,通过相同星期数成7的倍数的原理进行查询 select * from test_1 where DATEDIFF(DAY,'1900-01-02',t_time)%7=0 /* id value t_time ----------- ---------- ----------------------- 3 c 2009-04-21 00:00:00.000 */
--环境 --drop table test_2 create table test_2 ( id int identity(1,1), value varchar(10) ) insert test_2 select 'abc' union all select 'Abc' union all select 'ABC' union all select 'aBc' go create clustered index in_value on test_2(value) --我先要查找 值为'ABC'的记录 要区分大小写的 select * from test_2 where value COLLATE CHINESE_PRC_CS_AS ='ABC' 按CTRL+L看执行计划 发现时聚集索引扫描 这就说明它不是SARG,不考虑使用索引 解决方法: select * from test_2 where value COLLATE CHINESE_PRC_CS_AS ='ABC' and value='ABC' go 看执行计划,结果是聚集索引查找;
在某些情况下,你可能需要跨会话的维护一些共享值,这里可以通过一些手段自动建立这样一个全局临时表够你使用 具体方法就是在master数据库中建立一个以sp_开头的特殊存储过程,并且使用'startup'标志此存储过程,这样每次重启数据库后都会自动运行此存储过程, 通过在存储过程中建立全局临时表,就达到了共享全局表的目的。 create procedure sp_Create_Global as create table ##Global ( name varchar(50), value sql_variant ) go sp_procoption 'sp_Create_Global','startup','true' go cmd->net stop mssqlserver cmd->net start mssqlserver insert ##Global values('var_1','987abc') select * from ##Global
动态批处理中 EXEC 不像 sp_executesql 一样提供接口(这里就讲输出参数) 但是也有方法去解决这个问题 --环境: create table test_3 ( id int identity(1,1), value int ) insert test_3 select 1 union select 5 union select 9 go 1.全部写入动态字符串中 exec ( 'declare @n int select @N=count(*) from test_3 select @N ' ) 2.INSERT EXEC 形式 create table #cnt(n int) insert #cnt exec('select count(*) from test_3 ') declare @cnt int set @cnt=(select N from #cnt) select @cnt 3.动态批处理直接导入临时表 create table #cnt_2(n int) exec ( 'insert #cnt_2 select count(*) from test_3' ) declare @cnt int set @cnt=(select N from #cnt) select @cnt
推荐阅读更多>
- 请教怎么每5分钟取一条数据
- 关于declare关键字解决思路
- sql server 2008的.dbk文件在sql server 2005上 不能还原,该如何处理
- 连续插入号码解决办法
- 删除表中多余的重复记录(多个字段),只留有id最小的记录的有关问题
- 请问一上sql语句的更新多行,每行的数据根据id更新的写法
- Sql2005关于远程连接有关问题
- 待 !sql 列传行有关问题
- 小弟我想更新一个表,判断这个更新是不是符合条件,如果不是的,就把更新的行移除到另外一个表中,怎样做呢
- Crystal Report 9.0,小弟有难题
- sql条件应该如何写
- SQL2000在存储过程中使用游标,该怎么处理
- 写个触发器实现以下功能:企业内部人员调动时实现部门人数的增减;当删除某部门时,将该部门对应的员工全部删除,该怎么处理
- 怎么判断表中的某个字段必须包含三个值,都包含count就加1。求高手帮助
- 求sql语句,怎么有条件的删除重复数据
- SSRS 筛选器有关问题(带图)~请!
- SQL2005导入导出没DTS向导“查询生成器”
- 怎么写这样的语句
- 各位大俠,小弟我剛在SQL 2000服務器上創建了 訂閱服務器,分發服務器,然後它自動創建了一個
- *英文版win2003+英文版SQL2000显示中文时出现的是""号*该怎么解决