Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL Cursor Problem

Author  Topic 

stan_the_man21ph
Starting Member

5 Posts

Posted - 2010-03-15 : 23:45:40
Hi,

I have this code snippet:

CREATE FUNCTION [dbo].[fnGetSCACMembership]
(
@InAtomID VARCHAR(50)
)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @SCACLists AS VARCHAR(255)
DECLARE @SCACListsTemp AS VARCHAR(50)

DECLARE db_cursor CURSOR FAST_FORWARD FOR
SELECT
g.Name
FROM dbo.UserGroupMembership u
JOIN dbo.Groups g ON u.GroupAtomId = g.GroupsAtomId
WHERE useratomid = @InAtomID

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @SCACListsTemp
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SCACLists = @SCACListsTemp
FETCH NEXT FROM db_cursor INTO @SCACListsTemp
END
CLOSE db_cursor
DEALLOCATE db_cursor

RETURN @SCACLists

END


This part:
SET @SCACLists = @SCACListsTemp

I want to make it as
SET @SCACLists = @SCACLists + @SCACListsTemp

but when I do that, @SCACLists will not yield any value unlike the first wherein only the last value of @SCACListsTemp is saved to @SCACLists.

What seems to be the problem here? Does the @SCACLists losses its value inside the loop?


Thanks

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-15 : 23:52:57
quote:
Originally posted by stan_the_man21ph

Hi,

I have this code snippet:

CREATE FUNCTION [dbo].[fnGetSCACMembership]
(
@InAtomID VARCHAR(50)
)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @SCACLists AS VARCHAR(255)
DECLARE @SCACListsTemp AS VARCHAR(50)

DECLARE db_cursor CURSOR FAST_FORWARD FOR
SELECT
g.Name
FROM dbo.UserGroupMembership u
JOIN dbo.Groups g ON u.GroupAtomId = g.GroupsAtomId
WHERE useratomid = @InAtomID

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @SCACListsTemp
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SCACLists = @SCACListsTemp
FETCH NEXT FROM db_cursor INTO @SCACListsTemp
END
CLOSE db_cursor
DEALLOCATE db_cursor

RETURN @SCACLists

END


This part:
SET @SCACLists = @SCACListsTemp

I want to make it as
SET @SCACLists = @SCACLists + @SCACListsTemp

but when I do that, @SCACLists will not yield any value unlike the first wherein only the last value of @SCACListsTemp is saved to @SCACLists.

What seems to be the problem here? Does the @SCACLists losses its value inside the loop?


Thanks



could you please show us some sample data and expected output.this would be easy for us to provide you the solution better.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 00:26:07
you dont need cursor at all. what you need is just this

CREATE FUNCTION [dbo].[fnGetSCACMembership]
(
@InAtomID VARCHAR(50)
)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @SCACLists AS VARCHAR(255)

SELECT @SCACLists=COALESCE(@SCACLists + ',','') +
g.Name
FROM dbo.UserGroupMembership u
JOIN dbo.Groups g ON u.GroupAtomId = g.GroupsAtomId
WHERE useratomid = @InAtomID

RETURN @SCACLists

END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

stan_the_man21ph
Starting Member

5 Posts

Posted - 2010-03-16 : 00:47:13
quote:
Originally posted by visakh16

you dont need cursor at all. what you need is just this

CREATE FUNCTION [dbo].[fnGetSCACMembership]
(
@InAtomID VARCHAR(50)
)
RETURNS VARCHAR(255)
AS
BEGIN
DECLARE @SCACLists AS VARCHAR(255)

SELECT @SCACLists=COALESCE(@SCACLists + ',','') +
g.Name
FROM dbo.UserGroupMembership u
JOIN dbo.Groups g ON u.GroupAtomId = g.GroupsAtomId
WHERE useratomid = @InAtomID

RETURN @SCACLists

END


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Hi Visakhm!
Your code works perfectly!Thanks......Anyway still wondering though why the cursor method did not work as intended, any comments or ideas?....Anyway thanks again for your brilliant solution.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 00:55:18
it didnt work as you didnt initialise the variable @SCACLists . it will be null by default so you need to do

SET @SCACLists = COALESCE(@SCACLists,'') + @SCACListsTemp
instead of just

SET @SCACLists = @SCACLists + @SCACListsTemp



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

stan_the_man21ph
Starting Member

5 Posts

Posted - 2010-03-16 : 01:25:32
quote:
Originally posted by visakh16

it didnt work as you didnt initialise the variable @SCACLists . it will be null by default so you need to do

SET @SCACLists = COALESCE(@SCACLists,'') + @SCACListsTemp
instead of just

SET @SCACLists = @SCACLists + @SCACListsTemp



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Ah i see. Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-16 : 04:01:30
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -