SQL 2008 Concat Variable Using Cursor Loop Error?

Posted on 2014-01-22
Last Modified: 2014-01-23
I'm trying to build a string like so using the Cursor below: [Jim], [Bob], [Tom]

I receive the following error using Concat and SQL 2008:

   'Concat' is not a recognized built-in function name.

I'm calling the SP like so in a separate query window:
   Declare @Columns      varchar(255)
   Exec sp_MyTableTest 1469, @Columns OUTPUT
   Select @columns

   ---- Return ------

ALTER PROCEDURE [dbo].[SP_MyTableTest]
	@Agency int,
	@Columns	varchar(255) OUT



Declare @Name		varchar(30)

Set @MyCursor = Cursor For Select Code, Name, Price from tbl_Products 
        Open @MyCursor

	Fetch Next From @MyCursor
        Into @Name	

	While @@FETCH_STATUS = 0

                         Set @Columns = Concat( ',[' + @Name + ']' ) // Error
                         Set @Columns +=  ',[' + @Name + ']'  // Always returns Null

                	Fetch Next From @MyCursor
                        Into @Name	

        Close @MyCursor
        Deallocate @MyCursor

Open in new window

Question by:WorknHardr
  • 4
  • 2
LVL 35

Expert Comment

by:Robert Schutt
ID: 39801995
It seems to me that you only have to initialize @Columns to an empty string first as NULL + string = NULL.
LVL 35

Expert Comment

by:Robert Schutt
ID: 39801997
PS: I'm getting an error on the cursor when I test your code but I ignored that as I have a feeling this is not the exact code you're using (Name <=> Column).
LVL 35

Accepted Solution

Robert Schutt earned 400 total points
ID: 39802000
PS2: I didn't test CLR version, only
Set @Columns = @Columns + ',[' + @Name + ']'

Open in new window

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.


Author Comment

ID: 39803577
yea, always null:

   Set @Columns = @Columns + ',[' + @Name + ']'
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 400 total points
ID: 39803589
Ok, so have you added an init?

just add a line below "declare @Name" for example:
Set @Columns = ''

Open in new window


Author Closing Comment

ID: 39803811
That worked! thx

Declare @Name      varchar(30)
Set @Columns = ''

Set @Columns = @Columns + ',[' + @Name + ']'

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tsql query 6 22
Microsoft Access Write errors seem to be caused by bit fields 4 36
SQL Query assistance 16 26
SQL Count issue 24 16
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

777 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