My question is similar to what was asked on this thread Is it possible to combine those 2 SPARQL INSERT into one?
I want to have multiple INSERT WHERE statements in a query, but for different subjects. I will like to test a particular value ("testValueN") and if present would like to insert a new triple for that subject.
An example of it would be,
PREFIX Sensor: <http://example.com/Equipment.owl#>
{
INSERT {
?subject1 Sensor:test2 'newValue1' .
}
WHERE {
?subject1 Sensor:test1 'testValue1' .
}
};
{
INSERT {
?subject2 Sensor:test2 'newValue2' .
}
WHERE {
?subject2 Sensor:test1 'testValue2' .
}
};
I know the above query is wrong. I would like to know if something similar is possible in SPARQL.
Yes, this is possible. In fact, your example is almost completely fine, just lose the brackets around each insert:
PREFIX Sensor: <http://example.com/Equipment.owl#>
INSERT {
?subject1 Sensor:test2 'newValue1' .
}
WHERE {
?subject1 Sensor:test1 'testValue1' .
};
INSERT {
?subject2 Sensor:test2 'newValue2' .
}
WHERE {
?subject2 Sensor:test1 'testValue2' .
}
is a valid SPARQL update sequence.
I want to have multiple INSERT WHERE statements in a query, but for different subjects. I will like to test a particular value ("testValueN") and if present would like to insert a new triple for that subject.
You can do this using values to specify the oldValue/newValue pairs that you want, and it only requires a single insert. It also scales more nicely to new pairs, since you only have to add one line to the query.
PREFIX Sensor: <http://example.com/Equipment.owl#>
INSERT {
?subject Sensor:test2 ?newValue
}
WHERE {
values (?oldValue ?newValue) {
('testValue1' 'newValue1')
('testValue2' 'newValue2')
}
?subject Sensor:test1 ?oldValue
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With