Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 26th, 2007, 04:10 PM
Authorized User
 
Join Date: Nov 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default set password equal to variable

I'm scripting creating logins and want to set them all with the same default password. My thought is to declare a variable, then create the login with password equal to the variable. I get "incorrect syntax near '@PW'" using the following:
DECLARE @PW varchar(50)
SET @PW = 'password'

CREATE LOGIN loginname WITH PASSWORD = @PW,
    CHECK_POLICY=OFF,
    CHECK_EXPIRATION=OFF,
    DEFAULT_DATABASE=defaultdb
 
Old April 27th, 2007, 01:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

The code looks good, BTW which line does it err out? Can you try a simple CREATE LOGIN statement without the variable and see if that fails.

_________________________
- Vijay G
Strive for Perfection
 
Old April 27th, 2007, 11:32 AM
Authorized User
 
Join Date: Nov 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It returns error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '@PW'.

This works fine:
CREATE LOGIN loginname WITH PASSWORD = 'password',
    CHECK_POLICY=OFF,
    CHECK_EXPIRATION=OFF,
    DEFAULT_DATABASE=defaultdb
 
Old April 30th, 2007, 01:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Fine... I got the reason for it. DDL doesn't support parameterization. So you got to use dynamic SQL to overcome that.
Code:
Exec('CREATE LOGIN loginname WITH PASSWORD = ''' + @PW + ''',
 CHECK_POLICY=OFF, CHECK_EXPIRATION=OFF, DEFAULT_DATABASE=defaultdb')
 Cheers.

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
variable to equal part of string melkin Classic ASP Basics 28 May 17th, 2007 01:08 PM
Object variable or with block variable not set spacy ASP.NET 1.x and 2.0 Application Design 0 September 21st, 2004 12:19 AM
Set form value equal to databse fs22 Javascript How-To 17 July 16th, 2004 04:39 AM
How to "Set" a function equal to a DataReader DittoToo ASP.NET 1.0 and 1.1 Basics 1 July 1st, 2003 10:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.