Click here to Skip to main content
15,921,250 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Why does my stored procedure return 0 value instead of goods1,goods2,?

SQL
DECLARE @vendor_name nvarchar(50),@message varchar(80);

    
DECLARE vend_cursor CURSOR
    FOR SELECT DISTINCT dbo._Goods.god_Name
     FROM dbo._Roshani INNER JOIN
                     dbo._RoshaniGoods ON dbo._Roshani.rsh_id = dbo._RoshaniGoods.rsg_rshId INNER JOIN
                   dbo._Goods ON dbo._RoshaniGoods.rsg_godId = dbo._Goods.god_Id
                      where rsh_rgiId=@rgi_Id
OPEN vend_cursor
FETCH NEXT FROM vend_cursor
INTO  @vendor_name;

WHILE @@FETCH_STATUS=0
BEGIN
    --PRINT ' ';
    SELECT @message = ', ' + @vendor_name;
FETCH NEXT FROM vend_cursor
INTO  @vendor_name;
    
end
PRINT @message;
CLOSE vend_cursor;
DEALLOCATE vend_cursor;
Posted
Updated 5-Jun-11 22:22pm
v7
Comments
Dalek Dave 6-Jun-11 3:35am    
Edited for Grammar.

after the print operation you need the fetch the next value. Since you are fetching once only, value of "@@FETCH_STATUS" is remaining <> 0 and hence the infinite loop.
 
Share this answer
 
Comments
faezeh66 6-Jun-11 3:29am    
give me query plz
Dalek Dave 6-Jun-11 3:36am    
Good Call.
Check the result of the query first
SELECT DISTINCT dbo._Goods.god_Name
FROM dbo._Roshani 
  INNER JOIN dbo._RoshaniGoods ON dbo._Roshani.rsh_id = dbo._RoshaniGoods.rsg_rshId 
  INNER JOIN dbo._Goods ON dbo._RoshaniGoods.rsg_godId = dbo._Goods.god_Id
WHERE rsh_rgiId=@rgi_Id


Does it return 2 values?

Edit:
SQL
SET @message = @message + ',' + @vendor_name
 
Share this answer
 
v2
Comments
faezeh66 6-Jun-11 3:26am    
yes
Prerak Patel 6-Jun-11 3:31am    
After adding FETCH NEXT FROM vend_cursor INTO @vendor_name; in loop, did it work?
faezeh66 6-Jun-11 4:07am    
no but i get result
goods1
goods2

but i want goods1,goods2,...
Prerak Patel 6-Jun-11 4:11am    
Try using SET @message = @message + ',' + @vendor_name
faezeh66 6-Jun-11 4:22am    
i use it but return null

DECLARE @vendor_name varchar(50),@message varchar(80);
DECLARE vend_cursor CURSOR
FOR SELECT DISTINCT dbo._Goods.god_Name
FROM dbo._Roshani INNER JOIN
dbo._RoshaniGoods ON dbo._Roshani.rsh_id = dbo._RoshaniGoods.rsg_rshId INNER JOIN
dbo._Goods ON dbo._RoshaniGoods.rsg_godId = dbo._Goods.god_Id
where rsh_rgiId=@rgi_Id 
And isnull(dbo._Goods.god_Name,"0") <>  "0" -- You will need this line
OPEN vend_cursor
FETCH NEXT FROM vend_cursor
INTO @vendor_name;
 <code></code>
	WHILE @@FETCH_STATUS=0
	BEGIN
			--PRINT ' ';
			SELECT @vendor_name; ----- Products From Vendor:  @vendor_name;
			 
	FETCH NEXT FROM vend_cursor INTO @vendor_name; -- You Forgot this line
	END
PRINT @message;
CLOSE vend_cursor;
DEALLOCATE vend_cursor;<pre><pre>
 
Share this answer
 
v6
Comments
faezeh66 6-Jun-11 3:45am    
thank you, but it get me
goods1
goods2
0
but i want goods1,goods2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900