Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a header for SQL declared variables

Status
Not open for further replies.

kdjonesmtb2

Technical User
Nov 19, 2012
93
US
Hello

What is the syntax for creating headers for declared
SQL variables

See code below

I would like to have headers for each of
The print statements below


declare @ICD_DISCREPANCY float(2)



declare @CCA_ICD float(2)

declare @QNXT_ICD float(2)
set @CCA_ICD = (select count(distinct ICD) from ##CCA_Claims AS cca_ICD)



set @QNXT_ICD = (select count(distinct DIAGNOSISCODE) FROM ##QNXT_FTP as QNXT_ICD)


print @CCA_ICD

Print @QNXT_ICD

set @ICD_DISCREPANCY = 1-(@QNXT_ICD/@CCA_ICD)

Print @ICD_DISCREPANCY a
 
When you use the print statement, SQL Server essentially writes a line to the messages window. I only use "print" for debugging purposes, but here is how I would do it.

Code:
declare @ICD_DISCREPANCY float(2)



declare @CCA_ICD float(2)

declare @QNXT_ICD float(2)
set @CCA_ICD = (select count(distinct ICD) from ##CCA_Claims AS cca_ICD)



set @QNXT_ICD = (select count(distinct DIAGNOSISCODE) FROM ##QNXT_FTP as QNXT_ICD)


print 'CCA_ICD = ' + Convert(VarChar(10), @CCA_ICD)

Print 'QNXT_ICD = ' + Convert(VarChar(10), @QNXT_ICD)

set @ICD_DISCREPANCY = 1-(@QNXT_ICD/@CCA_ICD)

Print 'ICD_DISCREPANCY = ' + Convert(varchar(10), @ICD_DISCREPANCY)

If you are running code in a cursor or while loop, you may find it helpful to print a blank line occasionally, which you can accomplish with:

Code:
print ''


-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I should also mention that I've been known to add an optional debug parameter to some of my stored procedures. Something like this....

Code:
Create Procedure dbo.BuggyStoredProcedure
   @Param1 int,
   @Param2 VarChar(20),
   @Debug Bit = 0
AS
SET NOCOUNT ON

If @Debug = 1 Print 'Start of procedure'

-- Do stuff

If @Debug = 1 Print 'Some Value: ' + Convert(VarChar(10), @VariableName)

-- More stuff

If @Debug = 1 Print 'End of procedure'

This way I can leave the debug code in the procedure. Since my app doesn't know about the optional parameter, it would never send in a value. If a value is not sent in, @Debug will be 0 and there will not be any messages. When I am working on the procedure, I can call it from a SSMS window, and set the optional parameter = 1 so that the debug messages will show.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top