C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

Using of While Loop In Stored Procedure.


Posted Date: 15 Jul 2009    Resource Type: Code Snippets    Category: SQL
Author: satyaMember Level: Diamond    
Rating: 1 out of 5Points: 12





Using of While Loop in a Stored Procedure.






Description : Using of While Loop in a Stored Procedure, which I have written in MicroSoft SQL Server 2000. This is used

for certain action on a table. Scenario: I have got an requirement that, Currently in a table I have 1000 Assets(computers), when administrator wants to assign assets to delivery center, then administrator enters only numeric text like 100 assests need to assign to to Delivery Center, Than using while loop we are randomly picking the assets and assign to the Delivery Center.






CREATE Procedure Ps__UpdateAssetBul
------Variable that needs to be passed.-------
------Global Variables-------
@CNTINP Int, --Count that needs to assigned from Total Count by dynamically.
@DCenterId Varchar(50), --Delivery Center to which the assets needs to be assigned.
@Result Varchar(50) Output --Status that returns to the user.
As
Begin
--Start of While Loop for assigning number of products that are to be assigned for an delivery center.
WHILE(@CNTINP != 0)
BEGIN
------Local Variables-------
Declare @AssetStatus Varchar(50) ------To check the status of the product, whether it is already assigned.-------
Declare @Assetid varchar(50) ------To store the assetid of the product, whether it is already assigned.-------
--We will dynamically pick the Asset from the number of assets available.
select Top 1 @Assetid = assetid
from _Asset_Details e1 where Prodid = @ProdId And Status = 'unassigned'
--We will pick the AssetStatus of the Selected AssetId.
Select @AssetStatus = Status From AMS_Asset_Details
Where DeletionSTatus = 'U' And AssetId = @Assetid
--Condition to verify whether it is not assigned to any center.
If(@AssetStatus = 'UnAssigned')
Begin
--If the condition satisfies then it updates.
Update AMS_Asset_Details
Set Status = 'Assigned'
Where AssetId = @Assetid And DeletionStatus = 'U'
End
Else
Begin
--If the Status of the Asset is Assigned. Then we check the Count of the Assets is available.
If((select count(*)
from AMS_Asset_Details
where Prodid = @ProdId And Status = 'unassigned') > 0)
Begin
--If there are no Assets available then we increment the count.
Set @CNTINP = @CNTINP+1
End
End
SET @CNTINP = @CNTINP - 1
END
--if(@@error <> 0), condition to check that if there are any errors occured during the Updation.
If(@@Error <> 0)
Begin
--If an error encounters during the updation.
Set @Result = 'Error'
End
Else
Begin
--If there is no error, then output returns as 'Success'.
Set @Result = 'Success'
End
End




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Using of While Loop In Stored Procedure.  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Function To Get Tabular Data From a Delimilted String Variable
Previous Resource: Import .csv to SQL database
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use