All, this is something I have not had to do before and I cannot locate a simalar question for some reason. I wish to return the NVARCHAR @columnHeaders to a String in C#. The SQL query that will build the string I want is this
DECLARE @columnHeaders NVARCHAR(MAX);
SELECT @columnHeaders =
COALESCE (@columnHeaders + ',[' + Field + ']', '[' + Field + ']')
FROM SomeTable;
However, how do I then pull the variable @columnHeaders back into C#? Note, I could use a function etc., but I do not want to use Stored Procedures, Views or Functions unless absolutely neccessary…
I can select the the columnn headers in to a DataTable using
DECLARE @columnHeaders NVARCHAR(MAX);
SELECT COALESCE (@columnHeaders + ',[' + Field + ']', '[' + Field + ']')
FROM SomeTable;
and manipulate in C#, but it would be nice to return the string direct from SQL.
Thanks for your time.
If you don’t want to use stored procedures, this will probably work.
and use
ExecuteScalar()to get the results.However, I would personally recommend a stored procedure as being far neater, in that it keeps the SQL in the SQL Server.