Can I use a variable of type varchar with possible dots in SQL Server database?
<p>I'm creating a procedure to add users logins to a specific database. I call this procedure inside a trigger when a user is inserted in my database (I want to have different logins for each user in my site and in the access to the database). </p>

<p>Here's my trigger:</p>

<pre><code>ALTER TRIGGER [dbo].[sys_users_insert]
ON [dbo].[sys_users]
AFTER INSERT
AS
BEGIN
    DECLARE @USERNAME varchar(MAX)

    SELECT @USERNAME = INSERTED.username    
    FROM INSERTED

    EXEC CreateUser @USERNAME
END
</code></pre>

<p>The <code>CreateUser</code> is my procedure to create the logins in the database. The procedure is working. When I call it from a query editor:</p>

<pre><code>EXEC CreateUser 'patricia.santos'
</code></pre>

<p>The user is created with success. But when I insert a user in the table and trigger runs the user isn't created nor is inserted in the table. And I found out that if I insert a user without dots in the username everything goes ok. So I believe is something with the dot but I would like to know if there's some workaround to accept usernames with dots.</p>

<p>UPDATE</p>

<p>Here's my procedure for creating logins in my database:</p>

<pre><code>USE mydatabase
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CreateUser]
    @USERNAME AS VARCHAR(MAX) = ''
AS
BEGIN

declare @sql nvarchar(MAX)

If NOT EXISTS(select loginname from master.dbo.syslogins where name = @USERNAME)
BEGIN

    set @sql = 'CREATE LOGIN ' + @USERNAME + 
            ' WITH 
                PASSWORD = ''something'',
                CHECK_POLICY = OFF'

    exec(@sql)

    set @sql = 'ALTER SERVER ROLE sysadmin ADD MEMBER ' + @username

    exec(@sql)

    IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = @USERNAME)
    BEGIN

        -- Creates a database user for the login created above.
        set @sql = 'CREATE USER [' + @USERNAME + '] FOR LOGIN [' + @USERNAME + ']'

        exec(@sql)

        EXEC sp_addrolemember 'db_owner', @USERNAME
    END
END

END
</code></pre>

<p>Thank you :)</p>