MS SQL Server 2005 中的存储过程
这里我有一个场景:当我按下客户端应用程序(用 Delphi 开发)中的按钮时,会激活一个存储过程。存储过程首先为 select 语句声明一个游标,该语句返回两列 - BankID 和 BankCategoryID。然后我需要将游标内的每一行提取到记录中并检查 BankCategoryID 并根据 BankCategoryID 返回结果集,如下所示
CASE WHEN fetched_record.BankCategoryID=1 THEN
SELECT STATEMENT1 WHEN fetched_record.BankCategoryID=2 THEN
SELECT STATEMENT2 and so on...
:我将从上述任何情况检索到的结果集返回到我的客户端应用程序。这可能吗?
Here I got a scenario: When I press a button in client application (developed in Delphi) a stored procedure is activated. The stored procedure first declares a cursor for a select statement which returns two columns-BankID and BankCategoryID.Then I need to fetch each row inside the cursor into a record and check for the BankCategoryID and return a resultset according to the BankCategoryID like:
CASE WHEN fetched_record.BankCategoryID=1 THEN
SELECT STATEMENT1 WHEN fetched_record.BankCategoryID=2 THEN
SELECT STATEMENT2 and so on...
and then I return the result set retrieved from any of the above cases to my client application. Is thi possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您想使用
IF
作为游标内的控制语句?这是一个示例 TSQL 游标。您需要将两个列值加载到 2 个变量中:
@BankID
和@BankCategoryID
。即FETCH NEXT FROM MyCursor INTO @BankID, @BankCategoryID
旁白:我想知道这是否可以在没有光标的情况下完成?无论哪种情况,上述内容都应该适合您在游标的每次迭代中实现更多 TSQL 语句。
Perhaps you'd want to use an
IF
as a control statement within your cursor?Here's a sample TSQL cursor. You'd be loading your two column values into 2 variables:
@BankID
and@BankCategoryID
. i.e.FETCH NEXT FROM MyCursor INTO @BankID, @BankCategoryID
Aside: I'm wondering if this could be done all without a cursor? In either case, the above should work for you in implementing more TSQL statements in each iteration of your cursor.