怎样在存储过程中根据变量的不同使sql语句中where后面的条件不一样!怎么做判断!哪位gg帮帮忙

解决方案 »

  1.   


    CREATE PROCEDURE tp_Fetch_MainEmail_By_UserType
    (
     @UserType INT 
    )
    AS 
    BEGIN 
    SET NOCOUNT ON 

    IF @UserType = 391
    BEGIN          SELECT  A.FirstName + ' ' + A.LastName AS FullName,
    A.PersonID,
    B.ItemValue AS Email
      FROM PersonBase A
    Left JOIN PersonContactInfo B ON B.PersonID = A.PersonID AND B.ContactMethodCode = 404
    order by FullName
    END 

    IF @UserType = 392
    BEGIN 
    SELECT A.StaffFirst + ' ' + A.StaffLast AS FullName,
    A.StaffBaseID as PersonID,
    B.ItemValue AS Email
      FROM  StaffBase A
    Left JOIN StaffContactInfo B ON B.StaffBaseID = A.StaffBaseID AND B.ContactMethodCode = 404
    order by FullName
    END 

    IF @UserType = 393
    BEGIN 
    SELECT A.FirstName + ' ' + A.LastName AS FullName,
    A.ConTactBaseID as PersonID,
    B.ItemValue AS Email
      FROM ContactBase A
    LEFT JOIN ContactContactInfo B ON B.ConTactBaseID = A.ConTactBaseID AND B.ContactMethodCode = 404
    order by FullName END  IF @@ERROR <> 0
       RETURN 1  RETURN 0
    END GO
      

  2.   

    什么时候SQL Server支持Where后面用case作判断了?
    将你的变量作为参数传递给存储过程,存储过程中用if语句判断参数,分别执行。如果有多种情况那只能是多个if语句,case无法实现。
    如:singlepine(小山)
      

  3.   

    你看一下这个就明白了
    create  procedure TB_Login_insert_or_select
    (
    @id                    varchar(10),
    @password          varchar(10),
    @name               varchar(30)='',
    @erro                bit   output,
    @opreat             bit
    )
    as
    declare @number int
    if  (  @opreat=1)
     begin
       select @number=count(LU_id) from TB_Login where LU_id=@id 
        if  (@number=0)
          begin
            insert into TB_Login (LU_id,LU_password,LU_name) values(@id,@password,@name)
            set @erro=1
          end
        else
          begin
            set @erro=0
          end
     end else
      begin
       select  @number=count(LU_id) from TB_Login where LU_id=@id and LU_password=@password
         if  (@number=0)
       begin
           set @erro=0
      end
         else
    begin
           set @erro=1
    end
      end