SQL CLR 中的存储过程
如何在 SQLCLR 中使用 C# 编写存储过程?
目前我正在使用 SQL Server Management Studio,并使用 T-SQL 编写存储过程,例如 create proc sp_item as ....
。
How do you write a stored procedure using C# in SQLCLR?
Currently I am using SQL Server Management Studio and I write stored procedures using T-SQL such as create proc sp_item as ....
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅:
基本上,有 Visual Studio 模板它允许您开始使用 SQL CLR 项目。填写空白,编写实际代码,甚至可以直接从 Visual Studio 中将这些 CLR 程序集部署到 SQL Server 中。
需要注意的是:我会避免在 SQL CLR 存储过程中进行基于集合的更新和插入以及类似的操作 - 为此,T-SQL 存储过程明显更好更快。
SQL-CLR 非常适合扩展 SQL Server,例如字符串操作、日期处理、调用Web 服务或其他功能的能力。我建议反对尝试用 C# SQL CLR 存储过程替换所有 T-SQL 存储过程只是因为你可以或只是因为它很酷 -使用正确的工具完成正确的工作!对集合的大量操作最好留给 T-SQL。
See:
Basically, there are Visual Studio templates which allow you to get started with SQL CLR projects. Fill in the blanks, write your actual code, and you can even deploy those CLR assemblies into SQL Server directly from within Visual Studio.
One word of caution: I would refrain from doing set-based updates and inserts and stuff like that in a SQL CLR stored proc - for that, T-SQL stored procs are just plain better and faster.
SQL-CLR is great to extend SQL Server with stuff like string manipulation, date handling, ability to call e.g. web services or other stuff. I would recommend against trying to replace all T-SQL stored procs with C# SQL CLR stored procs just because you can or just because it's cool - use the right tool for the right job! Mass operations on sets are better left to T-SQL.
除了 Marc 提供的链接之外,我还编写了一个关于编写 SQLCLR 表值函数的简短教程(仅供参考,需要免费注册才能查看 SQL Server Central 上的文章):
具有全流处理的 CLR 表值函数示例 (STVF / TVF)
我还撰写了一系列有关使用 SQLCLR 的文章:
SQLCLR 的阶梯
此外,您不应该使用
sp_
作为存储过程名称中的前缀。这是一种特殊的语法,会降低性能,因为 SQL Server 首先在master
中检查该存储过程,然后然后,如果在那里找不到,它将搜索当前的存储过程。数据库。In addition to the links provided by Marc, I also wrote a short tutorial on writing SQLCLR Table-Valued Functions (FYI, free registration is required to view articles on SQL Server Central):
CLR Table-Valued Function Example with Full Streaming (STVF / TVF)
I have also been writing a series of articles about working with SQLCLR in general:
Stairway to SQLCLR
Also, you should not use
sp_
as a prefix in stored procedure names. That is a special syntax that degrades performance due to causing SQL Server to first check for that stored procedure inmaster
, and then, if not found there, it will search the current database.