SQL存储过程基础语法
一.注释
-- 单行注释,从这到本行结束为注释sql 语法,类似C++,c#中//
/* … */ 多行注释,类似C++,C#中/* … */
二.变量(int, smallint, tinyint, decimal,float,real, money ,smallmoneysql 语法, text ,image, char, varchar。。。。。。)
语法:
DECLARE
{
{@local_variable data_type}
} [,...n]
例如:
declare @ID int --申明一个名为@ID的变量,类型为int型
三.在SQL Server窗口中打印出变量的值
语法:
PRINT 'any ASCII text' | @local_variable | @@FUNCTION | string_expr
四.变量赋值
例如:
--从数据表中取出第一行数据的ID,赋值给变量@id,然后打印出来
Declare @ID int
Set @ID = (select top(1) categoryID from categories)
Print @ID
在SQL中,我们不能像代码那样直接给变量赋值,例如@id = 1,如果要达到这样的功能,可以这样写:
Declare @ID int
Set @ID = (select 1) -- 类似 @ID=1
Select @id=1 -- 类似 @ID=1
Print @ID
五.变量运算(+,-,*sql 语法,/,……)
以下必要时候省略变量申明
Set @ID = (select 1+5) --类似 @ID=1+5
Set @ID=(select 1-@ID) --类似 @ID=1-@ID
六.比较操作符
? >(greater than).
? <(less than).
? = (equals).
? <= (less than or equal to).
? >= (greater than or equal to).
? != (not equal to).
? <>(not equal to).
? !< (not less than).
? !> (not greater than).
没什么说的
七.语句块:Begin … end
将多条语句作为一个块,类似与C++,C#中的{ }
例如:
Begin
Set @ID1 = (select 1)
Set @ID2 = (select 2)
End
八.If, if…else…
语法:
IF Boolean_expression
{sql_statement | statement_block}
[ELSE
{sql_statement | statement_block}]
例如:
If @id is not null
Print ‘@id is not null
if @ID = 1
begin
Set @ID = (select 1 + 1)
end
else
begin
set @ID=(select 1+2)
end
上面的例子用到了比较操作符,语句块,和IF的语法。
九.执行其他存储过程 EXEC
例如
EXEC dbo.[Sales by Year] @Beginning_Date=’1/01/90’, @Ending_Date=’1/01/08’
十.事务
语法:
BEGIN TRAN[SACTION] [transaction_name | @tran_name_variable]
例如
BEGIN TRAN
-- 做某些操作,例如In
相关文档:
删除数据库表中某一字段的重复纪录,只想保留一条记录可用以下方法:
执行以下语句:
declare @max integer,@id varchar(18) --此处变量类型根据字段类型设置
declare cur_rows cursor local for select 字段,count(*) from 表名group by 字段 having count(*) > 1
open cur_rows
fetch cur_rows into @id,@max
wh ......
从dateTime类型数据中获取季度:
select cast(datepart(q,sign_date) as varchar(2))+'季度'
一.sql server日期时间函数
1. 当前系统日期、时间
select getdate()
2. dateadd 在向指定日期加上一段时间 ......
解压 SQLServer2005SP3-KB955706-x86-CHS.exe 文件(使用 WinRAR 可直接解压,或使用 /extract)
解压后文件夹 搜索 “*.msi *.msp” ,然后复制所有搜索到的文件 放到 D:\SQL2005\SP3 中.
提取 cs_sql_dev_all_dvd.iso(SQL Server 2005 开发版) 中的 x86 版本,放到 D:\SQL2005\MSSQL 中;此时 D:\SQL2005\M ......
无论是使用手工试探还是使用安全测试工具,恶意攻击者总是使用各种诡计从你的防火墙内部和外部攻破你的SQL服务器系统。既然黑客在做这样的事情。你也需要实施同样的攻击来检验你的系统的安全实力。这是理所当然的。下面是黑客访问和攻破运行SQL服务器的系统的十种诡计。
1.通过互联网直接连接
这些连接可以用来攻击没有防 ......