# Using a variable db name in a RawSQL function

**URL:** <https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876>\
**Category:** Using the ORM\
**Created:** [August 8, 2023, 8:23am UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876 "2023-08-08T08:23:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyzozyme](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lyzozyme/32/15225_2.png) [@lyzozyme](https://forum.djangoproject.com/u/lyzozyme)\
**Post date:** [August 8, 2023, 8:23am UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876/1 "2023-08-08T08:23:14Z")

</div>

Hi,

For the purpose of my application, I would like to annotate a queryset with a value I have to fetch from an other database. To do that, I tried the following code :

```auto
def get_queryset(self):
  return self.queryset.annotate(
      value=RawSQL(
          """
          DECLARE @database NVARCHAR(MAX) = %s;
          DECLARE @sql NVARCHAR(MAX) = N'
              SELECT c.value
              FROM [' + @database + N'].[dbo].[table] t
              WHERE t.vendor = vendor_ref';
          EXEC sp_executesql @sql;
          """,
          (settings.DATABASE),
      ),
  )

```

In this snippet, _vendor\_ref_ is a field of the Django model I am currently using and _settings.DATABASE_ is the name of the database I want to use for this query.

But the problem is I am getting a 404 - Not found error when running this code.

When I try to put the db name directly in the query without using the variable it works fine. Have you any idea why this is not working?

Thank you in advance!  
Lyzo

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 8, 2023, 11:41am UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876/2 "2023-08-08T11:41:15Z")

</div>

How are you getting a 404 as a result of this? (Or is that just your way of saying that the database is throwing a “Not Found” error regarding the database?)

What’s the purpose of the leading plus sign in the string in the brackets in the `FROM` statement?

Which database engine is being used here?

---

<div class="post-metadata">

**Author:** ![lyzozyme](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lyzozyme/32/15225_2.png) [@lyzozyme](https://forum.djangoproject.com/u/lyzozyme)\
**Post date:** [August 8, 2023, 12:28pm UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876/3 "2023-08-08T12:28:59Z")

</div>

404 is the error Django is throwing in response to my request but it has to be linked to my SQL query.

To be honest, I didn’t ask myself why the variable is included in the query like that. But I know that it is working fine when I am doing it this way using the django connection.cursor().

I am using a MySQL database.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 8, 2023, 12:31pm UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876/4 "2023-08-08T12:31:57Z")

</div>

> [@lyzozyme](#):
>
> but it has to be linked to my SQL query.

It may or may not. There’s insufficient evidence at this point to support that assertion.

Please post the url being requested that is generating the 404 and the view related to that url.

---

<div class="post-metadata">

**Author:** ![lyzozyme](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lyzozyme/32/15225_2.png) [@lyzozyme](https://forum.djangoproject.com/u/lyzozyme)\
**Post date:** [August 8, 2023, 12:38pm UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876/5 "2023-08-08T12:38:28Z")

</div>

I am requesting this url: [http://127.0.0.1:8003/my-api/MyModel/object\_reference/](http://127.0.0.1:8003/my-api/MyModel/object_reference/)

And here is the view:

```auto
class MyViewSet(ModelViewSet):
    model = MyModel
    queryset = MyModel.objects.all()

    lookup_field = "reference"

    def get_queryset(self):
      return self.queryset.annotate(
          value=RawSQL(
              """
              DECLARE @database NVARCHAR(MAX) = %s;
              DECLARE @sql NVARCHAR(MAX) = N'
                  SELECT c.value
                  FROM [' + @database + N'].[dbo].[table] t
                  WHERE t.vendor = vendor_ref';
              EXEC sp_executesql @sql;
              """,
              (settings.DATABASE),
          ),
      )

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 8, 2023, 12:52pm UTC](https://forum.djangoproject.com/t/using-a-variable-db-name-in-a-rawsql-function/22876/6 "2023-08-08T12:52:31Z")

</div>

Ahh, I see that DRF is involved here. Hopefully someone familiar with DRF on the forum can jump in here.
