• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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