[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL 2008 Concat Variable Using Cursor Loop Error?

Posted on 2014-01-22
6
Medium Priority
?
586 Views
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 ------
    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
Comment
Question by:WorknHardr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
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.
0
 
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).
0
 
LVL 35

Accepted Solution

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

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:WorknHardr
ID: 39803577
yea, always null:

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

Assisted Solution

by:Robert Schutt
Robert Schutt earned 1600 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

0
 

Author Closing Comment

by:WorknHardr
ID: 39803811
That worked! thx

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

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

656 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