Groups | Blog | Home
all groups > sql server programming > march 2007 >

sql server programming : What is wrong with this String operation


manganb NO[at]SPAM gmail.com
3/18/2007 10:25:45 PM
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)
xyb
3/18/2007 10:31:52 PM
[quoted text, click to view]

em... your problem are clearly described.
set @string =3D @string + @work
this line i can guess @string should to be declare as varchar(8000),or
the date lenght will not enough.
manganb NO[at]SPAM gmail.com
3/18/2007 10:35:35 PM
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)

[quoted text, click to view]

xyb
3/18/2007 10:43:43 PM
[quoted text, click to view]

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

0 good
-1error or record not in the set
-2 record not in the set
look at BOL for more info.
manganb NO[at]SPAM gmail.com
3/18/2007 11:00:42 PM

Hi,

When I set WHILE @@FETCH_STATUS =3D 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..




[quoted text, click to view]


xyb
3/18/2007 11:10:24 PM
On 3=D4=C219=C8=D5, =CF=C2=CE=E72=CA=B100=B7=D6, "mang...@gmail.com" <mang.=
[quoted text, click to view]

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 =3D ''
select @tempstr =3D ''
declare myCur cursor
for
select [id] from sysobjects where [id] < 100

open myCur
fetch myCur into @tempstr
while @@fetch_status =3D 0
begin
select @string =3D @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 12:00:00 AM
Hi
Make sure that @work variable was not set to NULL.

[quoted text, click to view]

Jack Vamvas
3/19/2007 12:00:00 AM
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.ITjobfeed.com



[quoted text, click to view]

manganb NO[at]SPAM gmail.com
3/19/2007 9:18:32 AM
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



[quoted text, click to view]

Hugo Kornelis
3/19/2007 11:23:08 PM
[quoted text, click to view]

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
AddThis Social Bookmark Button