展望2011

2007-04-27 | Transact-SQL学习整理[更新中]

上一篇 / 下一篇  2007-04-27 22:43:57 / 个人分类:数据库

PkgO'g!qrK0Transact-SQL学习整理

q2y5\`qSC3@)b0

7_5Q8@vxb0参考资料来源:http://msdn2.microsoft.com/zh-cn/library/ms310245.aspx51Testing软件测试网9w2kDS X4\

51Testing软件测试网'^];R9[;K1T4`

nvert
h5x V5L;X0是转化函数,将某种数据类型的表达式显式转换为另一种数据类型.格式:CONVERT (data_type[(length)], expression [, style]).xpression:是任何有效的表达式.data_type:目标系统所提供的数据类型,包括 bigint 和 sql_variant。不能使用用户定义的数据类型。style:日期格式样式,借以将 datetime 或 smalldatetime 数据转换为字符数据;或者字符串格式样式,借以将 float、real、money 或smallmoney 数据转换为字符数据

{(i&` I1k)e/p8a4b0

@O@.f.R?O!hGHN}0declare @game_id int
!_d1zL@'e o q0声明变量.变量是在批处理或过程的主体中用 DECLARE 语句声明的,并用 SET 或 SELECT 语句赋值。游标变量可使用此语句声明,并可用于其他51Testing软件测试网z@3w?6o/? U(L {#y a
与游标相关的语句。所有变量在声明后均初始化为 NULL。格式:DECLARE @local_variable (Transact-SQL)

*?H|T,e8I)w2e051Testing软件测试网-YaWbI;S g/J7s

select @game_id={?game_id}51Testing软件测试网sob^Jz/l
指定使用 DECLARE @local_variable 创建的指定局部变量应设置为指定表达式。{?game_id}就是右边传进来的参数变量.51Testing软件测试网[{ hf `K/m W

51Testing软件测试网rKWYY p?/A.Z8z

datediff函数
;YZ5Qk$Dr0返回跨两个指定日期的日期边界数和时间边界数。格式:DATEDIFF ( datepart , startdate , enddate ).datepart:指定应在日期的哪一51Testing软件测试网2[#?X]3x1K0s
部分计算差额的参数。startdate:计算的开始日期。enddate:计算的结束日期。

;P7LWb$aBks051Testing软件测试网%@hT {Jh8X

decimal51Testing软件测试网+U0So{P1T6F Y s
带固定精度和小数位数的数值数据类型。格式:decimal[ (p[ , s] )].p(精度):最多可以存储的十进制数字的总位数,包括小数点左边
5tH6Y1A5P/e ms0和右边的位数。s(小数位数):小数点右边可以存储的十进制数字的最大位数。小数位数必须是从 0 到 p 之间的值。

2Q'`;t-FP)B:M*F:b&j5\R0

"utH:J7H*i5`0sum51Testing软件测试网(xE/[%r_N8q_NXe
返回表达式中所有值的和或仅非重复值的和。SUM 只能用于数字列。空值将被忽略。格式:SUM ( [ ALL | DISTINCT ] expression )
6g2g1_4Fy-o Mc-G0ALL :对所有的值应用此聚合函数。ALL 是默认值。DISTINCT :指定 SUM 返回唯一值的和。expression:常量、列或函数与算术、位和字符
,Z4Q fAm3D)RaL0串运算符的任意组合。51Testing软件测试网o%NbG W7r6[

&dxLt_R hxu0IsNull
-^1^2n7^ }!y0使用指定的替换值替换 NULL。格式:ISNULL ( check_expression , replacement_value ).check_expression :将被检查是否为 NULL 的
r7ekG`Xy'J#oB+n0表达式。check_expression 可以为任何类型。replacement_value :当 check_expression 为 NULL 时要返回的表达式。如果51Testing软件测试网j$};f,oe%o*z5n%xS
check_expression 不为 NULL,则返回它的值

H+fT'F?051Testing软件测试网~&d[ E/|M2TG;kC

declare fix_cursor cursor51Testing软件测试网 yS/S Vi9P'h7tl3y
DECLARE CURSOR 定义 Transact-SQL 服务器游标的属性,例如游标的滚动行为和用于生成游标所操作的结果集的查询。OPEN 语句填充结果集,FETCH 从结果集返回行。CLOSE 语句释放与游标关联的当前结果集。DEALLOCATE 语句释放游标所使用的资源。
%_2ei7c;Nu*Vj0游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。游标总是与一条T_SQL 选择语句相关联因为游标由结果集(可以是零条、一条或由相关的选择语句检索出的多条记录)和结果集中指向特定记录的游标位置组成。当决定对结果集进行处理时,必须声明一个指向该结果集的游标。51Testing软件测试网}]Qo!W
游标允许应用程序对查询语句select 返回的行结果集中每一行进行相同或不同的操作,而不是一次对整个结果集进行同一种操作;它还提供对基于游标位置而对表中数据进行删除或更新的能力;而且,正是游标把作为面向集合的数据库管理系统和面向行的程序设计两者联系起来,使两个数据处理方式能够进行沟通。
}X \5gc0参考链接:http://msdn2.microsoft.com/zh-cn/library/ms180169.aspx
@ @ x ?s } ML0http://www.knowsky.com/344071.html

4{ N7n hG#V7A0

_B}(wcP ^:_0@+cR0if @@sqlstatus = 151Testing软件测试网` RK}d0] e
@@sqlstatus contains status information resulting from the last fetch statement for the current user session
tA,N {$xs4n0@@sqlstatus has no effect on @@error output.
@WjSm051Testing软件测试网 i2q/S QI(N3p+{ W I
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软件测试网3cB??$AS
参考资料:
{1r4S2K| K6z#D%y0http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@ebt-link;pt=41423;lang=zh?target=%25N%15_41649_START_RESTART_N%25
e~+I]~!Tu0http://www.nis.com/oracle/sql_fetch.html51Testing软件测试网J\2H:Z`2N3Y

b1k&iCzp}7ZE|0raiserror 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 occurred51Testing软件测试网 ^s#t!RRD+GW
参考资料:
/]9H6l9l+~r9d8Yv0http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookView;pt=41649;lang=zh?DwebQuery=raiserror

:VG,{kv;r4\0

]W%u S-v1P$W\ u0close fix_cursor               
k-{p[-~0deallocate cursor fix_cursor  
;iC/{Hl9C5TB[0When you are finished with the result set of a cursor, you can close it using.51Testing软件测试网bd+Ln9UC X&U
To discard a cursor, you must deallocate it using.51Testing软件测试网G#G bs?
Deallocating 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.51Testing软件测试网@@3~{DF
51Testing软件测试网8K4s)K LM2h
参考资料:
8F4h4mqw/xr0http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/51606;pt=50175/*;lang=zh;nh=1?DwebQuery=deallocate

^_kX'M7K7|!R0

Tb'v#Bt v.c&P!d0if @@error!=0  51Testing软件测试网M-r PS$}8D*tOoP
The @@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 system
i"H_w2Y%C.Y0参考资料:http://manuals.sybase.com/onlinebooks/group-as/asg1250e/sqlug/@Generic__BookTextView/41423;pt=38523/*;lang=zh?DwebQuery=@@error#X

:iIc2r+f%sxB-X_0

)yrN`sz,]E(A&u s0 

5YbY$W/ML051Testing软件测试网!]kZj/k~/U4Ge6_$P

if not exists(...)
2]!r Z`-w*h$dq;m~5eV0指定一个子查询,测试行是否存在。
y2e*n r[G0参考资料:http://msdn2.microsoft.com/zh-cn/library/ms188336.aspx

~!`.{kM'z0

O&U S6bm0exec sp_3d_prizegiving51Testing软件测试网 Te_%B|*v"E
执行 Transact-SQL 批中的命令字符串、字符串或执行下列模块之一:系统存储过程、用户定义存储过程、标量值用户定义函数或扩展存储过程。51Testing软件测试网 m;U/D^9L2e
参考资料:http://msdn2.microsoft.com/zh-cn/library/ms188332.aspx51Testing软件测试网3O+E.V;k6k"A1I

%{ q8MZ[!`0datediff(month,drawtime,:ld_dd)=0
D5OX4p.Za:S%b$Wm0格式:DATEDIFF ( datepart , startdate , enddate )51Testing软件测试网|1sfc wUx{7c4Y
返回跨两个指定日期的日期边界数和时间边界数。datepart:指定应在日期的哪一部分计算差额的参数。startdate:计算的开始日期。
.ysq[.o&sP0enddate:计算的结束日期。
5\_/t)pt zZ~$E0参考资料:http://msdn2.microsoft.com/zh-cn/library/ms189794.aspx

0XJ(xrpw"m!t3[#N051Testing软件测试网W*EjD;t.hUe}

right(region_code,4)="0000"51Testing软件测试网%f/K I)G6EERh2_%r
格式:RIGHT ( character_expression , integer_expression )51Testing软件测试网ZyX!\7EA0L&\
返回字符串中从右边开始指定个数的字符。character_expression字符或二进制数据表达式。character_expression 可以是常量、变量或
{ hj'\n0列,integer_expression正整数,指定 character_expression 将返回的字符数。51Testing软件测试网 }F#Gz^'FR
参考资料:http://msdn2.microsoft.com/zh-cn/library/ms177532.aspx51Testing软件测试网0f*c'tX3Zx&|

$g{?|"M"J&]0execute sp_statis_date_before '2007-06-06 10:00:00'
V*V:h H.Jxg0格式:51Testing软件测试网;M(C8B}(x
Execute a stored procedure or function51Testing软件测试网 Hc4U0vD`
[ { EXEC | EXECUTE } ]
8iY(k2wa,?l0    {
W |mO TUy@&gw0      [ @return_status = ]
s-d B7dC1H#CvM0      { module_name [ ;number ] | @module_name_var }51Testing软件测试网t"LykI*_*S9x C2A
        [ [ @parameter = ] { value51Testing软件测试网s8d"[aO Ju2zr/O
                           | @variable [ OUTPUT ]
3} LF }"KnI*^0                           | [ DEFAULT ]51Testing软件测试网 u s ^3LO:c
                           }
BN`6r7|&I:zQa0        ]
*ab"^0O Lu qBy0      [ ,...n ]51Testing软件测试网v1S IY+U&j/I
      [ WITH RECOMPILE ]
,N7c#_&exXw`/oZY9?0    }51Testing软件测试网X_M%[r:H.eP GK
[;]
kff Q[o0执行 Transact-SQL 批中的命令字符串、字符串或执行下列模块之一:系统存储过程、用户定义存储过程、标量值用户定义函数或扩展存储过程。
A%{Z2U8A|U{@7_wc0value:传递给模块或传递命令的参数值。如果参数名称没有指定,参数值必须以在模块中定义的顺序提供。51Testing软件测试网.g6E#O5eU9C
参考资料:http://msdn2.microsoft.com/zh-cn/library/ms188332.aspx

)},O9P*f+FR#p/]0

TAG: 数据库 SQL sybase

将测试进行到底 引用 删除 wang_sweet99   /   2010-12-17 16:58:10
太有才了
将测试进行到底 引用 删除 wang_sweet99   /   2010-12-17 16:58:01
3
jackz的个人空间 引用 删除 jackz   /   2007-05-10 20:02:02
不错不错
 

评分:0

我来说两句

Open Toolbar