Stored procedure in MS SQL Server 2005
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_re开发者_如何学运维cord.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?
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.
精彩评论