# Importing results from query

**URL:** <https://community.stardog.com/t/importing-results-from-query/1468>\
**Category:** Support\
**Created:** [February 6, 2019, 5:12pm UTC](https://community.stardog.com/t/importing-results-from-query/1468 "2019-02-06T17:12:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![doggyfood](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/doggyfood/32/106_2.png) [@doggyfood](https://community.stardog.com/u/doggyfood)\
**Post date:** [February 6, 2019, 5:12pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/1 "2019-02-06T17:12:28Z")

</div>

I would like to use a script to export part of the data from a database and then import it in another database.

I can use `stardog query` to export the desired partition of my data. However, the result formats (such as CSV, TEXT, SPARQL, etc) do not seem to be accepteb by `stardog data add`.

How can I achieve this?

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [February 6, 2019, 5:15pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/2 "2019-02-06T17:15:50Z")

</div>

You can use a `CONSTRUCT` query, then your results will be in RDF so you can add those to a database.

Or you can get fancy and use an `INSERT ... WHERE ...` query against the target database while fetching data from your source database using `SERVICE`.

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![doggyfood](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/doggyfood/32/106_2.png) [@doggyfood](https://community.stardog.com/u/doggyfood)\
**Post date:** [February 6, 2019, 5:19pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/3 "2019-02-06T17:19:30Z")

</div>

Thanks, I have not thought of CONSTRUCT, it might work, I will give it a try.

The second option is not valid since I need to move it to a place where no direct network connection is possible.

---

<div class="post-metadata">

**Author:** ![doggyfood](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/doggyfood/32/106_2.png) [@doggyfood](https://community.stardog.com/u/doggyfood)\
**Post date:** [February 6, 2019, 5:54pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/4 "2019-02-06T17:54:05Z")

</div>

CONSTRUCT seems to work, although it is getting more than desired. Is it possible to restrict the results only to the default graph? (ie. not querying any named graphs) I have the option "query all graphs" activated in the database and should not remove this even temporarily. I just need for this specific query to be able to say "only query the default graph, not any named graph".

I cannot find this in the manual.

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [February 6, 2019, 5:59pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/5 "2019-02-06T17:59:26Z")

</div>

Sure, you can use `FROM <tag:stardog:api:context:default>` in your query to limit the results to the default graph only.

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![doggyfood](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/doggyfood/32/106_2.png) [@doggyfood](https://community.stardog.com/u/doggyfood)\
**Post date:** [February 6, 2019, 6:02pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/6 "2019-02-06T18:02:48Z")

</div>

Great, many thanks!

I suppose there is no "standard" way to do it in SPARQL. Before, I tried to get the name of the default graph `(select distinct ?g where { graph ?g {?s ?p ?o} })` and then use it in the FROM clause (ie. `FROM <def_name_url>`) but it did not work. It only showed my preffixes.

---

<div class="post-metadata">

**Author:** ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)\
**Post date:** [February 6, 2019, 6:34pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/7 "2019-02-06T18:34:52Z")

</div>

Yeah, managing query datasets is a bit odd part of the SPARQL spec. The spec intentionally does not define the query dataset in case the query doesn't use either FROM or FROM NAMED. In that case the dataset is determined by the vendor, and Stardog uses either the default graph or all graphs depending on `query.all.graphs` for patterns outside of `graph g { ... }`.

If you don't use `query.all.graphs` you can still run SPARQL queries without any Stardog-specific URIs but you'd need to manually UNION scans in the default and named scopes, e.g. `{ ?s ?p ?o } UNION { graph ?g { ?s ?p ?o } }` to get all data. But if you do use `query.all.graphs=true`, then you need `FROM <tag:stardog:api:context:default>` to access the default graph because it really does not have any name in the SPARQL spec.

Cheers,  
Pavel

---

<div class="post-metadata">

**Author:** ![doggyfood](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/doggyfood/32/106_2.png) [@doggyfood](https://community.stardog.com/u/doggyfood)\
**Post date:** [February 6, 2019, 11:12pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/8 "2019-02-06T23:12:26Z")

</div>

Great answer. Many thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [February 20, 2019, 11:12pm UTC](https://community.stardog.com/t/importing-results-from-query/1468/9 "2019-02-20T23:12:31Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
