I need to create a user with dynamic names with a variable name
example:
The following code gives a syntax error.
Create Login @User_name WITH PASSWORD @password; USE database;
and I need to assign a role to the created user.
You cannot use variables for object names. You can fool
exec sp_addlogin @User_name, @password;
Either this, or build dynamic SQL, but be sure to use QUOTENAME to prevent SQL injection.
You cannot use create login with variables. You must create the statement dynamically or use sp_addlogin. According to http://msdn.microsoft.com/en-us/library/ms173768.aspx sp_addlogin is deprecated.
declare @UN sysname declare @PW sysname declare @S nvarchar(100) set @UN = 'UserName' set @PW = 'Password' set @S = 'CREATE LOGIN ' + quotename(@UN) + ' WITH PASSWORD = ' + quotename(@PW, '''') exec (@S)
Source: https://habr.com/ru/post/1339631/More articles:Using git-svn to ignore file only in svn repository? - gitModules between multiple versions of Python Linux - pythonCalling Unmanaged C ++ Code from C # - c ++How to assign a host name to a web server? - webservermaven release: don't plan to deploy projects with release version - maven-release-pluginSQL CREATE LOGON - cannot use @parameter as username - sqlFailed to check date in Spring MVC - dateBuild tool for iOS and Mac projects - iosPrint Sigma character in Java - javaMultiple columns are specified in an aggregate expression containing an external link - sql-server-2005All Articles