我在建一个存储过程的时候要涉及到根据参数的值来判断要怎么执行,在一般的语言中都有IF语句,那么在SQL中有类似的吗?请各位高手指点一下!例:
create proc aaa(@ad int)
as
下面是如果@ad为0则执行一组语句
   如果@ad为1的话又执行一组语句,在SQL中要怎么来写?

解决方案 »

  1.   

    IF @ad>0
    BEGIN
    ...
    END
      

  2.   

    if @ad=0 
    beginend 
    elseif @ad=1
    beginend 
      

  3.   

    if @ad=0 
    beginend 
    elseif @ad=1
    beginend 
      

  4.   

    if @ad=0 
    beginend 
    elseif @ad=1
    beginend 
      

  5.   

    提问之前,看一下手册会有很大收获。SQL Server 2005 Books Online  
     
    IF...ELSE (Transact-SQL)  IF Boolean_expression 
         { sql_statement | statement_block } 
    [ ELSE 
         { sql_statement | statement_block } ] Examples
    DECLARE @compareprice money, @cost money 
    EXECUTE usp_GetList '%Bikes%', 700, 
        @compareprice OUT, 
        @cost OUTPUT
    IF @cost <= @compareprice 
    BEGIN
        PRINT 'These products can be purchased for less than 
        $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
    END
    ELSE
        PRINT 'The prices for all products in this category exceed 
        $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
      

  6.   

    if 条件
    begin
    ...
    end