2007-04-27 | Transact-SQL学习整理[更新中]
上一篇 / 下一篇 2007-04-27 22:43:57 / 个人分类:数据库
Transact-SQL学习整理51Testing软件测试网T4saR"GYU(?
7au;{J/\w0参考资料来源:http://msdn2.microsoft.com/zh-cn/library/ms310245.aspx
d8[9{Qi2E;a'g051Testing软件测试网,VWDC2W%Fnvert51Testing软件测试网E;?+m\WFu
是转化函数,将某种数据类型的表达式显式转换为另一种数据类型.格式:CONVERT (data_type[(length)], expression [, style]).xpression:是任何有效的表达式.data_type:目标系统所提供的数据类型,包括 bigint 和 sql_variant。不能使用用户定义的数据类型。style:日期格式样式,借以将 datetime 或 smalldatetime 数据转换为字符数据;或者字符串格式样式,借以将 float、real、money 或smallmoney 数据转换为字符数据51Testing软件测试网zho1x~P
j7|$GW&u
declare @game_id int51Testing软件测试网
^_I
V5Z7~j2F
声明变量.变量是在批处理或过程的主体中用 DECLARE 语句声明的,并用 SET 或 SELECT 语句赋值。游标变量可使用此语句声明,并可用于其他51Testing软件测试网O;[5Q6GR$T\$Oh]$e
与游标相关的语句。所有变量在声明后均初始化为 NULL。格式:DECLARE @local_variable (Transact-SQL)51Testing软件测试网p5B,T%K;QOw[0s:u%B:N,G5}
select @game_id={?game_id}51Testing软件测试网-e S4AO+Nr:~
指定使用 DECLARE @local_variable 创建的指定局部变量应设置为指定表达式。{?game_id}就是右边传进来的参数变量.
A^t8A:ff!xw\#u0datediff函数
y\:F;R[#hK(q?0返回跨两个指定日期的日期边界数和时间边界数。格式:DATEDIFF ( datepart , startdate , enddate ).datepart:指定应在日期的哪一
}4O;Ch5c"G3G#`0部分计算差额的参数。startdate:计算的开始日期。enddate:计算的结束日期。
xg:keJ0decimal51Testing软件测试网P~5CH!e/EE
带固定精度和小数位数的数值数据类型。格式:decimal[ (p[ , s] )].p(精度):最多可以存储的十进制数字的总位数,包括小数点左边
2n,Y} X9i;kV0h"g6R4a0和右边的位数。s(小数位数):小数点右边可以存储的十进制数字的最大位数。小数位数必须是从 0 到 p 之间的值。
sum
Q$[hGj't0返回表达式中所有值的和或仅非重复值的和。SUM 只能用于数字列。空值将被忽略。格式:SUM ( [ ALL | DISTINCT ] expression )
;uu fk(h/wha9^0ALL :对所有的值应用此聚合函数。ALL 是默认值。DISTINCT :指定 SUM 返回唯一值的和。expression:常量、列或函数与算术、位和字符51Testing软件测试网rx7om0H)o&[&C1@l
串运算符的任意组合。
5sq)O*k9_Qx-Y}g0IsNull51Testing软件测试网$H&xW!@Y
使用指定的替换值替换 NULL。格式:ISNULL ( check_expression , replacement_value ).check_expression :将被检查是否为 NULL 的51Testing软件测试网&Kur}KIY!V K
表达式。check_expression 可以为任何类型。replacement_value :当 check_expression 为 NULL 时要返回的表达式。如果
Z.VIdZH f&C0check_expression 不为 NULL,则返回它的值
LE2Cg.z
}0declare fix_cursor cursor
6N'`(wyo5~0DECLARE CURSOR 定义 Transact-SQL 服务器游标的属性,例如游标的滚动行为和用于生成游标所操作的结果集的查询。OPEN 语句填充结果集,FETCH 从结果集返回行。CLOSE 语句释放与游标关联的当前结果集。DEALLOCATE 语句释放游标所使用的资源。
HB ^xAu,M0游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标总是与一条T_SQL 选择语句相关联因为游标由结果集(可以是零条、一条或由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成。当决定对结果集进行处理时,必须声明一个指向该结果集的游标。51Testing软件测试网
u)N^"}A5tf
游标允许应用程序对查询语句select 返回的行结果集中每一行进行相同或不同的操作,而不是一次对整个结果集进行同一种操作;它还提供对基于游标位置而对表中数据进行删除或更新的能力;而且,正是游标把作为面向集合的数据库管理系统和面向行的程序设计两者联系起来,使两个数据处理方式能够进行沟通。51Testing软件测试网0ybA:XE
Gt
参考链接:http://msdn2.microsoft.com/zh-cn/library/ms180169.aspx
y*pB.n8jMg NjEjD0http://www.knowsky.com/344071.html51Testing软件测试网%MTF(O!OMl-nN
if @@sqlstatus = 1
-lslqT7nC5Xb0@@sqlstatus contains status information resulting from the last fetch statement for the current user session51Testing软件测试网"Ia6r]@x
@@sqlstatus has no effect on @@error output.51Testing软件测试网.D|TG;y5Q.at O
51Testing软件测试网#RFb;z9j
fetch:This statement retrieves one row from a group of rows that were identified by the SQL SELECT statement. After a SELECT statement executes, you can use the FETCH statement to sequentially retrieve one row at a time from the selected rows.51Testing软件测试网{'j2~F-cfS ?1Qp
参考资料:51Testing软件测试网 MF'O(i)W6|%w
http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@ebt-link;pt=41423;lang=zh?target=%25N%15_41649_START_RESTART_N%25
-WP k B4e0I&i-AB.`(l0http://www.nis.com/oracle/sql_fetch.html51Testing软件测试网H~[7wi;p4D,p?x;W
p!p$olT)voE0raiserror 33333 " Selection failed in fix_cursor on database!" raiserror both displays a user-defined error or local variable message on the user's screen and sets a system flag to record the fact that an error has occurred
)X,?9l/v!Kf0参考资料:51Testing软件测试网W([N(njh({Yr
http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookView;pt=41649;lang=zh?DwebQuery=raiserror
'ckYj5r)A,@[/t0close fix_cursor 51Testing软件测试网0ppa1H1ZR
deallocate cursor fix_cursor
k.Kq:Cs3{U @j)^0When you are finished with the result set of a cursor, you can close it using.
T}.i%EO3su}&B}0To discard a cursor, you must deallocate it using.
YI^X!].v0Deallocating a cursor frees up any resources associated with the cursor, including the cursor name. You cannot reuse a cursor name until you deallocate it. If you deallocate an open cursor, Adaptive Server automatically closes it. Terminating a client connection to a server also closes and deallocates any open cursors.
(|
z~_8^8u
_A0tM0
4G+|uz$J/U"jz0参考资料:51Testing软件测试网u1[^{Uwl5U!r$v;j
http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/51606;pt=50175/*;lang=zh;nh=1?DwebQuery=deallocate
7B.Q!`^1lov0if @@error!=0
1Jq.lueC#~,yM0The @@error global variable is commonly used to check the error status of the most recently executed batch in the current user session. @@error contains 0 if the last transaction succeeded; otherwise @@error contains the last error number generated by the system51Testing软件测试网"c}DY9lN
参考资料:http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/41423;pt=38523/*;lang=zh?DwebQuery=@@error#X
rMT0a1} uB0 51Testing软件测试网f5]9Uh)qJ9P(C g\
4ir,Nf)d(Zt0if not exists(...)51Testing软件测试网f nx.J3Dm$}L#Y
指定一个子查询,测试行是否存在。
1]Q8E)`Y,?`[\0参考资料:http://msdn2.microsoft.com/zh-cn/library/ms188336.aspx51Testing软件测试网5N$o"K6n$F1j7{0D.yRm3~
VW"? JhP9Fi0exec sp_3d_prizegiving51Testing软件测试网0k5p'u5RM2iT
执行 Transact-SQL 批中的命令字符串、字符串或执行下列模块之一:系统存储过程、用户定义存储过程、标量值用户定义函数或扩展存储过程。
'yKU;_h6KRU;M4u$K0参考资料:http://msdn2.microsoft.com/zh-cn/library/ms188332.aspx51Testing软件测试网(IB"gF?
datediff(month,drawtime,:ld_dd)=0
,b,J%m m
AO0格式:DATEDIFF ( datepart , startdate , enddate )51Testing软件测试网-LvjRP;B;@9@Y@z
返回跨两个指定日期的日期边界数和时间边界数。datepart:指定应在日期的哪一部分计算差额的参数。startdate:计算的开始日期。
Xk1K g
Mz7eCGh0enddate:计算的结束日期。51Testing软件测试网7p oWW9YKlI
参考资料:http://msdn2.microsoft.com/zh-cn/library/ms189794.aspx51Testing软件测试网6j,o v8K|+z
right(region_code,4)="0000"
A-v$N&gw*mZe0格式:RIGHT ( character_expression , integer_expression )
e*t1N'p8C0返回字符串中从右边开始指定个数的字符。character_expression字符或二进制数据表达式。character_expression 可以是常量、变量或
N5q&t3B8m