1. SQL / Говнокод #4203

    −125

    1. 1
    2. 2
    3. 3
    4. 4
    CREATE TABLE IF NOT EXISTS `ls_user_administrator` (
      `user_id` int(11) unsigned NOT NULL,
      UNIQUE KEY `user_id` (`user_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    Видимо сделать столбец 'administrator' в таблице `ls_user` и ставить в него 1 или 0 - это слишком просто.. Или тут скрыта какая-то оптимизация?
    Дамп спёрт из LiveStreet.

    hybroid, 08 Сентября 2010

    Комментарии (26)
  2. SQL / Говнокод #4200

    −863

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    if (($pol) and ($brand)) {
        $result = mysql_query("SELECT * FROM inetmag WHERE pol = '$pol' AND brand = '$brand'");
    }
    ;
    if ((!$pol) and ($brand)) {
        $result = mysql_query("SELECT * FROM inetmag WHERE brand = '$brand'");
    }
    ;
    if (($pol) and (!$brand)) {
        $result = mysql_query("SELECT * FROM inetmag WHERE pol = '$pol'");
    }
    ;
    if ((!$pol) and (!$brand)) {
        $result = mysql_query("SELECT * FROM inetmag");
    }
    ;

    мой учитель сильно негодовал, и предложил вот это:
    $url = <<< TEXT
    SELECT *
    FROM inetmag
    WHERE
    pol = IF(LENGTH('$pol')<>0,('$pol'),pol)
    AND
    brand = IF(LENGTH ('$brand')<>0,('$brand'),brand)
    TEXT;
    $result = mysql_query($url);

    truedizzy, 08 Сентября 2010

    Комментарии (8)
  3. SQL / Говнокод #4177

    −865

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    FUNCTION f_decncity_arr_find_base(p_gds_id IN NUMBER, p_date IN DATE DEFAULT SYSDATE) RETURN VARCHAR2 IS
        v_denc15 NUMBER; v_k0 NUMBER; v_k1 NUMBER;
    BEGIN
        decncity_arr_find_base(p_gds_id, p_date, v_denc15, v_k0, v_k1);
        RETURN 'DENC15!=@'||TO_CHAR(v_denc15)||'@ K0!=@'||TO_CHAR(v_k0)||'@ K1!=@'||TO_CHAR(v_k1)||'@';
    END;

    Так и не поняли, что это....

    markbrutt, 06 Сентября 2010

    Комментарии (3)
  4. SQL / Говнокод #4147

    −856

    1. 1
    SELECT i.*, p.link as p_link, p.title as p_title, p.p_id FROM `items` i, `places` p WHERE i.public='1' and i.place=p.id and  (i.place='1' or i.place='1972' or i.place='2' or i.place='3' or i.place='4' or i.place='5' or i.place='6' or i.place='7' or i.place='8' or i.place='9' or i.place='10' or i.place='11' or i.place='12' or i.place='13' or i.place='14' or i.place='15' or i.place='16' or i.place='17' or i.place='18' or i.place='19' or i.place='20' or i.place='21' or i.place='22' or i.place='23' or i.place='24' or i.place='25' or i.place='26' or i.place='27' or i.place='28' or i.place='29' or i.place='30' or i.place='31' or i.place='32' or i.place='33' or i.place='34' or i.place='35' or i.place='36' or i.place='37' or i.place='38' or i.place='39' or i.place='40' or i.place='41' or i.place='42' or i.place='43' or i.place='44' or i.place='45' or i.place='46' or i.place='47' or i.place='48' or i.place='49' or i.place='50' or i.place='51' or i.place='52' or i.place='53' or i.place='54' or i.place='55' or i.place='56' or i.place='57' or i.place='58' or i.place='59' or i.place='60' or i.place='61' or i.place='62' or i.place='63' or i.place='64' or i.place='65' or i.place='66' or i.place='67' or i.place='68' or i.place='69' or i.place='70' or i.place='71' or i.place='72' or i.place='73' or i.place='74' or i.place='75' or i.place='76' or i.place='77' or i.place='78' or i.place='79' or i.place='80' or i.place='81' or i.place='82' or i.place='83' or i.place='84' or i.place='85' or i.place='86' or i.place='87' or i.place='88' or i.place='89' or i.place='90' or i.place='91' or i.place='92' or i.place='93' or i.place='94' or i.place='95' or i.place='96' or i.place='97' or i.place='98' or i.place='99' or i.place='100' or i.place='101' or i.place='102' or i.place='103' or i.place='104' or i.place='105' or i.place='106' or i.place='107' or i.place='108' or i.place='109' or i.place='110' or i.place='111' or i.place='112' or i.place='113' or i.place='114' or i.place='115' or i.place='116' or i.place='117' or i.place='118' or i.place='119' or i.place='120' or i.place='121' or i.place='122' or i.place='123' or i.place='124' or i.place='125' or i.place='126' or i.place='127' or i.place='128' or i.place='129' or i.place='130' or i.place='131' or i.place='132' or i.place='133' or i.place='134' or i.place='135' or i.place='136' or i.place='137' or i.place='138' or i.place='139' or i.place='140' or i.place='141' or i.place='142' or i.place='143' or i.place='144' or i.place='145' or i.place='146' or i.place='147' or i.place='148' or i.place='149' or i.place='150' or i.place='151' or i.place='152' or i.place='153' or i.place='154' or i.place='155' or i.place='156' or i.place='157' or i.place='158' or i.place='159' or i.place='160' or i.place='161' or i.place='162' or i.place='163' or i.place='164' or i.place='165' or i.place='166')  ORDER BY i.lastupdated DESC LIMIT 18160, 10;

    есть ещё примерно такой же, но с JOINами

    wiz, 01 Сентября 2010

    Комментарии (10)
  5. SQL / Говнокод #4140

    −854

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    procedure dynamic_open(p_sql VARCHAR2, p_params t_string_table, p_cur OUT SYS_REFCURSOR) is
      begin
        case p_params.count
          when 0 then open p_cur for p_sql;
          when 1 then open p_cur for p_sql using p_params(1);
          when 2 then open p_cur for p_sql using p_params(1),
                                                 p_params(2);
          when 3 then open p_cur for p_sql using p_params(1),
                                                 p_params(2),
                                                 p_params(3);
          when 4 then open p_cur for p_sql using p_params(1),
                                                 p_params(2),
                                                 p_params(3),
                                                 p_params(4);
    /* ... */
          when 100 then open p_cur for p_sql using p_params(1),
                                                 p_params(2),
                                                 p_params(3),
                                                 p_params(4),
    /* ... */
                                                 p_params(100);
        end case;
      end dynamic_open;

    Динамический SQL - такой динамический. Уложились всего-то в 5050 строк.

    wecanstoptrain, 31 Августа 2010

    Комментарии (12)
  6. SQL / Говнокод #4021

    −866

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    ALTER PROCEDURE [dbo].[spSearch]                  
    	@Name nvarchar(256)
    AS                  
    BEGIN                  
     SET NOCOUNT ON                  
                       
    	DECLARE @qry VARCHAR(MAX)
    	DECLARE @Where VARCHAR(1000)                                
    	              
    	SET @Where = '1=1'                  
            
    	IF ISNULL(@Name,'') <> ''                  
    	BEGIN                  
    		SET @Where = @Where + ' and a.Name = ''' + @Name + ''''                  
    	END      
    
    SET @Where = @Where + ' and a.Active = 1'     
          
    	SET @qry ='    
    select a.* from t_Table1 a   
    	WHERE ' + @Where     + 
     ' order by a.Name'
    
    	EXECUTE (@qry)                  
                       
    END

    zheka, 18 Августа 2010

    Комментарии (5)
  7. SQL / Говнокод #3939

    −865

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    (
             (                      
                    ((
                            SELECT a.* FROM collections a
                            LEFT JOIN link_xml_collections lxa ON
    lxa.collection_id = a.id
                            WHERE lxa.xml_id IS NULL
                    ) UNION (
                            SELECT a.* FROM collections a
                            WHERE get_action_of_collection_last_xml(a.id, a.modified) = 3
                    )) INTERSECT (
                            SELECT a.* FROM collections a
                            LEFT JOIN link_collection_compositions AS lcc ON lcc.collection_id = a.id
                            LEFT JOIN compositions AS c ON c.id = lcc.composition_id
                            WHERE c.status = 0
                    )
           ) INTERSECT (
                    SELECT a.* FROM public.collections AS a
                    LEFT JOIN link_collection_compositions AS lcc ON lcc.collection_id = a.id
                    LEFT JOIN compositions AS c ON c.id = lcc.composition_id  WHERE c.owner_id = 6 AND a.modified >= '2010-08-10 00:00:00'
            )
    ) EXCEPT (
            SELECT a.* FROM collections a WHERE a.title_ua ILIKE 'TEST%'
    )

    Попросили пофиксить багу в функции, которая в зависимости от передаваемых параметров формировала sql запрос и возвращала данные. (Человек коротый это писал уволился.) Решил залогировать конечный sql запрос - получилось такое.

    antonfin, 11 Августа 2010

    Комментарии (2)
  8. SQL / Говнокод #3782

    −861

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    /* Ask Dian for changing this enormous DECODE */
          sprintf( ranges_sort_condition,
                   "DECODE(CONTRACT_ID, '%s', 1, 0) + "
                   "DECODE(ISS_INST, '%s', 2, 0) + "
                   "DECODE(ACQ_INST, '%s', 4, 0) + "
                   "DECODE(ISS_BIN, '%s', 8, 0) + "
                   "DECODE(TERM_TYPE, %d, 16, 0) + "
                   "DECODE(IS_PRIMARY_CARD, %d, 32, 0), "
                   "  0,  1,  64,  2,  32,  3,  16,  4,   4,  5,   8,  6,  96,  7,  80,  8,"
                   " 68,  9,  72, 10,  40, 11,  24, 12,  12, 13,   2, 14,  10, 15,  48, 16,"
                   " 36, 17, 104, 18,  88, 19,  76, 20,  66, 21,  74, 22, 112, 23, 100, 24,"
                   " 56, 25,  44, 26,  34, 27,  42, 28,  20, 29,  28, 30,  18, 31,   6, 32,"
                   " 26, 33,  14, 34,  52, 35,  60, 36, 124, 37,  50, 38,  38, 39,  22, 40,"
                   "118, 41, 122, 42, 110, 43,  94, 44, 126, 45,   1, 46,  65, 47,  97, 48,"
                   " 81, 49,   5, 50,  73, 51, 105, 52,  89, 53,  77, 54,  67, 55,  75, 56,"
                   "113, 57, 121, 58, 101, 59, 109, 60,  99, 61, 107, 62,  85, 63,  93, 64,"
                   " 83, 65,  71, 66,  91, 67,  79, 68, 117, 69, 125, 70, 115, 71, 103, 72,"
                   " 87, 73, 119, 74,  59, 75,  47, 76,  95, 77,  63, 78, 127, 79, 999",
                   "-1", DEFAULT_INST_ID, DEFAULT_INST_ID, "-1", TERM_TYPE_DEF, IS_PRIMARY_DEF );
    
          ora_sql_stmt_buff.len = sprintf( (char*)ora_sql_stmt_buff.arr, 
                                           "SELECT ACQ_INST, "
                                           "       ISS_INST, "
                                           "       ISS_BIN,  "
                                           "       TERM_TYPE, "
                                           "       MSGTYPE,  "
                                           "       TASK,     "
                                           "       REVERSAL, "
                                           "       CONTRACT_ID, "
                                           "       IS_PRIMARY_CARD, "
                                           "       TMPL_ID   "
                                           "  FROM T_TRANS_AUTH_ALG "
                                           " WHERE TRANS_TYPE = :ora_trans_type "
                                           " ORDER BY DECODE(%s)", ranges_sort_condition );

    Это хоть и на Си, но формирование запроса SQL, так что попадает сюда.
    Хоть бы комментарий какой написали, демоны!

    nil, 26 Июля 2010

    Комментарии (3)
  9. SQL / Говнокод #3739

    −864

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    -- Процедура предназначена для выделения часов из времени
    CREATE PROCEDURE _hour(
    	_date_time	DATETIME YEAR TO SECOND DEFAULT NULL
    )RETURNING INTEGER;
    
    DEFINE _str	CHAR(20);
    
    IF _date_time IS NULL THEN
    	LET _date_time = CURRENT;
    END IF;
    
    LET _str = _date_time;
    LET _str = SUBSTR(_str,12,2);
    
    RETURN _str;
    
    END PROCEDURE;

    СУБД infomix
    а ведь можно сделать так
    CAST(CURRENT AS DATETIME HOUR TO HOUR)
    или так
    EXTEND(CURRENT, HOUR TO HOUR)

    и таких перлов с вычислением дат тьма тьмущая

    Grizzly, 20 Июля 2010

    Комментарии (2)
  10. SQL / Говнокод #3723

    −866

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    sBigDad	    varchar2(200) := '';
    vaKids		ids_va	:= ids_va();
    vaDads		ids_va	:= ids_va();
    vaBigDads	ids_va	:= ids_va();
    vaGrandKids	ids_va	:= ids_va();
    vaTempKids	ids_va	:= ids_va();

    borka, 17 Июля 2010

    Комментарии (6)