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.
| Author |
Topic |
|
yessi
Starting Member
2 Posts |
Posted - 2006-03-27 : 23:37:43
|
| Hi guys.......i ahve a trouble with stored procedurethis is my tableEmployeeID ManagerID Amountallen ryan 20000andrew Richard 10000bob sarah 1000charles robert 1000fred john 1000james allen 10000janet andrew 5000jeff sarah 1000john tricia 5000mary john 1000michelle richard 10000peter robert 1000richard ryan 20000robert james 5000ryan ryan above 20000sarah michelle 5000tricia allen 10000veronica andrew 5000For example if bob request price of purchase order less than 1000 the approval value will be set to bob(because he has maximum amount=1000), but if price of purchase order more than 1000 and less than 5000 it will set the approval value will be set to Sarah(Because Sarah is manager of bob) and if the value more than 5000 and less than 10000 it will set the approval value to michelle(cause michelle is sarah's manager) and if the value more than 10000 and less than 20000, the approval value will be set to Richard(cause richard is Michelle's Manager) and if value more than 20000, the approval value will be set to Ryan(Cause Ryan is a high level of approval and richard's manager).I have created the stored procedure but i dont know to continue my stored procedure, cause i juts have a little understanding of next approval, I have craeted may stored procedure but with the simple one(:( )CREATE PROCEDURE [dbo].[R_Next_Approval] ( @vUserID as nvarchar(50), @iAmount as int)ASBEGIN DECLARE @Manager as nvarchar(50) DECLARE @Authorised as varchar(1) DECLARE @temp as int DECLARE @amount as int SET @Manager = @vUserID SET @temp=@iAmount select amount, employeeID from Employees where amount =@iAmount IF ( SELECT amount FROM dbo.Employees WHERE employeeID = @vUserID) =@iAmount BEGIN SET @Authorised = 'Y' END ELSE SET @Authorised = 'N' if ( SELECT amount FROM dbo.Employees WHERE employeeID = @vUserID) >= @temp BEGIN SET @Manager = ( SELECT employeeID FROM dbo.Employees WHERE amount = @temp) IF (SELECT amount FROM dbo.Employees WHERE employeeID = @Manager) = @iAmount BEGIN SET @Authorised = 'Y' END END ENDGOPlease help me........................what should i do in my stored procedure or did i miss something?Best regardsYessi |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-03-28 : 05:30:10
|
| Something likedeclare @Authorised nvarchar(50) ,@next nvarchar(50), @userid nvarchar(50) select @next = @vUserIDwhile @Authorised is null and coalesce(@userid, @next) <> @nextbegin select @Authorised = case when Amount >= @iAmount then @next else null end , @userid = @next , @next = ManagerID from tbl where EmployeeID = @nextendselect AuthorisedBy = case when @userid = @next then 'high level manager ' = @userid else @userid end==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-28 : 06:50:04
|
Hi all,Yessi - this might be a little easier to get to grips with. Just run it...  --datadeclare @t table (EmployeeID varchar(20), ManagerID varchar(20), Amount int)insert @t select 'allen', 'ryan', 20000union all select 'andrew', 'Richard', 10000union all select 'bob', 'sarah', 1000union all select 'charles', 'robert', 1000union all select 'fred', 'john', 1000union all select 'james', 'allen', 10000union all select 'janet', 'andrew', 5000union all select 'jeff', 'sarah', 1000union all select 'john', 'tricia', 5000union all select 'mary', 'john', 1000union all select 'michelle', 'richard', 10000union all select 'peter', 'robert', 1000union all select 'richard', 'ryan', 20000union all select 'robert', 'james', 5000union all select 'ryan', 'ryan', nullunion all select 'sarah', 'michelle', 5000union all select 'tricia', 'allen', 10000union all select 'veronica', 'andrew', 5000--inputsdeclare @vUserID nvarchar(50)declare @iAmount intset @vUserID = 'bob'set @iAmount = 20001--calculationwhile 0 = 0begin select @vUserID = ManagerID from @t where EmployeeID = @vUserID and Amount < @iAmount if @@rowcount = 0 breakendselect @vUserID Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
|
|
|
|
|