Question: **Background**

I have a multi-tenant scenario and a unique Sql Server project that will be deployed into multiple databases instances on the same server. There will be one db for each tenant, plus one "model" db.

The "model" database serves three purposes:

1. Force some "system" data to be always present in each tenant database
2. Serves as an access point for users with a special permission to edit system data (which will be punctually synced to all tenants)
3. When creating a new tenant, the database will be copied and attached with a new name representing the tenant

There are triggers that checks if the modified / deleted data within tenant db corresponds to "system" data inside the "model" db. If it does, an error is raised saying that system data cannot be altered.

**Issue**

So here's a part of the trigger that checks if deletion can be allowed:

```
IF DB_NAME() <> 'ModelTenant' AND EXISTS
    (
    SELECT
        [deleted].*
    FROM
        [deleted]
            INNER JOIN [---MODEL DB NAME??? ---].[MySchema].[MyTable] [ModelTable]
            ON  [deleted].[Guid] = [ModelTable].[Guid]
    )
BEGIN;
    THROW 50000, 'The DELETE operation on table MyTable cannot be performed. At least one targeted record is reserved by the system and cannot be removed.', 1
END

```

I can't seem to find what should take the place of --- MODEL DB NAME??? --- in the above code that would allow the project to compile properly. When refering to a completely different project I know what to do: use a reference to the project that's represented with an SQLCMD variable. But in this scenario the project reference is essentially the same project; only on a different database. I can't seem to be able to add a self-reference in this manner.

What can I do? Does SSDT offers some kind of support for such a scenario?

Answer: 
First off, the positional parameter is `$1` and not `#1`.

Secondly, your script doesn't really "count the number of time the word `Author` appears"; it looks literally for `<Author>`, including the angle brackets.

I assume you wanted word boundaries, as in `\<Author\>`.

`grep -r` just lists all matching lines, prepended by filenames. You want only the count, and sorted. To do this, you can do

```
grep -rwch 'Author'

```

* `-w` searches for word matches
* `-c` returns a match count per file
* `-h` suppresses writing the file name

And to sort the output, you pipe it to `sort`:

```
grep -rwch 'Author' | sort -nr

```

`-n` is for "numerical sort", and `-r` for "reverse", so the largest number is first.

Notice how this *still* only counts how many *lines* matched "Author"; if there is a line with five matches, it is counted only as one by `grep -c`.

To properly count every single occurrence, you could to this:

```
find . -type f -exec bash -c 'grep -wo "Author" {} | wc -l' \; | sort -nr

```

* `find . -type f` finds recursively all files.
* `-exec` executes a command for each file found. Because we use a pipe in that command, we have to spawn a subshell with `bash -c`.
* `grep -wo "Author" {} | wc -l` finds every match of `Author` and prints it on a separate line; `wc -l` then counts the lines.
* After this happened for all files, `sort -nr` again sorts the results.
ITYM `$1`, not `#1`

..........................................