473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem concatenating column values into string...

I have a customer who has recently migrated their SQL server to a new
server. In doing so, a portion of a stored procedure has stopped
working. The code snippet is below:

declare @Prefixes varchar(8000),
declare @StationID int
-- ...
select @Prefixes = ''
select @Prefixes = @Prefixes + Prefix + '|||'
from Device
where Station_ID = @StationID

Essentially, we are trying to triple-pipe delimit all the device
prefixes located at a specified station. This code has worked
flawlessly for the last 10 months, but when the database was restored
on the new server, @Prefixes only contains the prefix for the last
device.

Is there a server, database, or connection option that permits this to
work that I am not aware of? Why would this work on the old server and
not on the new? (BTW - both servers are running SQL 2000 Standard
SP4).

Thanks!

Apr 5 '06 #1
6 5383
SQL
The code that you posted looks fine
When you run this

select Prefix
from Device
where Station_ID = @StationID

how many rows does it return?
Is it possible that the restored DB does not have all the data?
Denis the SQL Menace
http://sqlservercode.blogspot.com/

Apr 5 '06 #2
Sorry, I should have specified more details. My first thought was that
after restoring the database, a user had changed the station
configuration (unlikely, but possible). However, the station
configuration is identical on both the new server and that old server.

The station in question, has 2 devices with prefixes 2 and 3.
Therefore, @Prefixes should ressemble '2|||3|||', however I only get
'3|||' on the new server.

When I test your query, it does properly return 2 records with prefixes
2 and 3 (on the new server and on the old server).

Thanks!

Apr 5 '06 #3
da**************@gmail.com (da**************@gmail.com) writes:
I have a customer who has recently migrated their SQL server to a new
server. In doing so, a portion of a stored procedure has stopped
working. The code snippet is below:

declare @Prefixes varchar(8000),
declare @StationID int
-- ...
select @Prefixes = ''
select @Prefixes = @Prefixes + Prefix + '|||'
from Device
where Station_ID = @StationID

Essentially, we are trying to triple-pipe delimit all the device
prefixes located at a specified station. This code has worked
flawlessly for the last 10 months, but when the database was restored
on the new server, @Prefixes only contains the prefix for the last
device.

Is there a server, database, or connection option that permits this to
work that I am not aware of? Why would this work on the old server and
not on the new? (BTW - both servers are running SQL 2000 Standard
SP4).


Because the result of this operation is undefined. Rewrite the code to run
a cursor instead. On SQL 2005 there is syntax that permits you do this in
one syntax, but it's not the one above. (It's a quite obscure solution
that uses XPath.)

Refer also to http://support.microsoft.com/default.aspx?scid=287515.
Pay particular attention to the the first sentence under CAUSE.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 5 '06 #5
As a short-term fix, I have replaced this query with a cursor.

The reference to the sqljunkies blog entries doesn't help. There is no
difference between what I'm accomplishing with the current query,
versus moving that same query into a scalar function.

Erland: I'm confused regarding the article that you posted. I am not
calling any functions in the select list, or the order by clause. I
don't understand "The correct behavior for an aggregate concatenation
query is undefined." I am successfully using similar queries in
multiple solutions, this is the first time that I have experienced this
problem. Again, I've only moved the database to a new server and then
this problem began.

Thanks All!
David

Apr 6 '06 #6
da**************@gmail.com (da**************@gmail.com) writes:
Erland: I'm confused regarding the article that you posted. I am not
calling any functions in the select list, or the order by clause. I
don't understand "The correct behavior for an aggregate concatenation
query is undefined."
An aggregate concatenation is when you try:

SELECT @x = @x + col FROM tbl

that is, precisly what you had in your code.

And the result of this operation is undefined. That is, there is no
guarantee that you get the result you expect.

The article itself is admittedly strange, because if first says
that this type of operation is undefined, and then it tries to explain
how you should use it anyway.
I am successfully using similar queries in multiple solutions,


Yes, the method is deceivable, as it often gives the desired result,
despite that there is no guarantee for it.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.seBooks Online for SQL
Server 2005
athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000
athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Apr 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dont bother | last post by:
Hey, I have these attributes: index which is a numerical value value vector which is a numerical float value and I want to concatenate like this:
6
by: james | last post by:
CAn Some one please help when this scrip runs it does not enter the data from my forms, it just says where each feild should be. I have tested it using google address and it appears to work just...
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I canīt see...
0
by: bob brar | last post by:
hi, I have the following function in a class. Public Function getCustomers() As DataSet Dim Db As New CDatabaseAccessor getCustomers = Db.getData("SELECT A.*, RTRIM( A.TITLE)+', '+ RTRIM...
4
by: lindiwemaduna | last post by:
I want to concatenate values of two text boxes into one string but these should be separated by a space in the database table. i have tried all the following but twas not successful: Dim fullName...
8
by: tony | last post by:
Hello! I have below a for loop and a switch in the for loop. I have also a enum called colBlowStep with some values. I have also an array called m_columnBlowStep with some strings. All items in...
7
by: Mary | last post by:
I have a student who has a hyphenated first name. If I concatenate the name like this: StudentName:( & ", " & ), it works as expected. If, however, I try to get the first name first by...
29
by: ApeX | last post by:
Hi guys, i hace a question i have a datagrid col1 col2 ----------------- D text0 text1 text2 D text3
6
by: Mark | last post by:
Hi, i have an application which works with date. The regional settings of the computer (XP prof. dutch version) are set to French (Belgium). Asp.net and Sql server take the short date format of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.