SqlCmd variable reference is not allowed in object names
Asked Answered
C

0

7

I'm creating a visual studio database project.

In one of the script, I want to achieve something like

CREATE USER [$(DatabaseName)\UserX]
    WITHOUT LOGIN
    WITH DEFAULT_SCHEMA = dbo

Which popups error

SQL70604: SqlCmd variable reference is not allowed in object names ($(DatabaseName)\UserX).

After a bit study, the closest solution I found was to create user by sp_executesql but it will lead schema compare feature invalid right?

I'm not quite familiar with database project, but I imagine there should be some better way to achieve this which I just need some direction.

Coauthor answered 9/5, 2018 at 4:7 Comment(8)
I'm more concerned that you are embedding database names into user names.Daukas
It's a design of our current system which I wondering why it made in that way too.Coauthor
The reason seems that we always give the server and SQL instance on it the same name. Therefore, it's actually MachineName\UserName which can associate to windows group directly.Coauthor
There may be other valid uses for this, e.g. an organisation that has AD domains like DEV, SIT, UAT and PRODUCTION, so you would have to do something like CREATE LOGIN [$(USERDOMAIN)\MyUser] ... on each domain.Glycol
@Glycol the bounty you created is for a completely different question and has nothing to do with variables. Put the groups into database roles and assign permissions to roles, not domain groups. The only thing you'll have to change is the role assignments. That's no different than asking how to nest windows groups in generalGregoire
@PanagiotisKanavos I'm not sure what you are suggesting. SQL Server needs a domain to be specified next to the username, so there seems to be no way of mapping Windows AD Groups like "DEV\MyGroup, SIT\MyGroup, ..." inside of a MS VS database project like CREATE LOGIN [$(USERDOMAIN)\MyGroup]....Can you clarify what setup you are suggesting?Glycol
I said use roles, not users. You're asking a completely different question than this one. Post a new question instead of adding an unrelated bounty. Better yet, look at dba.stackexchange.com although I expect you'll get the same answers. For example, How do I assign an entire Active Directory group security access in SQL Server 2008?Gregoire
@PanagiotisKanavos see my new question here: dba.stackexchange.com/q/334722/143235Glycol

© 2022 - 2024 — McMap. All rights reserved.