1 :普通SQL语句可以用Exec执行
例: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N
2:字段名,表名,数据库名之类作为变量时,必须用动态SQL
错误: declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
&nb ......
1.说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 11
2.说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
SQL: insert into b(a, b, c) select d,e,f from a;
3.说明:显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
4.说明:外连接查询(表名1:a 表名2:b)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUTER JOIN b ON a.a = b.c
5.说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff(’minute’,f开始时间,getdate())>5
6.说明:两张关联表,删除主表中已经在副表中没有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )
说明:–
SQL:
SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE
from TABLE1,
(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE
from (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND
from TABLE2
WHERE TO_CHAR(UPD_DATE,’YYYY ......
SQL当前日期获取技巧
select getdate() //2003-11-07 17:21:08.597
select convert(varchar(10), getdate(),120) //2003-11-07
select convert(char(8),getdate(),112) //20031107
select convert(char(8),getdate(),108) //17:22:18
select datepart(year,getdate()) //2007
当前日期
select CONVERT(varchar(10), getdate(), 120)
一个月第一天的SQL 脚本:
SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)
本周的星期一
SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
一年的第一天
SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
季度的第一天
SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)
当天的半夜
SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
上个月的最后一天
&nbs ......
select * from pet;
insert into pet values('Liujingwei','Liuchao','cat','f','1984-04-18',null);
UPDATE pet set birth='1989-08-31' WHERE name='Slim';
select * from pet WHERE birth>'1998-1-1';
SELECT * from pet WHEREselect * from pet;
insert into pet values('Liujingwei','Liuchao','cat','f','1984-04-18',null);
UPDATE pet set birth='1989-08-31' WHERE name='Slim';
select * from pet WHERE birth>'1998-1-1';
SELECT * from pet WHERE species='dog' and sex='f';
select * from pet WHERE species='snake' or species='cat';
SELECT * from pet WHERE (species='cat' and sex='m') or (species='dog' and sex='f');
SELECT name,birth from pet;
SELECT owner from pet;
SELECT distinct owner from pet;
SELECT name,species,birth from pet where species='dog' or species='cat';
=======================================================================
select name,birth from pet ORDER BY birth;
select name,birth from pet order by birth desc;
select name,species,birth from pet order by ......
Select CONVERT(varchar(100), GETDATE(), 23);
返回形式:2008-11-29
Select CONVERT(varchar(100), GETDATE(), 102)
返回形式:2008.11.29
Select CONVERT(varchar(100), GETDATE(), 101)
返回形式:11/29/2008
更多详情请参见如下列表:
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM
Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06
Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16
Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06
Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06
Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06
Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06
Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06
Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46
Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM
Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06
Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16
Select CONVERT(varchar(100), GETDATE(), 12): 060516
Select CON ......
在使用NHibernate过程中经常会使用到复杂的sql查询,但是使用hql又比较麻烦的情况下,我们往往都会想到采用原始的sql来执行。但是如何利用NHibernate来执行sql呢?问题来了,在NHibernate中也有AdoTemplate的方法可以执行sql的,但是这里要介绍的是另外一种方法:CreateSQLQuery。以下部分例子源自于网络。
实例一(源自于http://blog.csdn.net/canduecho/archive/2009/05/04/4149930.aspx,感谢博主分享):
Nhibernate中CreateSQLQuery用法实例:
涉及的表:
Cake{
Id ,
CakeName
。。。。
}
CakeSize{
CakeId,-为外键,对应Cake表的字段Id
Size
}
(其中ISession session = NHibernateHelper.GetCurrentSession();)
用法一(返回数值):
ISQLQuery query = session.CreateSQLQuery("SELECT COUNT(Id) AS C from Cake").AddScalar("C", NHibernateUtil.Int32);
int c = Convert.ToInt32(query.UniqueResult());
或int c = query.UniqueResult<int>;//使用此方法发现sql执行了两次,故不推荐使用。
用法二(返回对象实体):
ISQLQuery query = session.CreateSQLQuery("select * from cake c").AddEntity("CAKE.DataTransfer.Entities.C ......