081219练习set rowcount,set nocount no
- --设置返回的行数
- set rowcount 2
- select * from Ttest
- --设置是否返回影响的行数
- set nocount no
- select * from Ttest
- set nocount off
081218练习一下
- use lyds#db
declare @lstr nvarchar(50)
declare @rstr nvarchar(50)
declare @str nvarchar(50)
declare @iint int
set @lstr=' ltrim '
set @rstr=' rtrim '
set @str='12345 '
set @iint=len(@str)
update t_Goods set summary='summary'
+ '左' + ltrim(@lstr)
+ '右' + rtrim(@rstr)
+ '截取' + left(@str,3)
+ 'end'
where gid=2 - print @iint
--select * from t_Goods
081216
判断表或存储过程是否存在
- select * from sysobjects where name='表名'
- select * from sysobjects where name='存储过程名'
初始化表
truncate table ttest
1.左连接,右连接。
2.set nocount on 和set nocount off
默认情况下,存储过程将返回过程中每个语句影响的行数。如果不需要在应用程序中使用该信息(大多数应用程序并不需要),请在存储过程中使用 SET NOCOUNT ON 语句以终止该行为。根据存储过程中包含的影响行的语句的数量,这将删除客户端和服务器之间的一个或多个往返过程。尽管这不是大问题,但它可以为高流量应用程序的性能产生负面影响
当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。当 SET NOCOUNT 为 OFF 时,返回计数。
如果存储过程中包含的一些语句并不返回许多实际的数据,则该设置由于大量减少了网络流量,因此可显著提高性能。
即使当 SET NOCOUNT 为 ON 时,也更新 @@ROWCOUNT 函数。 当 SET NOCOUNT 为 ON 时,将不给客户端发送存储过程中的每个语句的 DONE_IN_PROC 信息。当使用 Microsoft® SQL Server™ 提供的实用工具执行查询时,在 Transact-SQL 语句(如 SELECT、INSERT、UPDATE 和 DELETE)结束时将不会在查询结果中显示"nn rows affected"。
在你统计大量的数据的数量,而又不需要详细的查看每一条记录的时候可以用OFF
3.@@identity中包含语句生成的最后一个标识值。
http://www.cnblogs.com/czh-liyu/archive/2007/12/22/1010169.html
4.
- @@Error
- select @CurrentError = @@Error
- IF @CurrentError != 0
- BEGIN
- GOTO ERROR_HANDLER
- END
5.
- use lyds#db
- declare
- @start datetime,
- @end datetime
- set
- @start='2008 11 24'
- set
- @end='2008 11 26'
- select distinct sid,sdate from t_Storein where sdate between @start and @end
- --似乎distinct只能对应一列,多了就失效。
6.关于distinct的变通方法。
- use lyds#db
- declare
- @start datetime,
- @end datetime
- set
- @start='2008 11 24'
- set
- @end='2008 11 26'
- select sid,max(sdate) from t_Storein where sdate between @start and @end group by sid
- --似乎distinct只能对应一列,多了就失效。
- --高手的方法,就是用max再group by
- --select distinct sid,convert(varchar(10),sdate,120) from t_Storein where sdate between
- @start and @end
- StringBuilder sb = new StringBuilder();
- sb.Append("insert into t_StoreIn select * from t_StoreIn_Temp;");
- sb.Append("delete from t_StoreIn_Temp");
7.这种等值连接,没有直观概念,只能理解到这里,即其中一个表的关键值是连接条件,就不会有问题。
select b.*,a.gname 物品名称 from t_StoreIn b,t_Goods a where b.gid=a.gid
081202
- declare @cname
- select @cname=cname from test_cursor where cid=1
- 注释方法
- --===============================
- -- 发放指定职员的工资
- --作者:张树强
- --===============================
- /***********************************************
- 作者:XXX
- 日期:
- ***********************************************/
- create procedure sf_当月工资发放 @职员编号 char(10)
- as
- begin transaction
- update 月工资统计表 set 发放否='是' from 月工资统计表
- where 职员编号=@职员编号
- insert into 工资发放历史表
- select * from 月工资统计表
- where 职员编号=@职员编号
- delete 月工资统计表 where 职员编号=@职员编号
- commit
- GO