gxh973121

博客园 首页 新随笔 联系 订阅 管理

读查询语句示例:
     Declare @count int

1    set @strSql=N'select @a= count(*) from ['+ @tblName + '] where  1=1 '+   @strWhere
2    exec sp_executesql  @strSql ,N'@a int output',@Count output
3    select @Count
    
要点:
 1.利用系统存储过程 sp_executesql
 2. 在要执行的Sql文中加入参数,如 "@a",在sp_executesql的参数  声 明中要指定参数的类型,参数的方向。
 3. sp_executesql的每个字符类型的参数都要是 n开头的数据类型,如是nvarchar 不能是      varchar,否则会报错 “过程需要类型为 'ntext/nchar/nvarchar' 的参数”.

exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output

  读存储过程示例:

create procedure ProTest
(
         @name varchar(10),
         @money int output
)
as
begin
        if(@name='1')
                  set @money=1000
        else
                  set @money=2000
end

这个只是一个简单的示例,这个存储过程返回的是@money 这个参数的值,那么当我们在另外一个存储过程中调用此存储过程的时候如何获取这个参数呢,方法如下:


declare @m int ---用来接收返回值的变量
exec ProTest @name='1',@money=@m output --一定要注名是output

就这么简单,我们就获得了返回值,然后就可以利用它了

posted on 2009-04-06 11:39  gxh973121  阅读(763)  评论(1编辑  收藏  举报