---
title: "Trigger med If-sats"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/176909-trigger-med-if-sats"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "asp"
published: "2009-02-13T18:46:56.000Z"
updated: "2009-02-15T15:24:27.000Z"
replies: 5
views: 475
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/176909-trigger-med-if-sats"
---

# Trigger med If-sats

## #1 — asp, 2009-02-13T18:46Z

Hej igen,

```
create trigger TeamCheck
before insert on Teams
referencing new as newRow
for each row
begin
	if ((select sum(scored) from Players where teamId = :newRow.playerId) > 0) then
		-- Actions...
	end if;
end;
```

Ger felmeddelandet:

**Warning: Trigger created with compilation errors.**

Det är select-satsen som krånglar till det, om jag i if-satsen skriver 0 \> 0 så skapas triggern utan "compilation errors".

Kan tilläggas att jag använder Oracle.

Permalänk: https://www.webforum.nu/p/176909

## #2 — LarsG, 2009-02-13T20:47Z

Vad får du ut om du skriver

```
sql>show errors trigger TeamCheck;
```

?

Permalänk: https://www.webforum.nu/p/2168901

## #3 — asp, 2009-02-14T01:35Z

```
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null others <an identif ier> <a double-quoted delimited-identifier> <a bind variable> avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a strin g literal with character set specification> <a number> <a sing le-quoted SQL string> pipe <en sträng inom alternativa citatte cken med teckenuppsättningsangivelse> <en SQL-sträng
```

samt

```
PLS-00103: Encountered the symbol ")" when expecting one of the f ollowing: . , @ ; for <an identifier> <a double-quoted del imited-identifier> group having intersect minus order partitio n start subpartition union where connect SAMPLE_
```

Permalänk: https://www.webforum.nu/p/2168921

## #4 — LarsG, 2009-02-14T16:56Z

Jaha, det verkar vara någon begränsning i Oracle. Ett alternativ är att du tilldelar värdet av select-frågan till en variabel och använder den i din if-sats.

```

declare a number;
select count(*) into a from ...

if  a > 0 then
```

Permalänk: https://www.webforum.nu/p/2168964

## #5 — asp, 2009-02-14T17:23Z

Funkar inte heller, jag deklarerar och tilldelar variablen efter begin och innan if-satsen.

```
PLS-00103: Encountered the symbol "IF" when expecting one of the following: . ( * @ % & - + ; / at for mod remainder rem <a n exponent (**)> and or group having intersect minus order sta rt union where connect || indicator multiset
```

```
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: begin function package pragma procedure subtyp e type use <an identifier> <a double-quoted delimited-identifi er> form current cursor The symbol "begin" was substituted for "SELECT" to continue.
```

Permalänk: https://www.webforum.nu/p/2168965

## #6 — asp, 2009-02-15T15:24Z

Nu fick jag det att fungera. Jag misstolkade nog var jag skulle placera declare-satserna.

```
DECLARE
	...
BEGIN
	SELECT...
	IF (...) THEN
	
	END IF
END;
```

Permalänk: https://www.webforum.nu/p/2169078

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/176909-trigger-med-if-sats
