[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

What is wrong with this String operation

manganb

3/19/2007 5:26:00 AM

I am having a problem with returning a string of values from the
table, this will be part of a scalar function but here is where the
code is jamming up.


Code:
DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
int
set @omBranchID = 11601
declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
FROM dbo.omBranchStatus INNER JOIN
dbo.omStatus ON
dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
omBranchStatus.omBranchStatusComplete = 0
Order By omStatus.omStatusRank

OPEN MyCursor
FETCH MyCursor INTO @work
WHILE @@FETCH_STATUS <> -1
BEGIN


set @string = @string + @work

FETCH MyCursor INTO @work
END
DEALLOCATE MyCursor

PRINT @string


What is the solution to this and what is the problem called (so I can
learn about it)

9 Answers

xyb

3/19/2007 5:32:00 AM

0

On 3?19?, ??1?25?, mang...@gmail.com wrote:
> I am having a problem with returning a string of values from the
> table, this will be part of a scalar function but here is where the
> code is jamming up.
>
> Code:
> DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> int
> set @omBranchID = 11601
> declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> FROM dbo.omBranchStatus INNER JOIN
> dbo.omStatus ON
> dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> omBranchStatus.omBranchStatusComplete = 0
> Order By omStatus.omStatusRank
>
> OPEN MyCursor
> FETCH MyCursor INTO @work
> WHILE @@FETCH_STATUS <> -1
> BEGIN
>
> set @string = @string + @work
>
> FETCH MyCursor INTO @work
> END
> DEALLOCATE MyCursor
>
> PRINT @string
>
> What is the solution to this and what is the problem called (so I can
> learn about it)

em... your problem are clearly described.
set @string = @string + @work
this line i can guess @string should to be declare as varchar(8000),or
the date lenght will not enough.

manganb

3/19/2007 5:36:00 AM

0

Alas,

That does not fix it either. I am not getting a message or anything,
just blankness.

I even tried getting sneaky and passes just the column ID number and
did a nested select statement (incredibly inefficient)

On Mar 18, 10:31 pm, "xyb" <xiangyua...@gmail.com> wrote:
> On 3?19?, ??1?25?, mang....@gmail.com wrote:
>
>
>
> > I am having a problem with returning a string of values from the
> > table, this will be part of a scalar function but here is where the
> > code is jamming up.
>
> > Code:
> > DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> > int
> > set @omBranchID = 11601
> > declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> > FROM dbo.omBranchStatus INNER JOIN
> > dbo.omStatus ON
> > dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> > WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> > omBranchStatus.omBranchStatusComplete = 0
> > Order By omStatus.omStatusRank
>
> > OPEN MyCursor
> > FETCH MyCursor INTO @work
> > WHILE @@FETCH_STATUS <> -1
> > BEGIN
>
> > set @string = @string + @work
>
> > FETCH MyCursor INTO @work
> > END
> > DEALLOCATE MyCursor
>
> > PRINT @string
>
> > What is the solution to this and what is the problem called (so I can
> > learn about it)
>
> em... your problem are clearly described.
> set @string = @string + @work
> this line i can guess @string should to be declare as varchar(8000),or
> the date lenght will not enough.


xyb

3/19/2007 5:44:00 AM

0

On 3?19?, ??1?25?, mang...@gmail.com wrote:
> I am having a problem with returning a string of values from the
> table, this will be part of a scalar function but here is where the
> code is jamming up.
>
> Code:
> DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> int
> set @omBranchID = 11601
> declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> FROM dbo.omBranchStatus INNER JOIN
> dbo.omStatus ON
> dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> omBranchStatus.omBranchStatusComplete = 0
> Order By omStatus.omStatusRank
>
> OPEN MyCursor
> FETCH MyCursor INTO @work
> WHILE @@FETCH_STATUS <> -1
> BEGIN
>
> set @string = @string + @work
>
> FETCH MyCursor INTO @work
> END
> DEALLOCATE MyCursor
>
> PRINT @string
>
> What is the solution to this and what is the problem called (so I can
> learn about it)

e :) i and yourself have not read code carefully.
this line:
while @@fetch_status <> -1 --error here
begin
....
end

0 good
-1error or record not in the set
-2 record not in the set
look at BOL for more info.

manganb

3/19/2007 6:01:00 AM

0


Hi,

When I set WHILE @@FETCH_STATUS = 0, I still have the same result.

My understanding of <> -1 is that it would return a blank or a result
(and there are results in this table set, when I nest a PRINT @work
within the Cursor loop it returns the correct data, as a single line,
but there is something magical about conticating it with iself
(@string) that makes it flip out..




On Mar 18, 10:43 pm, "xyb" <xiangyua...@gmail.com> wrote:
> On 3?19?, ??1?25?, mang....@gmail.com wrote:
>
>
>
> > I am having a problem with returning a string of values from the
> > table, this will be part of a scalar function but here is where the
> > code is jamming up.
>
> > Code:
> > DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> > int
> > set @omBranchID = 11601
> > declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> > FROM dbo.omBranchStatus INNER JOIN
> > dbo.omStatus ON
> > dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> > WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> > omBranchStatus.omBranchStatusComplete = 0
> > Order By omStatus.omStatusRank
>
> > OPEN MyCursor
> > FETCH MyCursor INTO @work
> > WHILE @@FETCH_STATUS <> -1
> > BEGIN
>
> > set @string = @string + @work
>
> > FETCH MyCursor INTO @work
> > END
> > DEALLOCATE MyCursor
>
> > PRINT @string
>
> > What is the solution to this and what is the problem called (so I can
> > learn about it)
>
> e :) i and yourself have not read code carefully.
> this line:
> while @@fetch_status <> -1 --error here
> begin
> ...
> end
>
> 0 good
> -1error or record not in the set
> -2 record not in the set
> look at BOL for more info.



xyb

3/19/2007 6:10:00 AM

0

On 3?19?, ??2?00?, "mang...@gmail.com" <mang....@gmail.com> wrote:
> Hi,
>
> When I set WHILE @@FETCH_STATUS = 0, I still have the same result.
>
> My understanding of <> -1 is that it would return a blank or a result
> (and there are results in this table set, when I nest a PRINT @work
> within the Cursor loop it returns the correct data, as a single line,
> but there is something magical about conticating it with iself
> (@string) that makes it flip out..
>
> On Mar 18, 10:43 pm, "xyb" <xiangyua...@gmail.com> wrote:
>
>
>
> > On 3?19?, ??1?25?, mang...@gmail.com wrote:
>
> > > I am having a problem with returning a string of values from the
> > > table, this will be part of a scalar function but here is where the
> > > code is jamming up.
>
> > > Code:
> > > DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> > > int
> > > set @omBranchID = 11601
> > > declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> > > FROM dbo.omBranchStatus INNER JOIN
> > > dbo.omStatus ON
> > > dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> > > WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> > > omBranchStatus.omBranchStatusComplete = 0
> > > Order By omStatus.omStatusRank
>
> > > OPEN MyCursor
> > > FETCH MyCursor INTO @work
> > > WHILE @@FETCH_STATUS <> -1
> > > BEGIN
>
> > > set @string = @string + @work
>
> > > FETCH MyCursor INTO @work
> > > END
> > > DEALLOCATE MyCursor
>
> > > PRINT @string
>
> > > What is the solution to this and what is the problem called (so I can
> > > learn about it)
>
> > e :) i and yourself have not read code carefully.
> > this line:
> > while @@fetch_status <> -1 --error here
> > begin
> > ...
> > end
>
> > 0 good
> > -1error or record not in the set
> > -2 record not in the set
> > look at BOL for more info.- ??????? -
>
> - ??????? -

my test code below:

create function test(@vinput varchar(10))
returns varchar(1000)
as
begin
declare @string varchar(200)
declare @tempstr varchar(200)
select @string = ''
select @tempstr = ''
declare myCur cursor
for
select [id] from sysobjects where [id] < 100

open myCur
fetch myCur into @tempstr
while @@fetch_status = 0
begin
select @string = @string + @tempstr + ','
fetch myCur into @tempstr
end
deallocate myCur
return @string
end
go

select dbo.test('test')

drop function dbo.test

Uri Dimant

3/19/2007 6:36:00 AM

0

Hi
Make sure that @work variable was not set to NULL.

<manganb@gmail.com> wrote in message
news:1174281945.121735.98210@n76g2000hsh.googlegroups.com...
>I am having a problem with returning a string of values from the
> table, this will be part of a scalar function but here is where the
> code is jamming up.
>
>
> Code:
> DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> int
> set @omBranchID = 11601
> declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> FROM dbo.omBranchStatus INNER JOIN
> dbo.omStatus ON
> dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> omBranchStatus.omBranchStatusComplete = 0
> Order By omStatus.omStatusRank
>
> OPEN MyCursor
> FETCH MyCursor INTO @work
> WHILE @@FETCH_STATUS <> -1
> BEGIN
>
>
> set @string = @string + @work
>
> FETCH MyCursor INTO @work
> END
> DEALLOCATE MyCursor
>
> PRINT @string
>
>
> What is the solution to this and what is the problem called (so I can
> learn about it)
>


Jack Vamvas

3/19/2007 8:47:00 AM

0

How exactly is it jamming up?
Try putting SET @string = '' , prior to the DECLARE CURSOR starting ,
and reply with what result you get?



Jack Vamvas
___________________________________
Advertise your IT vacancies for free at - http://www.ITj...



<manganb@gmail.com> wrote in message
news:1174281945.121735.98210@n76g2000hsh.googlegroups.com...
>I am having a problem with returning a string of values from the
> table, this will be part of a scalar function but here is where the
> code is jamming up.
>
>
> Code:
> DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> int
> set @omBranchID = 11601
> declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> FROM dbo.omBranchStatus INNER JOIN
> dbo.omStatus ON
> dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> omBranchStatus.omBranchStatusComplete = 0
> Order By omStatus.omStatusRank
>
> OPEN MyCursor
> FETCH MyCursor INTO @work
> WHILE @@FETCH_STATUS <> -1
> BEGIN
>
>
> set @string = @string + @work
>
> FETCH MyCursor INTO @work
> END
> DEALLOCATE MyCursor
>
> PRINT @string
>
>
> What is the solution to this and what is the problem called (so I can
> learn about it)
>


manganb

3/19/2007 4:19:00 PM

0

Thank you Jack and Uri,

Both your advice fixed it.

Why is it that I had to set the string to ''? and there was a null
result in the set, and so ISNULL took care of that wee issue.

-Brian

Below is the working code:
DECLARE @string varchar(8000), @work varchar(10), @omBranchID int
set @omBranchID = 17674
declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
FROM dbo.omBranchStatus INNER JOIN
dbo.omStatus ON
dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
omBranchStatus.omBranchStatusComplete = 0
Order By omStatus.omStatusRank


set @string = ''
OPEN MyCursor
FETCH MyCursor INTO @work
WHILE @@FETCH_STATUS = 0
BEGIN

set @string = @string + ' ' + ISNULL(@work,'')

FETCH MyCursor INTO @work
END
DEALLOCATE MyCursor

PRINT @string



On Mar 19, 1:46 am, "Jack Vamvas" <DEL_TO_RE...@del.com> wrote:
> How exactly is it jamming up?
> Try putting SET @string = '' , prior to the DECLARE CURSOR starting ,
> and reply with what result you get?
>
> Jack Vamvas
> ___________________________________
> Advertise your IT vacancies for free at -http://www.ITj...
>
> <mang...@gmail.com> wrote in message
>
> news:1174281945.121735.98210@n76g2000hsh.googlegroups.com...
>
> >I am having a problem with returning a string of values from the
> > table, this will be part of a scalar function but here is where the
> > code is jamming up.
>
> > Code:
> > DECLARE @string as nvarchar(200), @work nvarchar(200), @omBranchID
> > int
> > set @omBranchID = 11601
> > declare MyCursor CURSOR FOR SELECT dbo.omStatus.omStatusAlphaShort
> > FROM dbo.omBranchStatus INNER JOIN
> > dbo.omStatus ON
> > dbo.omBranchStatus.omStatusID = dbo.omStatus.omStatusID
> > WHERE dbo.omBranchStatus.omBranchID = @omBranchID AND
> > omBranchStatus.omBranchStatusComplete = 0
> > Order By omStatus.omStatusRank
>
> > OPEN MyCursor
> > FETCH MyCursor INTO @work
> > WHILE @@FETCH_STATUS <> -1
> > BEGIN
>
> > set @string = @string + @work
>
> > FETCH MyCursor INTO @work
> > END
> > DEALLOCATE MyCursor
>
> > PRINT @string
>
> > What is the solution to this and what is the problem called (so I can
> > learn about it)


Hugo Kornelis

3/19/2007 10:23:00 PM

0

On 19 Mar 2007 09:18:32 -0700, manganb@gmail.com wrote:

>Why is it that I had to set the string to ''?

Hi Brian,

That's because according to the ANSI standard for SQL, any expression
that includes at least one NULL argument should result in NULL (unless
explicitly noted otherwise, which is only the case for very few
expressions).

The reason behind this, is that NULL signifies that the data is missing
from the database, not that it doesn't exist at all. It might exist, it
is just not known to the database. So if you concatenate something to an
unknown string, the result surely can only be another unknown string.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hug...