简体   繁体   中英

How to Call in stored procedure another procedure with if statement

Have table users with vallues :

FirstName     LastName     Age
 A               A          20
 B               B          21
 C               C          22
 D               D          21
 E               E          20

then I create procedure ,where want check Age then in if statement call another procedures

SELECT @firstName = FirstName ,lastName = LastName ,@age = Age FROM dbo.users
if @age = 20
exec myProc1 'firstName','LastName'
if @age = 21
exec myProc2 'firstName','LastName'
if @age = 22
exec myProc3 'firstName','LastName'

When I call this procedure if statement take only one row and then call another procedure which is in IF statements,for example works only

   if @age = 20
    exec myProc1 'firstName','LastName'

because Age = 20 is the first row in table. I want check all rows in table ,how do it ? Is it possible to do it without cursor ?

It is possible to do this without a cursor. But it still uses looping.

declare @t table as (id int identity(1, 1) primary key,
                     FirstName varchar(255),
                     LastName varchar(255),
                     Age int
                    );

insert into @t(FirstName, LastName, Age)
    select FirstName, LastName, Age
    from dbo.users;

declare @maxi int = (select max(id) from @t);

declare @i = 0, @FirstName varchar(255), @LastName varchar(255), @Age int;

while @i <= maxi
begin
    select @FirstName = FirstName, @LastName = LastName, @Age = age
    from @t
    where id = @i;
    if @age = 20
        exec myProc1 @firstname, @LastName;
    if @age = 21
        exec myProc2 @firstname, @LastName;
    if @age = 22
       exec myProc3 @firstname, @LastName;

    set @i = @i + 1;
end;

A couple things. First, you don't really need the temporary table. It is convenient for getting the loop counter. Second, I also assume you want the variables as arguments to the three stored procedures rather than constant strings.

Create a table variable:

DECLARE @List TABLE(Id int IDENTITY(1,1), FirstName nVarchar(Max), LastName nVarchar(Max), Age INT)

then insert all data with in table variable

INSERT INTO @List (FirstName,LastName,Age)(SELECT FirstName, LastName, Age FROM YourTable)

DECLARE @intNumberOfRecords int, @intRowCount int, @FirstName nVarchar(Max), @LastName nVarchar(Max), @Age int
SET @intNumberOfRecords = @@ROWCOUNT
SET @intRowCount = 1  

WHILE @intRowCount <= @intNumberOfRecords  
BEGIN 


select @FirstName = FirstName, @LastName = LastName, @Age = age
from YourTable Where Id = @intRowCount 
if @Age = 20
    exec myProc1 @firstname, @LastName;
if @Age = 21
    exec myProc2 @firstname, @LastName;
if @Age = 22
   exec myProc3 @firstname, @LastName;

SET @intRowCount = @intRowCount + 1;

END

User can Also use CASE statement here instead of if

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM