SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Script not working?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

blakemckenna
Starting Member

1 Posts

Posted - 10/05/2012 :  14:37:00  Show Profile  Reply with Quote
I have a small SQL script that isn't producing the results I'm expecting. I'm not real savvy writing SQL code. If I just run the "Select" statement, it will return 4 rows. However, using the cursor in the way that I am, it's not returning anything. What am I
doing wrong?

The value that is returned is NULL.

Thanks,
Blake


BEGIN
	DECLARE @string		varchar(25)
	DECLARE @output		varchar(6000)

	DECLARE tmpCur CURSOR STATIC LOCAL
	FOR SELECT DISTINCT A.TW_PSuid
 	         FROM RespParty A INNER JOIN Citations B ON B.TW_PSUID = A.TW_PSUID 
	       WHERE RPName Like '%meyers%'	And 
		  A.TW_PSuid Is Not Null	And
		  EntityNumber In ('0717','0718');

	OPEN tmpCur

	WHILE 1 = 1
	BEGIN 
	   FETCH tmpCur INTO @string
	   IF @@fetch_status <> 0 
		  BREAK

	   SET @output = @output + @string + ',';
	END

	SELECT @output AS PSUID;

	CLOSE tmpCur;
	DEALLOCATE tmpCur
END

chadmat
The Chadinator

USA
1974 Posts

Posted - 10/05/2012 :  14:39:31  Show Profile  Visit chadmat's Homepage  Reply with Quote
And why are you wanting to use a cursor here?

-Chad
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/06/2012 :  00:25:04  Show Profile  Reply with Quote
you just need this and no cursor


SELECT STUFF((SELECT DISTINCT ',' + CAST(A.TW_PSuid AS varchar(25))
 	         FROM RespParty A INNER JOIN Citations B ON B.TW_PSUID = A.TW_PSUID 
	       WHERE RPName Like '%meyers%' And 
		  A.TW_PSuid Is Not Null	And
		  EntityNumber In ('0717','0718')
                  FOR XML PATH('')
),1,1,'')


If you really want to use cursor solution you need to initialise @output to '' or modify code as below

...
 SET @output = COALESCE(@output,'') + @string + ',';
..



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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000