Solved

SQL Concat Variable Cast at Runtime Returns NULL?

Posted on 2014-01-23
3
320 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
ID: 39804764
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
ID: 39804769
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
ID: 39804843
Excellent`a... thx
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

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