473,473 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

variable always empty in stored procedure

In the code below, the statement 'Print @Sites' prints nothing, even
though the cursor contains 4 records, and 'Print @Site' prints the
appropriate values. Can anyone see my mistake? I am attempting to
print a delimited string of the values referred to by @Sites.

Thanks.
Dan Fisherman


DECLARE SiteCursor CURSOR
GLOBAL
SCROLL
STATIC
FOR SELECT OfficeName FROM ClientOffices WHERE ClientID=12 ORDER BY
OfficeName

OPEN SiteCursor

DECLARE @Sites varchar(1000)
DECLARE @Site varchar(100)

FETCH NEXT FROM SiteCursor INTO @Site

WHILE @@FETCH_STATUS=0
BEGIN
print @Site
SET @Sites = @Sites + ', ' + @Site
FETCH NEXT FROM SiteCursor INTO @Site
END

PRINT @Sites
CLOSE SiteCursor
DEALLOCATE SiteCursor
GO
Jul 20 '05 #1
4 6887
Daniel,

The initial value of @Sites is null, so @Sites + ', ' + @Site is always
null.
Set it to blank before your loop.

Set @Sites = ''

Shervin

"Daniel Fisherman" <df********@optonline.net> wrote in message
news:a9*************************@posting.google.co m...
In the code below, the statement 'Print @Sites' prints nothing, even
though the cursor contains 4 records, and 'Print @Site' prints the
appropriate values. Can anyone see my mistake? I am attempting to
print a delimited string of the values referred to by @Sites.

Thanks.
Dan Fisherman


DECLARE SiteCursor CURSOR
GLOBAL
SCROLL
STATIC
FOR SELECT OfficeName FROM ClientOffices WHERE ClientID=12 ORDER BY
OfficeName

OPEN SiteCursor

DECLARE @Sites varchar(1000)
DECLARE @Site varchar(100)

FETCH NEXT FROM SiteCursor INTO @Site

WHILE @@FETCH_STATUS=0
BEGIN
print @Site
SET @Sites = @Sites + ', ' + @Site
FETCH NEXT FROM SiteCursor INTO @Site
END

PRINT @Sites
CLOSE SiteCursor
DEALLOCATE SiteCursor
GO

Jul 20 '05 #2
The following code still prints an empty line when executing 'Print
@Sites'. Any ideas?
OPEN SCur
DECLARE @Sites char(1000)
DECLARE @Site varchar(100)

FETCH NEXT FROM SCur INTO @Site
Set @Sites = ''

WHILE @@FETCH_STATUS=0
BEGIN
PRINT @Site
SET @Sites = @Sites + @Site

FETCH NEXT FROM SCur INTO @Site
END

PRINT @Sites
close SCur
Jul 20 '05 #3
Daniel,

It's because you have changed the type of @Sites from VARCHAR to CHAR. When
you use CHAR your variable has trailing spaces. Even when you assign a blank
string to @Sites, it's a string of 1000 spaces. So when you try to
concatinate another string to it, the result is 1000 spaces plus @Site and
when you store it again in @Sites, SQL Server automatically trims it to fit
it into @Sites.
So, you have two options:

1. Use VARCHAR instead of CHAR.
OR
2. Trim trailing spaces before adding new site.
SET @Sites = RTrim(@Sites) + @Site

Shervin

"Daniel Fisherman" <df********@optonline.net> wrote in message
news:a9**************************@posting.google.c om...
The following code still prints an empty line when executing 'Print
@Sites'. Any ideas?
OPEN SCur
DECLARE @Sites char(1000)
DECLARE @Site varchar(100)

FETCH NEXT FROM SCur INTO @Site
Set @Sites = ''

WHILE @@FETCH_STATUS=0
BEGIN
PRINT @Site
SET @Sites = @Sites + @Site

FETCH NEXT FROM SCur INTO @Site
END

PRINT @Sites
close SCur

Jul 20 '05 #4
thanks. that worked
Jul 20 '05 #5

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

Similar topics

4
by: Greg | last post by:
I need to send the result of a procedure to an update statement. Basically updating the column of one table with the result of a query in a stored procedure. It only returns one value, if it didnt...
2
by: Chip Mayan | last post by:
Hello, I am fairly new at stored procedures. I have created some that will go through a table and return a start date and an end date that is dependent upon the fiscal period you want, but I...
8
by: Peter | last post by:
Hi, there I have created an stored procedure using the DDL below for my MS Access Database and no error occurs. Also it can create an stored procedure if I changed the parameter from "" to...
3
by: Pieter | last post by:
Hi, I have a View which contains (of course) several columns. Now I want to make a Stored Procedure that does a Select based on the parameters. For exemple: One time I want to select all the...
2
by: timpera2501 | last post by:
I am a newb to OOP programming and I'm only just starting with C#. I've done a TON of reading lately, and I'm currently in the process of modifying some of the function provided by the...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
6
by: pretzla | last post by:
I have a PL/SQL script where I load data from a stored procedure into bind variables. Then, I insert that data from the bind variables into an Oracle table with a simple insert statement. The...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.