2
votes

I'm working with a SQL stored procedure and dynamic SQL. When I try to debug everything works fine, up until when I'm printing my @columnName.

this is the part of the code that's causing trouble:

DECLARE @query NVARCHAR(2000)
DECLARE @columnName NVARCHAR(250)
SELECT @query = 'SELECT myTable.value FROM myTable WHERE myConditions'
    EXECUTE sp_executesql @query, N'@columnName NVARCHAR(30) OUTPUT', @columnName OUTPUT
    PRINT N'query='+@query
    set @query = ''
    PRINT N'query after reset='+@query
    PRINT N'columnName='+@columnName
    PRINT N'any other message'
Select @query = 'SELECT myValues AS '''+@columnName+''',
                 FROM myTable2
                 WHERE myConditions';
EXEC(@query);

This is the output messages:

query=SELECT myTable.value FROM myTable WHERE myConditions
query after reset=

any other message

So, where is my columnName= output?

EDIT

SQL Version: Microsoft SQL Server 2012 - 11.0.2100.60 (X64) Feb 10 2012 19:39:15 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor)

2
SQL is just the Structured Query Language - a language used by many database systems, but not a a database product... many things are vendor-specific - so we really need to know what database system (and which version) you're using.... - marc_s
Added the version under "EDIT" - Johan Hjalmarsson
Added the appropriate tag sql-server-2012 - that's where you should specify these kinds of things.... - marc_s

2 Answers

8
votes

Add SET CONCAT_NULL_YIELDS_NULL OFF to the beginning of your process to allow the rest of the statement to print if there is a NULL value passed.

2
votes

Solved it.

Turns out that if I add something with null, the result will be null. So if I add 'string to be printed'+NULL then nothing will be printed. Now I just have to figure out why the heck my variable doesn't have a value ^^ Wish me luck!