关于存储过程的一点小结

当 SET NOCOUNT 为 ON 时,不返回计数(表示受 Transact-SQL 语句影响的行数)。当 SET NOCOUNT 为 OFF 时,返回计数。
如果存储过程中包含的一些语句并不返回许多实际的数据,则该设置由于大量减少了网络流量,因此可显著提高性能。

SET 不返回结果
     SELECT 返回结果 注:(SET是设置的意思 SELECT是选择的意思 )

 

例:DECLARE @str1 varchar(10) @int1 int 
SET @str1
='string' 
SELECT @int1
=123 
PRINT 
'str1:'+@str1 
PRINT 
'int1:'+STR(@int1) 
这个语句 SET 和 SELECT 有什么区别??
都可以给变量赋值,像给单个变量赋值时,如果不涉及到数据表操作,通常使用SET关键字,如果是多个变量赋值,并且这些值是从数据表中读取的情况下,一般都采用SELECT赋值。

 

 

declare
@str1 varchar(
10),
@str2 varchar(
20),
@int1 
int,
@int2 
int
set @str1='panjun'
select @str2
='luhan'
set @int1=
(
select top 
1 convert(int,left(au_id,1)) from authors
)

select @int2=
(
select top 
1 convert(int,left(au_id,1)) from authors
order by au_id desc
)
print @str1
print @str2
print @int1
print @int2

从存储过程获得所要的结果,或者在一个存储过程中调用另一个存储过程

 

CREATE PROCEDURE panjun1

@total 
int output
 AS
set nocount on
select @total
=
(
select sum(typeid)
from news
)
GO

 

 

CREATE PROCEDURE panjun2
@id 
int,
@total 
int output
 AS
set nocount off
select @total
=
(
select sum(typeid)
from news
where id>@id
)
GO

 

我在前台的数据分析器中测试代码

 

use luntan
declare @total 
int
declare @id 
int
set @id=4
execute panjun1 @total output
print @total
execute panjun2 @id,@total output
print @total
print @id
go

 

同时收藏一个人的相似代码

 

在开发阶段,有时不想在c#中处理一些表中的字段内容,直接在存储过程中处理,以下是我的在开发时,在存储过程中处理日期时,通过一个存储过程中调用另一个存储过程的实现方法,第一次运用这种,刚开始不会的,在网上查了一下,感觉有必要写下来,以便下会遇到时方便查阅!刚学存储过程不长,有些代码处理,对高手来说,可能很差劲,还望高手指正,本人将加一修改!谢谢

--外部传参数如 2007年6月25日,处理完后为25-Jun-07
CREATE procedure pro_test
@date_1 varchar(255)
as
--保存年
declare @year varchar(4)
--保存月
declare @month varchar(2)
--保存日
declare @day varchar(2)
--根据@date_1调用另一个存储过程,返回与@month相对应的英文
declare @result varchar(20)
--保存最终处理结果25-Jun-07
declare @ymd varchar(20)

begin
set @year = substring(@date_1,1,4);
set @month = substring(@date_1,charindex('',@date_1)+1,charindex('',@date_1)-charindex('',@date_1));
--清空月前面的'0'
if left(@month,1= "0"
    
set @month =substring(@month,2,1);
--调用另一个存储过程,请注意output
exec pro_test2 @month@result output;

set @day = substring(@date_1,charindex('',@date_1)+1,len(@date_1)-charindex('',@date_1));
--清空日前面的'0'
if left(@day,1="0"
    
set @day = substring(@day,2,1);
set @ymd = @day+'-' +@result+'-' +@year;
select @ymd;
select @year as y;
select @month as m;
select @day as d;
select @result
select @day+'-' +@result+'-' +@year

end
GO

另一个存储过程,用来处理月
CREATE procedure pro_test2
@month varchar(255),
@result varchar(255) output
as
set @result =
case 
    
when @month = '1' then 'Jan'
    
when @month = '2' then 'Feb'
    
when @month = '3' then 'Mar'
    
when @month = '4' then 'Apr'
    
when @month = '5' then  'May'
    
when @month = '6' then'Jun'
    
when @month = '7' then 'Jul'
    
when @month = '8' then 'Aug'
    
when @month = '9' then 'Sep'
    
when @month = '10' then  'Oct'
    
when @month = '11' then  'Nov'
    
else  'Dec'
end
select @result ;
GO
追加一点:case的用法,case的用法,如c#中switch的用法相类试,但case执行完后要返回内容,而switch中,case的直接在case中代码段中,处理逻辑!
posted on 2008-07-19 11:03  小顾问  阅读(448)  评论(0编辑  收藏  举报