tags:

views:

83

answers:

4

I wrote some T-SQL which will concatenate all my strings in one column together but nvarchar(max) is not enough to store it.

Is there another way?

The code I'm trying to use is:

DECLARE @codes NVARCHAR(max) 
SET @codes = '' 
SELECT @codes = @codes + ',' + CONVERT(nvarchar,code) 
FROM dbo.listing SELECT @codes
+3  A: 

Unlimited, no. At some point you're going to run out of storage space. Seriously, if you find yourself creating individual elements that wont fit within about 231 bytes, you're doing something wrong.

Why don't you tell us the "real" problem that you're trying to solve? Without the preconceived notions that you must do it in a certain way.

paxdiablo
let see my problem, my database has about 100000 rows, and has one column "Code" which is int datatype. Now my problem is : I want to concatenate all the Code together but the returning is more than nvarchar(max). How you solve this?
pang
Why do you want to do this? You're still not stating the root problem, just the problem you have doing it a certain way. In any case, 100,000 integers at ~12 characters each is only 1.2 million characters (2.4 million bytes), isn't it. Your varchar(max) should be able to handle 2.4 billion bytes. Maybe your figures aren't indicative but the best response you can give is why this is necessary. If it's to be used outside of SQLSvr, you can write them to a file (one example). What is that data to be used for?
paxdiablo
thanks for quick response Pax, this is just a once time generation , so I want to do it in sql statement. let see my code DECLARE @codes NVARCHAR(max)SET @codes = ''SELECT @codes = @codes + ',' + CONVERT(nvarchar,code) FROM dbo.listingSELECT @codesit doesn't return all the characters, some are truncated automatically.
pang
A: 

The only suggestion that I can think off the top of my head would be to use varchar(max) if you aren't worried about international characters. But that isn't going to give you a massive increase in storage capacity.

That must be a really massive SQL string though for it not to fit.

A: 

You need to cast the code column to nvarchar(max) or else it will truncate:

DECLARE @codes NVARCHAR(max) 
SET @codes = '' 
SELECT @codes = @codes + ',' + CONVERT(nvarchar**(max)**,code) 
FROM dbo.listing SELECT @codes
Adam Ruth
A: 

What are you planning to do with this awful variable? Why not just write a join to the table instead?

HLGEM