Solved

T-SQL: Stored Procedure - variable assignment

Posted on 2010-11-24
3
451 Views
Last Modified: 2012-05-10
Techies--
Why does @TestCaseStatement in the code snippet fail to set to any value when variables are empty? I am trying to output to a table the values of variables whether they contain strings, integers or are just unset.

Also,  when I get this to work, I want to import this logic into a program that has multiple transactions -- how do I manage
the GOTO routine so as to avoid the write to the error/debug log under ProcessDEBUG: from being treated as part of an existing transaction? Finally, what happens on a return in a GOTO when called while processing a transaction?




DECLARE @dbgFlag bit
DECLARE @TestCaseStatement nvarchar(max)

SET @dbgFlag = 'True'

IF(@valTest = 'GO')
 BEGIN

-- ##DEBUG
   IF (@dbgFlg = 'True')
    BEGIN
     -- // this works because @StrikeTypeName is set to a value like 'Field'
     -- SET @TestCaseStatement = 'UPDATE s1: StrikeTypeName incoming: ' + 
     --   @StrikeTypeName + ' conditioned      lowercase: ' + @StrikeTypeName + 
     -- ' PatClassTypeID: ' + convert(varchar(10),@PatClassTypeID) 
     -- // This did NOT work, because the incoming values are not set to anything.
     -- SET @TestCaseStatement = 'UPDATE s2: ret_myDesignID incoming: ' + 
     -- convert(varchar(10),@ret_myDesignID) + ' BOPatternNum incoming: ' + 
     -- @BOPatternNum + ' BOPatternID: ' + convert(varchar(10),@BOPatternID)
     
     -- // This worked because I forced a value of '103'.
     SET @TestCaseStatement = 'UPDATE s2: ret_myDesignID incoming: ' + convert(varchar(10),103)

     PRINT @TestCaseStatement
     GOTO ProcessDEBUG

    END

 END

ProcessDEBUG:

INSERT INTo SHG_ErrorLog 
 (EntryDate,
  AppName,
  ErrorMsg
 )
VALUES
 (getdate(),
  'DASH',
  @TestCaseStatement
 )

return

Open in new window

0
Comment
Question by:ditallop
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34206302
that is because 'string' + NULL returns NULL ...

so, you need to handle NULL value in your case of building the SQL.

     SET @TestCaseStatement = 'UPDATE s2  SET ret_myDesignID = ' + CASE WHEN @ret_myDesignID IS NULL THEN 'NULL' ELSE CAST(@ret_myDesignID as varchar(10)) END + ' ' 

Open in new window

and in case the field is varchar:
     SET @TestCaseStatement = 'UPDATE s2  SET ret_myDesignID = ' + CASE WHEN @ret_myDesignID IS NULL THEN 'NULL' ELSE '''' + CAST(@ret_myDesignID as varchar(10)) + '''' END + ' ' 

Open in new window

0
 

Author Comment

by:ditallop
ID: 34206454
Thanks! BTW, on the second half of the question -- what happens under a goto with a return when processing a transaction--and is it possible to isolate the insert to the debug log outside of the transaction assumption that the insert to the debug logger is part of a specific transaction?  what i want to do is be able to use this goto from whatever transaction i happen to be on --
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34206503
you have to ensure that your procedure does COMMIT/ROLLBACK need to be balanced.
so, if you have a BEGIN TRANSACTION, and RETURN without a COMMIT/ROLLBACK, you will have the result that if the calling code does not commit on it's turn, the transaction is rolled back.

so, for the debug, you can rollback explicitly (if a transaction has been started, check @@trancount value), and begin + commit a transaction for the debug stuff apart.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get row count of current SQL query 8 59
SQL Backup skipping a few tables 7 51
Table create permissions on SQL Server 2005 9 43
Loops and updating in SQL Query 9 54
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question