sql常用语句

查询为空的记录: 字段 is null
重复记录只显示一次select distinct 字段名 from 表名 
 distinct必须放在其它字段开头

sql表间数据复制

全部复制select * into NewTable from OldTable

复制结构select * into NewTable from OldTable where 1=2

复制数据insert into NewTable (y1,y2,y3) select y1,y2,y3 from OldTable

查询开始日期与结束日期之前的数据
select * from userlog
where inpudate>='2009-10-10' and inpudate<'2009-10-11'
 
查询大于或小于指定日期的数据
SELECT * FROM [Table] WHERE  datediff(n,Time,getdate())>7
(查询比现在时间超过7分钟的记录)
DateDiff 函数
返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目。
语法
DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
设置
interval 参数的设定值如下:
设置 描述
yyyy 年  q 季 m 月 y 一年的日数 d 日 w 一周的日数 ww 周 h 时 n 分钟 s 秒
 
IF语句用法
if 条件 begin 处理 end
===>SQLServer       Connect       String    
本地连接字符Data Source=ONDA\SQLEXPRESS;Initial Catalog=fangqm;Integrated Security=True
     
      Driver=SQL   Server;Database=fwbmk;Server=127.0.0.1;UID=sa;PWD=xx;       (自带)        
      使用标准安全级别:        
      strConnect       =       _T("Provider=sqloledb;Data       Source=MyServerName;Initial       Catalog=MyDatabaseName;User       Id=MyUsername;Password=MyPassword;");        
      使用信任连接:        
      strConnect       =       _T("Provider=sqloledb;Data       Source=MyServerName;Initial       Catalog=MyDatabaseName;Integrated       Security=SSPI;");        
      指定SQLServer实例:        
      strConnect       =       _T("Provider=sqloledb;Data       Source=MyServerName\MyInstanceName;Initial       Catalog=MyDatabaseName;User       Id=MyUsername;Password=MyPassword;");        
      本地数据库:        
      strConnect       =       _T("Provider=sqloledb;Data       Source=(local);Initial       Catalog=myDatabaseName;User       ID=myUsername;Password=myPassword;");        
      网络数据库:        
      strConnect       =       _T("Provider=sqloledb;Network       Library=DBMSSOCN;Data       Source=130.120.110.001,1433;Initial       Catalog=MyDatabaseName;User ID=MyUsername;Password=MyPassword;");

修改SQL数据库dbo所有者

20110825来自:奎宇工作室可以解决discuz论坛,对象名 'dnt_templates' 无效,出现这种问题,是数据库拥用者不对引起无权,
解决办法,更改数据库当前的拥用者,我是把库的拥用者全改成dbo
下载附件,在查询分析器里执行即可解决

--如果是要用户表/存储过程/视图/触发器/自定义函数一齐改,则用游标(不要理会错误提示)
declare tb cursor local for
select 'sp_changeobjectowner ''['+replace(user_name(uid),']',']]')+'].['
+replace(name,']',']]')+']'',''dbo'''
from sysobjects
where xtype in('U','V','P','TR','FN','IF','TF') and status>=0
open tb
declare @s nvarchar(4000)
fetch tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch tb into @s
end
close tb
deallocate tb
go
请使用浏览器的分享功能分享到微信等