question

Gordy avatar image
Gordy asked

How to specify a group on the local machine using CREATE LOGIN

I wish to a create login for a local group on a server without actually needing the current server name so that I can use the same create statement on multiple servers?

i.e Local Machine is called Kate, allocating a local machine windows group called BOB

Normally I would create as follows in T-SQL

CREATE LOGIN [Kate\BOB] FROM WINDOWS

BUT - On the machine called Rubarb, the same statement won't work. Is there a generic way to specify the local machine to specify a group so it the same statement will work on two different machines?

BTW - I don't happen to have a network group for this and I wish I could easily get one created so just don't go there..

sql-server-2005t-sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Magnus Ahlkvist avatar image
Magnus Ahlkvist answered

You can do it with dynamic SQL (with all the disadvantages that brings)

DECLARE @sql nvarchar(300)
SET @sql = 'CREATE LOGIN [' + @@SERVERNAME + '\BOB] FROM WINDOWS'
EXEC sp_executeSQL @sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image
Scot Hauder answered

EXEC('CREATE LOGIN [' + @@SERVERNAME +'\BOB] FROM WINDOWS')

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Scot Hauder avatar image Scot Hauder commented ·
got me by 49 seconds
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.