sql编程

1、函数:不能改变任何数据,只能给一个数据,返回数据,即查询功能

2、触发器:存储过程中自动执行(系统已不用,项目开发部还是在用),e开头的都是带触发器的视图。

3、定义变量:(不加@,即为游标)

--变量在若干情况下需要初始化
declare @i varchar(2000)
set @i=''
select @i=@i+username+';' from myUser where UserName is not null
print @i

4、定义表变量:

DECLARE @变量名  TABLE(列名1 类型,列名2,类型……)
DECLARE @CstTemp TABLE 
 (
      CstGUID varchar(40),
      CstName varchar(10)
)

5、临时表:

使用select into,存在内存不用需要dorp掉 ,表名前加#
select identity(int,1,1)as id
, buguid,buname 
into #t from mybusinessunit

6、循环表:

declare @i int
select @i=count(*) from #t
declare @j int
set @j=1
declare @s varchar(2222)
while @j<=@i
 begin
  select @s=buname from #t where id=@j
  print @s
  set @j=@j+1
 end

7、exec执行sql:使用程序拼出sql

exec('declare @i int set @i=5 print @i')

 8、表函数:

create function f1(@a int,@b int)
returns int
as
begin
 return @a+@b
end

print dbo.f1(2,3)

9、视图:(里面不允许使用 * 号),视图的增删改需要使用触发器

select view s
as
select  字段 from biao

if OBJECT_ID('te') is not null
drop view te
go
create view te
 as
 ....

 10、存储过程:

alter proc p1 (@i int=1 ,@j int output)
as 
begin 
 set @j=@i
end
declare @j1 int
exec p1 2,@j1 OUTPUT
print @j1

 

 

posted @ 2013-12-26 16:40  ps十一郎  阅读(176)  评论(0编辑  收藏  举报