如果条件在函数内

时间:2014-04-01 12:49:20

标签: sql function sql-function

我需要在我的Function中创建一个条件,如果是真的,请使用一个sql语句,如果不使用另一个,但是我遇到了问题。我的功能代码:

ALTER FUNCTION [dbo].[FN_TODAS_SMP]
(   
   @STATUS TINYINT, 
   @VALOR_CARGA DECIMAL(12,2),  
   @PLACA VARCHAR(10), 
   @NUMERO_SMP INT, 
   @WORKFLOW CHAR(1),
   @DESC_FROTA_CLIENTE VARCHAR(25), 
   @FILTRAR_PLACA BIT, 
   @FILTRAR_SMP BIT, 
   @FILTRAR_VALOR BIT, 
   @FILTRAR_WORKFLOW BIT,
   @FILTRAR_FROTA BIT,
   @ID_USUARIO INT, 
   @FILTRA_REGIOES BIT,
   @IS_REGIONALIZADO BIT
)
RETURNS TABLE 
AS
RETURN 
(
    WITH REGIONS AS
    (   
        SELECT
            ISNULL(dbCTRL_REGIAO.ID_REGIAO, -1) ID_REGIAO
        FROM
            dbUSUARIO
            INNER JOIN DBCONTROLE_REGIAO_GRUPO_USUARIO dbCTRL_REGIAO ON(dbUSUARIO.ID_GRUPO = dbCTRL_REGIAO.ID_GRUPO)
            INNER JOIN dbCLIENTE_REGIAO CR ON dbCTRL_REGIAO.ID_REGIAO = CR.ID_REGIAO
        WHERE ID_USUARIO = @ID_USUARIO --1884
        GROUP BY dbCTRL_REGIAO.ID_REGIAO
    ),
    STATUS_IN AS
    (
      -- Marcio Silveira - 05/12/2011 - Ajuste para telemonitoramento
      -- @STATUS /* 2: MONITORAMENTO - 9: LOGISTICO (TELEMONITORAMENTO) - 5: PENDENTE - 8: EM ESPERA - 4: EM APROVAÇÃO */)
      -- QUANDO STATUS IGUAL 2 EM MONITORAMENTO DEVE TRAZER SMP'S STATUS 9 LOGISTICO TAMBÉM
      SELECT @STATUS STATUS_SMP
      UNION
      SELECT 9 STATUS_SMP
      WHERE @STATUS = 2
    ),
   SMP_BASE_TEMP AS
   (
      SELECT ID_SMP, TIPODONO
      FROM DBSMP_BASE TEMP
      WHERE TIPODONO = 'T'
   ),
   SMP_BASE_DONO AS
   (
      SELECT
         TEMP.ID_SMP, TEMP.TIPODONO, TEMP.ID_BASE, TEMP.TIPOBASE, TEMP.COMANDA, TEMP.MEXEU
      FROM
         DBSMP_BASE TEMP
            LEFT JOIN SMP_BASE_TEMP V ON V.ID_SMP = TEMP.ID_SMP
      WHERE
         ISNULL(V.TIPODONO, 'D') = TEMP.TIPODONO
   )

   SELECT           
      SMP.ABREV_TECNOLOGIA, SMP.STATUS, SMP.STSMON, SMP.STSGER, SMP.NUMERO_SMP, SMP.VEICULO_PLACA, SMP.APELIDO, SMP.IGNICAO,
      SMP.WORKFLOW, SMP.VALOR_CARGA, SMP.DTINICIOPREV, SMP.DTINICIOREAL, SMP.DTULTPOSICAO, SMP.REF2, SMP.ID_SMP,
      SMP.MODULO, SMP.ID_TECNOLOGIA, SMP.PREFIXO_TECNOLOGIA, SMP.LATITUDE, SMP.LONGITUDE, SMP.LATITUDE_DES, SMP.LONGITUDE_DES, SMP_BASE.ID_BASE,
      SMP.TIPO TIPOSMP, SMP.ID_ROTA, SMP_BASE.TIPODONO, SMP_BASE.COMANDA, SMP_BASE.TIPOBASE, SMP_BASE.MEXEU, BASE.DESCRICAO,
      VEICULO.DESC_FROTA_CLIENTE
   FROM
      DBUSUARIO U
      INNER JOIN DBUSUARIO_GRUPO UG ON U.ID_GRUPO = UG.ID_GRUPO
      INNER JOIN dbCONTROLE_REGIAO_GRUPO_USUARIO CRGU ON U.ID_GRUPO = CRGU.ID_GRUPO
      INNER JOIN dbCLIENTE_REGIAO CR ON CRGU.ID_REGIAO = CR.ID_REGIAO
      INNER JOIN DBCLIENTE C ON CR.ID_CLIENTE = C.ID_CLIENTE
      INNER JOIN DBSMP AS SMP ON C.ID_CLIENTE = SMP.ID_CLIENTE

      LEFT JOIN SMP_BASE_DONO AS SMP_BASE ON SMP_BASE.ID_SMP = SMP.ID_SMP
      LEFT JOIN DBBASE AS BASE ON BASE.ID_BASE = SMP_BASE.ID_BASE
      INNER JOIN DBVEICULO AS VEICULO ON VEICULO.ID_VEICULO = SMP.ID_VEICULO

   WHERE
      (SMP.STATUS IN (SELECT STATUS_SMP FROM STATUS_IN))
      AND ((@FILTRAR_PLACA = 0) OR (SMP.VEICULO_PLACA = @PLACA /* FILTRAR POR PLACA */))
      AND ((@FILTRAR_SMP = 0) OR (SMP.NUMERO_SMP = @NUMERO_SMP /* FILTRAR POR NÚMERO DE SMP */))
      AND ((@FILTRAR_VALOR = 0) OR (SMP.VALOR_CARGA >= @VALOR_CARGA /* SMP COM VALORES ACIMA DE */))
      AND ((@FILTRAR_WORKFLOW = 0) OR (SMP.WORKFLOW = @WORKFLOW /* A: ABERTO - B: BAIXADO */))
      AND ((@FILTRAR_FROTA = 0) OR (VEICULO.DESC_FROTA_CLIENTE = @DESC_FROTA_CLIENTE /* FILTRAR POR DES. DA FROTA */))
      AND (
                (@FILTRA_REGIOES = 0) OR
                (
                    (SMP.ID_REGIAO1 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO2 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO3 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO4 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO5 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO6 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO7 IN (SELECT ID_REGIAO FROM REGIONS))
                )
          )
      AND U.ID_USUARIO = @ID_USUARIO

)

我想做点像

 ... same code
IF conditionIsTrue

SELECT          
      SMP.ABREV_TECNOLOGIA, SMP.STATUS, SMP.STSMON, SMP.STSGER, SMP.NUMERO_SMP, SMP.VEICULO_PLACA, SMP.APELIDO, SMP.IGNICAO,
      SMP.WORKFLOW, SMP.VALOR_CARGA, SMP.DTINICIOPREV, SMP.DTINICIOREAL, SMP.DTULTPOSICAO, SMP.REF2, SMP.ID_SMP,
      SMP.MODULO, SMP.ID_TECNOLOGIA, SMP.PREFIXO_TECNOLOGIA, SMP.LATITUDE, SMP.LONGITUDE, SMP.LATITUDE_DES, SMP.LONGITUDE_DES, SMP_BASE.ID_BASE,
      SMP.TIPO TIPOSMP, SMP.ID_ROTA, SMP_BASE.TIPODONO, SMP_BASE.COMANDA, SMP_BASE.TIPOBASE, SMP_BASE.MEXEU, BASE.DESCRICAO,
      VEICULO.DESC_FROTA_CLIENTE
   FROM
      DBUSUARIO U
      INNER JOIN DBUSUARIO_GRUPO UG ON U.ID_GRUPO = UG.ID_GRUPO
      INNER JOIN dbCONTROLE_REGIAO_GRUPO_USUARIO CRGU ON U.ID_GRUPO = CRGU.ID_GRUPO
      INNER JOIN dbCLIENTE_REGIAO CR ON CRGU.ID_REGIAO = CR.ID_REGIAO
      INNER JOIN DBCLIENTE C ON CR.ID_CLIENTE = C.ID_CLIENTE
      INNER JOIN DBSMP AS SMP ON C.ID_CLIENTE = SMP.ID_CLIENTE

      LEFT JOIN SMP_BASE_DONO AS SMP_BASE ON SMP_BASE.ID_SMP = SMP.ID_SMP
      LEFT JOIN DBBASE AS BASE ON BASE.ID_BASE = SMP_BASE.ID_BASE
      INNER JOIN DBVEICULO AS VEICULO ON VEICULO.ID_VEICULO = SMP.ID_VEICULO

   WHERE
      (SMP.STATUS IN (SELECT STATUS_SMP FROM STATUS_IN))
      AND ((@FILTRAR_PLACA = 0) OR (SMP.VEICULO_PLACA = @PLACA /* FILTRAR POR PLACA */))
      AND ((@FILTRAR_SMP = 0) OR (SMP.NUMERO_SMP = @NUMERO_SMP /* FILTRAR POR NÚMERO DE SMP */))
      AND ((@FILTRAR_VALOR = 0) OR (SMP.VALOR_CARGA >= @VALOR_CARGA /* SMP COM VALORES ACIMA DE */))
      AND ((@FILTRAR_WORKFLOW = 0) OR (SMP.WORKFLOW = @WORKFLOW /* A: ABERTO - B: BAIXADO */))
      AND ((@FILTRAR_FROTA = 0) OR (VEICULO.DESC_FROTA_CLIENTE = @DESC_FROTA_CLIENTE /* FILTRAR POR DES. DA FROTA */))
      AND (
                (@FILTRA_REGIOES = 0) OR
                (
                    (SMP.ID_REGIAO1 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO2 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO3 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO4 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO5 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO6 IN (SELECT ID_REGIAO FROM REGIONS)) OR
                    (SMP.ID_REGIAO7 IN (SELECT ID_REGIAO FROM REGIONS))
                )
          )
      AND U.ID_USUARIO = @ID_USUARIO
ELSE
  run another SQL

但是我在“IF”处遇到了“不正确的Sintax”错误,您是否知道我必须如何处理呢?

2 个答案:

答案 0 :(得分:0)

你在使用BEGIN&结束.. EG:

 if condition = true  begin       
     select statement one
  end   
  else begin
    select statement two 
end

答案 1 :(得分:0)

此处有一些选项:https://stackoverflow.com/a/1558192/2773046

Common Table Expressions只能跟着我害怕的SELECT,INSERT,UPDATE,DELETE或MERGE语句。