Solved

SQL Concat Variable Cast at Runtime Returns NULL?

Posted on 2014-01-23
3
318 Views
Last Modified: 2014-01-23
I'm trying to build a string of converted decimals into variable @Prices and keep getting null returns when I call this code in a separate query window:

   Declare @Prices      varchar(max) = NULL
   exec sp_MyTableTest @Prices OUTPUT
   select @Prices

   ---- Return ------
    NULL

ALTER PROCEDURE [dbo].[SP_MyTableTest]
(
	@Prices	 varchar(max) OUT
)

AS

BEGIN

Declare @Price		varchar(10)
Set @Prices = ''

Set @MyCursor = Cursor For Select Code, Name, Cast(Price as varchar(10) ) 
                                                  from tbl_Products 
        Open @MyCursor

	Fetch Next From @MyCursor
        Into @Price	

	While @@FETCH_STATUS = 0

		Begin
 
                        Set @Prices = @Prices + '''' + @Price + ''',' // Always returns Null

                        Set @Prices = ''''+ @Price + ''',' //WORKS!

                	Fetch Next From @MyCursor
                        Into @Name	
                End	

        Close @MyCursor
        Deallocate @MyCursor

Open in new window

0
Comment
Question by:WorknHardr
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
Comment Utility
this should work better:
ALTER PROCEDURE [dbo].[SP_MyTableTest]
(
	@Prices	 varchar(max) OUT
)

AS

BEGIN

Declare @Price		varchar(10)
Set @Prices = ''

Set @MyCursor = Cursor For 
Select Cast(Price as varchar(10) ) as price 
 from tbl_Products 
  where price is not null

        Open @MyCursor

	Fetch Next From @MyCursor
        Into @Price	

	While @@FETCH_STATUS = 0

		Begin
 
                        Set @Prices = isnull(@Prices,'') + '"' + @Price + '",'

                	Fetch Next From @MyCursor
                        Into @price	
                End	

        Close @MyCursor
        Deallocate @MyCursor
                                  

Open in new window

0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
Comment Utility
you could shorten the code also, and avoid the cursor altogether:
ALTER PROCEDURE [dbo].[SP_MyTableTest]
(
	@Prices	 varchar(max) OUT
)

AS

BEGIN

Select @Prices = isnull(@prices, '') + '"' + Cast(Price as varchar(10) ) + '",'  
 from tbl_Products 
  where price is not null

END

Open in new window

0
 

Author Closing Comment

by:WorknHardr
Comment Utility
Excellent`a... thx
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now