473,406 Members | 2,336 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,406 software developers and data experts.

assigning each record to one string

hello,

i would like to loop through a record set and assign each value to the
same string, (example i would like to return all of the first name in
the authors table = Authors_total.)

should i use a cursor or just a loop to do this? I have had some
trouble with the syntax in a cursor.

nicholas.gadacz

Jul 23 '05 #1
4 1478
Dear Nicholas,

I hope following will be help full for you.
------------------------------------------------
Declare @varstr as varchar(4000) -- declreation of
set @varstr = ''; --initializing you know the fact Null + somhting =
Null
select @varstr = @varstr+','+isnull(ProductName,',') from Product;
Select @varstr;
------------------------------------------------

Best of Luck :) :) :)

Saghir Taj
MCDBA
www.dbnest.com: Home of DB Professionals.

ng*****@ftresearch.com wrote:
hello,

i would like to loop through a record set and assign each value to the same string, (example i would like to return all of the first name in
the authors table = Authors_total.)

should i use a cursor or just a loop to do this? I have had some
trouble with the syntax in a cursor.

nicholas.gadacz


Jul 23 '05 #2
Why not do that client-side? SQL isn't the best place for this kind of
presentational functionality.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3
I am still not sure how i would loop through all of the records. if a
use a cursor i get an error variable assignment is not allowed in a
cursor declaration.

The reason why I don't put this functionality is the client side is
that I have multiple client sides: asp php and soon .aspx (.net) with
changes I want to have the code centralized.

nicholas.gadacz

Jul 23 '05 #4
(ng*****@ftresearch.com) writes:
I am still not sure how i would loop through all of the records. if a
use a cursor i get an error variable assignment is not allowed in a
cursor declaration.
DELARE @str varchar(8000), @col varchar(30)

DECLARE cur INSENSTIVE CURSOR FOR
SELECT col FROM tbl ORDER BY col
OPEN cur
WHILE 1 = 1
BEGIN
FETCH cur INTO @col
IF @@fetch_status <> 0
BREAK

SELECT @str = CASE WHEN @str IS NULL
THEN @col
ELSE @str + ',' + @col
EMD
END
DEALLOCATE cur

This is one of the few things where you must use a cursor. Another poster
showed an example with a SELECT statement. However, that is not guaranteed
to work.
The reason why I don't put this functionality is the client side is
that I have multiple client sides: asp php and soon .aspx (.net) with
changes I want to have the code centralized.


Beware that the above solution has a hard limit of the output string of
8000 characters.

In SQL2005 there will actually be a way to do this in a single statement,
by some fairly funny usage of the new XML stuff.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5

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

Similar topics

5
by: gtz669 | last post by:
I have a class which I am using for data stroage. I declare an instance of that class in my main class which is running my java applet. I Iassign it a value in the init () function and it works...
1
by: Tony | last post by:
Thanks for writing back and don't laugh at what I am about to post I am new to this type of stuff....this is what I have tried and it isn't working(please note in the try section I was just trying...
2
by: Michael Easterly | last post by:
What is a good way to read a text file and read each line, then assign data to variables? I have this so far, #include <iostream> #include <fstream> using namespace std; string...
2
by: JC | last post by:
Hi, I have a database that imports 4 reports to my "data" table, each report has its own identifier. Then I have a table with Analysts with the report identifier that they are to be assigned to. ...
8
by: Woody Splawn | last post by:
Lets say I have a winform that is populated with a dataset. The dataset and data table may have several rows in it. Lets say I am looking at the winform and I want to assign a value to a certain...
2
by: McGeeky | last post by:
Hi. I want to assign an XML string to an XML attribute. This XML string must undergo "escape" conversion so that the < and & symbols are converted in to escaped equivalents. Does the .Net...
5
by: Phil Latio | last post by:
I have 2 virtually identical tables and wish to move data between them. Basically one table is called "live_table" and the other is named "suspended_table" and the only difference is that the...
10
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m...
3
by: liquid | last post by:
Microsoft Access I am a newbee, so this may be a stupid question, but I will ask anyway. I have a form "Month1" and a linked subform "Monthlystuff" linked by a textfield "Month" and I placed in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.