Multiple SPARQL “INSERT WHERE” queries in a single request

让人想犯罪 __ 提交于 2019-12-05 04:28:55
Jeen Broekstra

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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!