• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

SQL 2008 Concat Variable Using Cursor Loop Error?

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 ------
    NULL

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

AS

BEGIN

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

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

                	Fetch Next From @MyCursor
                        Into @Name	
                End	

        Close @MyCursor
        Deallocate @MyCursor

Open in new window

0
WorknHardr
Asked:
WorknHardr
  • 4
  • 2
2 Solutions
 
Robert SchuttSoftware EngineerCommented:
It seems to me that you only have to initialize @Columns to an empty string first as NULL + string = NULL.
0
 
Robert SchuttSoftware EngineerCommented:
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).
0
 
Robert SchuttSoftware EngineerCommented:
PS2: I didn't test CLR version, only
Set @Columns = @Columns + ',[' + @Name + ']'

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
WorknHardrAuthor Commented:
yea, always null:

   Set @Columns = @Columns + ',[' + @Name + ']'
0
 
Robert SchuttSoftware EngineerCommented:
Ok, so have you added an init?

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

Open in new window

0
 
WorknHardrAuthor Commented:
That worked! thx

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

Set @Columns = @Columns + ',[' + @Name + ']'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now