Print vs. Select

  • Hello all,

    to do some sort of debugging in Sql Server Management studio i can use either

    1) Select "Critical point passed"

    or

    2) print "Critical point passed"

    Are there any advantages/disadvantages for those 2 options?

    Thanks.

  • Probably PRINT introduces less overhead.

    If inside a loop, consider that SSMS can handle a limited number of resultsets.

    I don't know if this is what you were asking, I hope this helps.

    -- Gianluca Sartori

  • In Select statement you can specify column heading, whereas in print statment you cannot.

    Select 'Critical point passed' 'column1'

    print 'Critical point passed'

    Thanks & Regards,
    Krish.
    (Together We WIN)
  • This was removed by the editor as SPAM

  • For checking the flow of T-SQL code in SSMS, PRINT statement is fine. For debugging, another option is RAISERROR. You can check the details @ http://www.sqlservercentral.com/articles/SQL+Puzzles/quickhintsforusingtheraiserrorcommand/2114/

    And also @ http://msdn.microsoft.com/en-us/library/ms178592.aspx

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

  • Atif Sheikh (5/27/2010)


    For checking the flow of T-SQL code in SSMS, PRINT statement is fine. For debugging, another option is RAISERROR. You can check the details @ http://www.sqlservercentral.com/articles/SQL+Puzzles/quickhintsforusingtheraiserrorcommand/2114/

    That is a pretty good intro article to RAISERROR. I often use variations described in there but sometime they limit what I can do. Rarely, when I really need to go to this extent, I use PRINT statements like the following which allows you to do a great deal if you care to. Just a bit of a pain to set up and cast everything and change settings. But here is a simple example to show how one might do this and account for NULLs in the variables:

    DECLARE @CONCAT_NULL_YIELDS_NULL bit;

    DECLARE @SomeID int;

    DECLARE @SomeProcName varchar(128);

    DECLARE @SomeOtherID int;

    DECLARE @otherDesc varchar(128);

    DECLARE @ReturnStatus int;

    SET @SomeID = 123456789;

    SET @SomeProcName = 'SomeProcedure';

    SET @SomeOtherID = 987654321;

    SET @otherDesc = 'A description here';

    SET @ReturnStatus = NULL;

    --get the setting of CONCAT_NULL_YIELDS_NULL so you can reset it

    SET @CONCAT_NULL_YIELDS_NULL = CAST(SESSIONPROPERTY('CONCAT_NULL_YIELDS_NULL') as bit); --returns sql_variant

    SET CONCAT_NULL_YIELDS_NULL OFF;

    PRINT '@SomeID: ' + CAST(@SomeID as varchar(100))

    + ' @SomeProcName: ' + @SomeProcName

    + ' @SomeOtherID: ' + CAST(@SomeOtherID as varchar(100)) + ' desc= ' + @otherDesc

    + ' @ReturnStatus: ' + CAST(@ReturnStatus as varchar(100));

    --put the setting back the way it was

    IF @CONCAT_NULL_YIELDS_NULL = 1 BEGIN

    SET CONCAT_NULL_YIELDS_NULL ON;

    END

    ELSE BEGIN

    SET CONCAT_NULL_YIELDS_NULL OFF;

    END;

    If you have reached the print limit size you might check out this question posed:

    http://ask.sqlservercentral.com/questions/1968/any-way-around-the-print-limit-of-nvarcharmax-in-ssms

Viewing 6 posts - 1 through 6 (of 6 total)

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