- 1
- 2
- 3
- 4
select
if(t.town_name is null, t.id, t.town_name) as 'town',
...
from towns t;
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
−858
select
if(t.town_name is null, t.id, t.town_name) as 'town',
...
from towns t;
в sql внезапно тоже есть приведение типов и можно получить вот такое назвение города
1101000010010010110100001011101111010000 1011000011010000101101001101000010111000 1101000010110010110100001011111011010001 1000000111010001100000101101000010111110 1101000010111010
−856
ALTER PROCEDURE Estimate @Goods_ID int ,@Workshop_ID int AS
SELECT 9, null, null, 'Арматура',null, null , null, null
Union
SELECT 8, dbo.Charges.FK_TypeCh, dbo.Charges.ID, ' '+dbo.Charges.NAME, dbo.Charge_Price.KBO,
dbo.Charges.Cost, Round(dbo.Charges.Cost*dbo.Charge_Price.KBO,1),
Round( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume,1)
FROM dbo.Charge_Price INNER JOIN
dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID
WHERE (dbo.Charge_Price.FK_Workshop = 6) AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
Union
SELECT 7, null, null,' Всего по арматуре', null, null,Round( SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO) ,1)as Сумма,
Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume),1)
FROM dbo.Charge_Price INNER JOIN
dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID
WHERE (dbo.Charge_Price.FK_Workshop = 6) AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
Union
/*
Ещё мурнадцать запросов с объединением
*/
UNION
SELECT -3, null, null,' Полная себестоимость', null, null, Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO)*1.05,1) as Сумма,
Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume)*1.05,1)
FROM dbo.Charge_Price INNER JOIN
dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID
WHERE ( (dbo.Charge_Price.FK_Workshop = @Workshop_ID) OR (dbo.Charge_Price.FK_Workshop = 6) OR
(dbo.Charge_Price.FK_Workshop = 7) )AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
UNION
SELECT -4, null, null,' Плановые накопления', null, null, Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO)*0.1575,1) as Сумма,
Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume)*0,1575,1)
FROM dbo.Charge_Price INNER JOIN
dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID
WHERE ( (dbo.Charge_Price.FK_Workshop = @Workshop_ID) OR (dbo.Charge_Price.FK_Workshop = 6) OR
(dbo.Charge_Price.FK_Workshop = 7) )AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
UNION
SELECT -5, null, null,' Цена оптовая', null, null, Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO)*1.2075,1) as Сумма,
Round(SUM( dbo.Charges.Cost*dbo.Charge_Price.KBO/ dbo.Goods.volume)*1,2075,1)
FROM dbo.Charge_Price INNER JOIN
dbo.Charges ON dbo.Charge_Price.FK_Charge = dbo.Charges.ID INNER JOIN
dbo.Workshop ON dbo.Charge_Price.FK_Workshop = dbo.Workshop.ID INNER JOIN
dbo.Goods ON dbo.Charge_Price.FK_Goods= dbo.Goods.ID
WHERE ( (dbo.Charge_Price.FK_Workshop = @Workshop_ID) OR (dbo.Charge_Price.FK_Workshop = 6) OR
(dbo.Charge_Price.FK_Workshop = 7) )AND (dbo.Charge_Price.FK_Goods = @Goods_ID)
Order by 1 desc, 2, 3
Формирование какой то сметы в стле 1С
−855
SELECT h.*,
if(h.gender_id != 0, (SELECT g.title FROM p_cm5_horses_gender g WHERE h.gender_id = g.id AND h.lang_id = g.lang_id),"") AS gender,
if(h.colour_id != 0, (SELECT c.title FROM p_cm5_horses_colour c WHERE h.colour_id = c.id AND h.lang_id = c.lang_id),"") AS colour,
if(h.color_group != 0, (SELECT cg.title FROM p_cm5_horses_colour_group cg WHERE h.color_group = cg.id AND h.lang_id = cg.lang_id),"") AS color_group_name,
if(h.temper_id != 0, (SELECT t.title FROM p_cm5_horses_temper t WHERE h.temper_id = t.id AND h.lang_id = t.lang_id),"") AS temper,
if(h.grading_id != 0, (SELECT t.title FROM p_cm5_horses_grading t WHERE h.temper_id = t.id AND h.lang_id = t.lang_id),"") AS grading,
if(h.breed_id != 0, (SELECT b.title FROM p_cm5_horses_studbook b WHERE h.breed_id = b.id AND h.lang_id = b.lang_id),"") AS breed,
if(h.training_first != 0, (SELECT t.training_name FROM p_cm5_horses_training t WHERE h.training_first = t.id AND h.lang_id = t.lang_id),"") AS training_first_name,
if(h.level_first != 0, (SELECT t.level_name FROM p_cm5_horses_training_level t WHERE h.level_first = t.id AND h.lang_id = t.lang_id),"") AS level_first_name,
if(h.training_second != 0, (SELECT t.training_name FROM p_cm5_horses_training t WHERE h.training_second = t.id AND h.lang_id = t.lang_id),"") AS training_second_name,
if(h.level_second != 0, (SELECT t.level_name FROM p_cm5_horses_training_level t WHERE h.level_second = t.id AND h.lang_id = t.lang_id),"") AS level_second_name,
if(h.price_id != 0, (SELECT p.title FROM p_cm5_horses_price p WHERE h.price_id = p.id AND h.lang_id = p.lang_id),"") AS price,
if(h.price_id != 0, (SELECT p.title FROM p_cm5_horses_price p WHERE h.price_id = p.id AND p.lang_id = 2),"") AS price2
FROM p_cm5_horses h
WHERE h.enabled = 1 AND h.lang_id = 1 AND h.id = 219
копался в одном проекте который нам отдали на аутсорс, нашел своего рода LEFT JOIN-ины
−855
ALTER TRIGGER Snag ON [dbo].[Registration]
FOR INSERT, UPDATE, DELETE
AS
begin
If not Exists (SELECT *
FROM XXXXXX
WHERE (DAY([DAT]) =DAY(GetDate())) )
While 1=1 begin RAISERROR ('Trial expired ', 18, 1) end
end
trial period control :)
−864
SELECT COUNT(*) FROM apteka.cache_offers WHERE ORG_ID = '21963' AND `subject_id` IN (35,30052,20262,20263,20001,30488,30776,22099,22044,21982,21938,21823,
(еще триста чисел),
26313,26358,26478,23381,27430,26367,26418,23433,23404,23372,23289,23321,23285,23315,23282,23406,23287,
26356,23429,23263,23349,23399,23356,23291,23346,23351,23250,23198,23194,23319,23238,23296,23259,23167,23268,23130,23052,23119,22430,23146,22315,23069,23083,23
127,23189,23023,23014,23059,22988,23020,23007,22989,22949,22937,22939,22941,22979,22786,22842,22849,22758,22913,22919,22833,22718,22753,22779,22724,22857,2276
8,22761,22733,22823,22759,22764,22658,22681,22674,22706,22599,22698,22667,22627,22776,22654,22655,22601,22619,22598,22694,22696,22603,22541,22570,22469,22534,
22515,22676,22516,22477,22491,22677,22513,22421,22537,22451,22507,22482,22440,22353,22523,22422,22510,22397,22473,22348,22383,22424,22333,22380,22359,22508,22
486,22456,22330);
Стандартный запрос. При формировании страницы конструкция встречается многократно (>10 раз)
−162
USE [VOLTAREN]
GO
/****** Object: UserDefinedFunction [dbo].[myReplace] Script Date: 04/05/2011 12:11:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[myReplace](@NAME varchar(255))
RETURNS varchar(255)
BEGIN
set @NAME = (
UPPER(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
replace(
@NAME,'*','')
,'табл','таб')
,'~','')
,'`','')
,'<','')
,'>','')
,'}','')
,'{','')
,']','')
,'[','')
,'=','')
,'_','')
,'#','')
,'$','')
,'?','')
,'^','')
,'&','')
,'@','')
,'''','')
,'"','')
,':','')
,'%','')
,'-','')
,'+','')
,')','')
,'(','')
,'N','')
,'№','')
,'|','')
,'\','')
,'/','')
,'!','')
,';','')
,',','')
,'.','')
,' ','')));
return(
replace(
@NAME,
'покробол','по')
)
END;
функция удаления не нужных символов из строки
−858
create or replace function t8() returns integer as $$
declare
rec record;
begin
for rec in select * from purchase loop
update purchase set amount=(
select book.cost*purchase.quantity*(1-customer.discount/100.0)
from purchase, book, customer
where purchase.id=rec.id and
book.id=purchase.book and
customer.id=purchase.customer
)
where purchase.id=rec.id;
end loop;
return 1;
end;
$$ language plpgsql;
select t8();
прочно засевшие в голове алгоритмические языки
−861
if @new_dg_sor_code is null and @dg_sor_code<>7
and
(
(
(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey in (1,1158,1166) and dl_control=0)=
(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey in (1,1158,1166))
)
and (
(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=3 and dl_control=0)=
(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=3)
)
and (
(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=5 and dl_control=0)=
(select count(*) from tbl_dogovorlist WITH (nolock) where dl_dgcod=@dg_code and dl_svkey=5)
)
)
Видимо зарплата этого парня зависит от количества запросов к tbl_dogovorlist.
А иначе зачем ему 6 запросов вместо одного?
−862
BEGIN
DECLARE @id_user int;
IF (EXISTS(SELECT ut.id_user FROM project_userTable ut WHERE ut.userStr=@UserName OR ut.email = @email))
begin
SET @errors='Такое имя или пароль уже существуют';
RETURN;
end
INSERT INTO project_userTable (userStr,passwordStr,email,profileLink,id_user_type,activate_ticket)
VALUES (@UserName,@password, @email, @profileLink,2,@activate_ticket)
IF (@@ROWCOUNT!=1)
begin
SET @errors='Ошибка регистрации';
RETURN
end
SET @id_user=@@IDENTITY;
INSERT INTO project_keyTable ([datetime],id_user, first_key ,second_key )
VALUES (GETDATE(),@id_user,@firstKey,@secodKey)
IF (@@ROWCOUNT=1)
begin
SET @errors='Done';
RETURN
end
SET @errors='А воттеперь реально косяк.';
END
Реально косяк. Кстати, а что читать?
−862
SET @SQL = '
SELECT
@diff1 = @diff1 + ''CASE WHEN '' + Field_Name + ''=@'' + Field_Name +
'' OR ('' + Field_Name + '' IS null AND @'' + Field_Name + '' IS NULL) THEN ''''''''
ELSE '''''' + Field_Name + ''="''''+COALESCE(CAST('' + Field_Name + '' AS varchar(max)), ''''NULL'''')
+ ''''", '''' END +'',
@diff2 = @diff2 + ''CASE WHEN '' + Field_Name + ''=@'' + Field_Name +
'' OR ('' + Field_Name + '' IS Null AND @'' + Field_Name + '' IS NULL) THEN ''''''''
ELSE '''''' + Field_Name + ''="''''+COALESCE(CAST(@'' + Field_Name + '' AS varchar(max)), ''''NULL'''')
+ ''''", '''' END +'',
@declare = @declare + ''DECLARE @'' + Field_Name + '' '' + Field_Type + '';'',
@set = @set + ''SET @'' + Field_Name + ''='' + Field_Value + '';''
FROM (' + @pre + ') AS t'
Моё. Кусочек небольшой хранимки в 700 строк.
Формирование SQL-a, который формирует SQL, который, в свою очередь, формирует строку для записи изменений таблицы в лог.