2

I have two SPARQL updates.First one:

INSERT 
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH  <[http://example/bookStore]>
   { ?book dc:date ?date .
     FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
     ?book ?p ?v
} }

Second:

INSERT 
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH  <[http://example/bookStore3]>
   { ?book dc:date ?date .
     FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
     ?book ?p ?v
} }

Can i combine them with the UNION operator? And if yes, is it an equivalent result? Is it possible to use UNION in SPARQL updates such as in "Select"?

Cheryl
  • 245
  • 1
  • 11

2 Answers2

2

AndyS's answer is correct; you can combine them, and the description of UNION is found in section 7 Matching Alternatives of the SPARQL specification. The combined query would be:

INSERT { 
  GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{ 
  {
    GRAPH  <[http://example/bookStore]> {
      ?book dc:date ?date .
      FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
      ?book ?p ?v
    }
  }
  UNION
  {
    GRAPH <[http://example/bookStore3]> {
      ?book dc:date ?date .
      FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
      ?book ?p ?v
    }
  }
}

In this particular case where the patterns are so similar, you could also just abstract out the differing parts with VALUES:

INSERT { 
  GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{ 
  values (?graph ?startDate) { 
    (<[http://example/bookStore]> "1970-01-01T00:00:00-02:00"^^xsd:dateTime)
    (<[http://example/bookStore3]> "1980-01-01T00:00:00-02:00"^^xsd:dateTime)
  }
  GRAPH ?graph {
    ?book dc:date ?date .
    FILTER ( ?date > ?startDate )
    ?book ?p ?v
  }
}
Community
  • 1
  • 1
Joshua Taylor
  • 80,876
  • 9
  • 135
  • 306
1

The WHERE clause is the same as SPARQL Query - you can use UNION.

AndyS
  • 14,989
  • 15
  • 20
  • Yes, but for example in SQL update there is also the same WHERE clause as in SQL but we cannot use the UNION operator – Cheryl Mar 13 '13 at 17:11
  • @Cheryl SPARQL has some similarities with SQL, in that they both provide a way of querying some data but, aside from that, they are very different. Regardless of whether SQL allows you to combine WHERE and UNION, in SPARQL you can combine them, e.g., `select * where { { ?animal a :Cat } UNION { ?animal a :Dog } }` to get all the `?x`s that are `:Cat`s and all the `?x`'s that are `:Dog`s. – Joshua Taylor Aug 26 '13 at 18:47