How to print each record in a result set?

  • Hi all,

    I would like to print each record in the result set, is using CURSOR a proper approach?

    Exmaple, I want a list of Customer ID and Name

    -- * Print Customer ID and Name --

    USE Northwind

    DECLARE @CustID NCHAR(5)

    DECLARE @CustName NVARCHAR(40)

    DECLARE myCursor CURSOR FOR

    SELECT CustomerID, CompanyName FROM Customers

    OPEN myCursor

    FETCH NEXT FROM myCursor INTO @CustID, @CustName

    WHILE @@FETCH_STATUS = 0

    BEGIN

    PRINT @CustID + ' - ' + @CustName

    FETCH NEXT FROM myCursor INTO @CustID, @CustName;

    END

    CLOSE myCursor

    DEALLOCATE myCursor

    Kindly advise, thanks.

  • What's wrong with just doing this:

    SELECT CustomerID, CompanyName FROM Customers

  • I wanted the result in text format, and with some additional text also.

  • Thanks, just found that by setting Results to Text will do. 😀

  • you can cast or convert the data in text format after adding additional text.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply