0

我正在使用 SQL Server 2008 R2 我有这样的存储过程:

CREATE PROCEDURE Get_Code_Tourne_Matin
    -- Add the parameters for the stored procedure here
@The_FA int,
@The_Jour int,
@The_Insee varchar(10)
AS
BEGIN
declare @TheCode varchar(250)


set @TheCode = case
    when exists (SELECT T_TOURNE_LABEL.LIBELLE 
                  FROM TOURNE
                  LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
                  WHERE THE_FA = @The_FA AND 
                        NO_INSEE = @The_Insee AND 
                        JOUR = @The_Jour AND 
                       (datepart(hh, LE_HEURE) between 13 and 23 or datepart(hh, LE_HEURE) between 0 and 6)) then LIBELLE
    when exists (SELECT LIBELLE
                  FROM TOURNE
                  LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
                  WHERE THE_FA = @The_FA AND 
                        NO_INSEE = @The_Insee AND 
                        JOUR = 0 AND 
                       (datepart(hh, LE_HEURE) between 13 and 23 or datepart(hh, LE_HEURE) between 0 and 6)) then LIBELLE
    else '00'


    end


RETURN @TheCode  
END

我只想归还诽谤。
找到第一个带有 3 个变量的选择,然后重新运行 libelle
else 如果找到运行带有 2 个变量的选择,然后重新运行 libelle
else retrun '00'

LIBELLE 似乎有语法错误

任何的想法 ?

4

3 回答 3

3

我认为是这样的:

set @TheCode = COALESCE(
(SELECT TOP 1 T_TOURNE_LABEL.LIBELLE 
                  FROM TOURNE
                  LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
                  WHERE THE_FA = @The_FA AND 
                        NO_INSEE = @The_Insee AND 
                        (JOUR = @The_Jour OR JOUR = 0) AND 
                       (datepart(hh, LE_HEURE) between 13 and 23 or datepart(hh, LE_HEURE) between 0 and 6)
ORDER BY JOUR DESC), '00')

应该涵盖你所有的情况。(如果您的查询中永远不可能有两个匹配的行,一个 hasJOUR = @The_Jour另一个 has JOUR = 0,那么您可以删除TOP 1andORDER BY子句(请注意,我假设任何可能的@The_Jour值都大于 0)

于 2013-03-05T09:14:41.987 回答
0
set @TheCode =(  SELECT   ISNULL(( SELECT TOP 1 T_TOURNE_LABEL.LIBELLE
               FROM   TOURNE
               LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
               WHERE  THE_FA = @The_FA AND NO_INSEE = @The_Insee AND JOUR = @The_Jour
                               AND ( DATEPART(hh, LE_HEURE) BETWEEN 13 AND 23
                               OR DATEPART(hh, LE_HEURE) BETWEEN 0 AND 6
                               )), '00') AS LIBELLE
于 2013-03-05T09:31:39.897 回答
0
CREATE PROCEDURE Get_Code_Tourne_Matin
@The_FA int,
@The_Jour int,
@The_Insee varchar(10)
AS
BEGIN
declare @TheCode varchar(250)
    Set @TheCode = '';
    SELECT top 1 @TheCode = T_TOURNE_LABEL.LIBELLE 
            FROM TOURNE
            LEFT JOIN T_TOURNE_LABEL ON TOURNE.LIB_TOURNE = T_TOURNE_LABEL.NOID
            WHERE THE_FA = @The_FA AND 
                NO_INSEE = @The_Insee AND 
                (JOUR = @The_Jour or JOUR = 0 )AND 
                (datepart(hh, LE_HEURE) between 13 and 23 or datepart(hh, LE_HEURE) between 0 and 6)

    set @TheCode = case
        when len(@TheCode) > 0
        when exists (SELECT @TheCode)
        else '00'

        end
    RETURN @TheCode  
END
于 2013-03-05T09:45:30.520 回答