MS SQL Server 2005 中的存储过程

发布于 2024-11-29 03:49:54 字数 400 浏览 0 评论 0原文

这里我有一个场景:当我按下客户端应用程序(用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2024-12-06 03:49:54

也许您想使用 IF 作为游标内的控制语句?

WHILE @@FETCH_STATUS = 0
BEGIN
    IF @BankCategoryID=1  
    BEGIN
       SELECT Baz From Bat;
       DECLARE @Spam bit; 
       SELECT @Spam = 0;
    END

    IF @BankCategoryID=2  
    BEGIN
       SELECT Foo FROM Bar;       
    END
  FETCH NEXT FROM MyCursor INTO @BankID, @BankCategory
END

这是一个示例 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?

WHILE @@FETCH_STATUS = 0
BEGIN
    IF @BankCategoryID=1  
    BEGIN
       SELECT Baz From Bat;
       DECLARE @Spam bit; 
       SELECT @Spam = 0;
    END

    IF @BankCategoryID=2  
    BEGIN
       SELECT Foo FROM Bar;       
    END
  FETCH NEXT FROM MyCursor INTO @BankID, @BankCategory
END

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文