Announcement

Collapse
No announcement yet.

Update

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Update

    Hab ein kleines Problem und komme nicht so recht weiter...

    Muss eine Spalte updaten, komme aber immer nur zu Fehlern..

    Habe folgendes vor:

    Ich möchte gern, dass wenn in der tabelle "specials" in der spalte "status" der Wert "1" gesetzt ist, dass in der tabelle "products" die Spalte "product_template" auf 100 geupdatet wird.

    Kann mir da jemand helfen?

    Hatte folgendes probiert:

    Code:
    UPDATE products
    SET options_template =100
    SELECT * FROM specials WHERE status='1'

  • #2
    Vielleicht so:
    Code:
    UPDATE products
    SET options_template =100
    WHERE EXISTS (SELECT * FROM specials WHERE status='1')

    Comment


    • #3
      Das sieht ja schonmal viel besser aus! Vielen Dank!!!

      Jetzt habe ich das Problem, dass der Status aus der Tabelle "specials" nur bei bestimmten "Produkt_ids" gesetzt ist. Und möchte gern, dass auch nur die entsprechende product_id in der tabelle products geupdatet werden. Ist das möglich?

      Ich hoffe meine Frage ist verständlich?!

      Comment


      • #4
        Hallo,

        das folgende Beispiel zeigt eine der möglichen Implementierungen für die gestellte Aufgabe:
        [highlight=SQL]
        USE tempdb
        GO
        CREATE TABLE Products
        (
        p_id INT NOT NULL IDENTITY PRIMARY KEY,
        options_template INT NOT NULL DEFAULT 0
        )
        GO
        INSERT INTO dbo.Products DEFAULT VALUES;
        INSERT INTO dbo.Products DEFAULT VALUES;
        INSERT INTO dbo.Products DEFAULT VALUES;
        GO

        CREATE TABLE Specials
        (
        s_id INT NOT NULL IDENTITY PRIMARY KEY,
        p_id INT NOT NULL REFERENCES Products(p_id),
        status INT NOT NULL
        )
        GO
        INSERT INTO dbo.Specials (p_id, status) VALUES (1,1);
        INSERT INTO dbo.Specials (p_id, status) VALUES (2,2);
        INSERT INTO dbo.Specials (p_id, status) VALUES (3,1);
        GO

        UPDATE
        dbo.Products
        SET
        options_template = 100
        FROM
        dbo.Specials AS s
        WHERE
        (Products.p_id = s.p_id) AND (s.status = 1)
        GO

        -- Ergebnis prüfen
        SELECT * FROM dbo.Products
        [/highlight]

        Ergebnis:

        Code:
        p_id        options_template
        ----------- ----------------
        1           100
        2           0
        3           100

        Comment

        Working...
        X