一直在用SQL Server 2000,但很多问题都没深究。该总结一下了。

存储过程和用户定义函数都是“SQL语句组成的子程序,用以封装代码以便重复使用”。但区别也是显而易见的。简单说,存储过程功能强大,但调用不便,用户函数正相反。

用户定义函数的优点
可以在SQL语句中调用,直接使用返值,从而形成复杂的SQL应用。
存储过程则只能用execute命令调用,用输出参数的到返回的结果。

用户定义函数的缺点
能在函数中使用的语句有严格限制:

  1. 不支持create、drop等DDL命令
  2. insert、delete、update只能用在临时表上
  3. 不支持动态SQL
  4. 不支持“不确定”的函数,比如常用的getdate。不确定函数是指输入参数相同,返回结果可能不同的函数。全部不确定函数如下表
    @@CONNECTIONS @@TOTAL_ERRORS

变通办法
要解决用户函数的种种限制,可以巧妙运用用户函数调用存储过程。

比如,需要在函数中用getdate,就写个存储过程返回getdate的结果。然后从函数里去调用这个存储过程。需要在函数中使用update等,也可类推。

本文来自http://jijian91.com  原文http://jijian91.com/blog20070330/sql-server-procedure-vs-function.html

Google

评论

Good.Be the first to comment on this entry.

Post comment

comment has COPYRIGHT too!

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
URIs must be fully qualified (eg: http://www.domainname.com) and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.