SQL Server 2008格納プロセス例
2713 ワード
出典:http://www.jb51.net/article/54730.htm
呼び出し方式:exec GetComment 3
呼び出し方法:
declare@cnt int
exec GetCommentCount 1,@cnt out put
print@cnt
呼び出し方法:
declare@cnt int
exec@cnt=
print@cnt
CREATE proc func_withconditions
(
@first Name varhar(20)、
@lastName varhar(20)
)
AS
begin
declare@sql varhar(500)
set@sql='select*from eployee where 1=1 '
if(@first Name is not null)
set@sql=@sql+'and first_name='+
'''+@first Name+
''
if(@lastName<>'and@lastName is not null)
set@sql=@sql+'and last_name='+
'''+@lastName+
''
exec(@sql)
end
GO
呼び出し方法:
exec func_withconditions'ah'
exec func_withconditions'ah'NULL
exec func_withconditions NULL、'jhg'
-- --
create
proc GetComment
(@commentid
int
)
as
select
*
from
Comment
where
CommentID=@commentid
呼び出し方式:exec GetComment 3
-- --
create
proc GetCommentCount
@newsid
int
,
@
count
int
output
as
select
@
count
=
count
(*)
from
Comment
where
NewsID=@newsid
呼び出し方法:
declare@cnt int
exec GetCommentCount 1,@cnt out put
print@cnt
-- --
create
function
MyFunction
(@newsid
int
)
returns
int
as
begin
declare
@
count
int
select
@
count
=
count
(*)
from
Comment
where
NewsID=@newsid
return
@
count
end
呼び出し方法:
declare@cnt int
exec@cnt=
MyFunction
1print@cnt
-- --
Create
function
GetFunctionTable
(@newsid
int
)
returns
table
as
return
(
select
*
from
Comment
where
NewsID=@newsid)
go
:
select
*
from
・GetFunctionTable(2)
CREATE proc func_withconditions
(
@first Name varhar(20)、
@lastName varhar(20)
)
AS
begin
declare@sql varhar(500)
set@sql='select*from eployee where 1=1 '
if(@first Name is not null)
set@sql=@sql+'and first_name='+
'''+@first Name+
''
if(@lastName<>'and@lastName is not null)
set@sql=@sql+'and last_name='+
'''+@lastName+
''
exec(@sql)
end
GO
呼び出し方法:
exec func_withconditions'ah'
exec func_withconditions'ah'NULL
exec func_withconditions NULL、'jhg'