我正在研究SQL Server 2008 R2 Enterprise,我一直在研究密码生成器,它会自动更改所提供用户名的SQL密码。我在另一个位置找到了脚本并对其进行了一些修改。我遇到的问题是,当我运行脚本时,它会出错。下面是代码,之后是错误。
set nocount on
declare @PASSWORD_MIN_LENGTH int
declare @PASSWORD_MAX_LENGTH int
declare @LOGIN SYSNAME
select @PASSWORD_MIN_LENGTH = 25
select @PASSWORD_MAX_LENGTH = 25
select @LOGIN = 'Test_User'
declare @PASSWORD_DATA table (PASSWORD_CHAR VARCHAR(1) )
declare @NUM table ( DIGIT int not null primary key clustered)
insert into @NUM (DIGIT)
select DIGIT = 0 union all select DIGIT = 1 union all
select DIGIT = 2 union all select DIGIT = 3 union all
select DIGIT = 4 union all select DIGIT = 5 union all
select DIGIT = 6 union all select DIGIT = 7 union all
select DIGIT = 8 union all select DIGIT = 9
order by 1
insert into @PASSWORD_DATA (PASSWORD_CHAR)
select
PASSWORD_CHAR =
substring(b.CHARACTERS,a.RAND_INT%b.MOD,1)
from
(select
aa.NUMBER,
RAND_INT = abs(convert(int, convert(varbinary(100), newid())))
from
(select
NUMBER = a.DIGIT+(b.DIGIT*10)
from
@NUM a
cross join
@NUM b) aa
) a
cross join
(select
MOD = len(bb.CHARACTERS)-1,
bb.CHARACTERS
from
(select
CHARACTERS = 'ABCDEFGHJKLMNPQURSUVWXYZ' +
'abcdefghjkmnpqursuvwxyz' +
'ABCDEFGHJKLMNPQURSUVWXYZ' +
'abcdefghjkmnpqursuvwxyz' +
'ABCDEFGHJKLMNPQURSUVWXYZ'+
'abcdefghjkmnpqursuvwxyz' +
'ABCDEFGHJKLMNPQURSUVWXYZ'+
'abcdefghjkmnpqursuvwxyz' +
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'@#$^*+=<>?'+
'@#$^*+=<>?'+
'@#$^*+=<>?'+
'@#$^*+=<>?') bb
) b
order by
newid()
--select * from @PASSWORD_DATA
declare @password varchar(100)
select @password = ''
select @password = @password+PASSWORD_CHAR
from @PASSWORD_DATA
select @password =
-- Random length from MIN to MAX characters
substring(@password,1,
@PASSWORD_MIN_LENGTH +
(abs(convert(int,convert(varbinary(100),newid()))))%(@PASSWORD_MAX_LENGTH -@PASSWORD_MIN_LENGTH+1))
print
'
PASSWORD = '+@password+'
exec master.dbo.sp_password
@old = NULL ,
@new = '''+@password+''',
@loginame = '''+isnull(@LOGIN,'''')+'''
'
exec master.dbo.sp_password
@old = NULL ,
@new = '''+@password+''',
@loginame = '''+isnull(@LOGIN,'''')+'''
exec master.dbo.sp_password
运行后,它会出现以下错误。
Msg 15007,Level 16,State 1,Procedure sp_password,Line 142
''isnull(@LOGIN,'')+''不是有效的登录信息,或者您没有权限。
我希望将其转换为存储过程,以便在被问到时自动生成和更改SQL密码。任何帮助,将不胜感激。 PS。我是盒子里的系统管理员。
答案 0 :(得分:1)
以这种方式尝试:
IF @LOGIN IS NOT NULL
exec master.dbo.sp_password
@old = NULL ,
@new = @password,
@loginame = @LOGIN
答案 1 :(得分:0)
这可能是您需要一些动态SQL的地方:
set nocount on
declare @PASSWORD_MIN_LENGTH int
declare @PASSWORD_MAX_LENGTH int
declare @LOGIN SYSNAME
select @PASSWORD_MIN_LENGTH = 25
select @PASSWORD_MAX_LENGTH = 25
select @LOGIN = 'Test_User'
declare @PASSWORD_DATA table (PASSWORD_CHAR VARCHAR(1) )
declare @NUM table ( DIGIT int not null primary key clustered)
----------------------------
DECLARE @TSQL NVARCHAR(4000);
-----------------------------
insert into @NUM (DIGIT)
select DIGIT = 0 union all select DIGIT = 1 union all
select DIGIT = 2 union all select DIGIT = 3 union all
select DIGIT = 4 union all select DIGIT = 5 union all
select DIGIT = 6 union all select DIGIT = 7 union all
select DIGIT = 8 union all select DIGIT = 9
order by 1
insert into @PASSWORD_DATA (PASSWORD_CHAR)
select
PASSWORD_CHAR =
substring(b.CHARACTERS,a.RAND_INT%b.MOD,1)
from
(
select
aa.NUMBER,
RAND_INT =
abs(convert(int,convert(varbinary(100),newid())))
from
(
select
NUMBER = a.DIGIT+(b.DIGIT*10)
from
@NUM a cross join @NUM b
) aa
) a
cross join
(
select
MOD = len(bb.CHARACTERS)-1,
bb.CHARACTERS
from
(
select
CHARACTERS =
'ABCDEFGHJKLMNPQURSUVWXYZ'+
'abcdefghjkmnpqursuvwxyz'+
'ABCDEFGHJKLMNPQURSUVWXYZ'+
'abcdefghjkmnpqursuvwxyz'+
'ABCDEFGHJKLMNPQURSUVWXYZ'+
'abcdefghjkmnpqursuvwxyz'+
'ABCDEFGHJKLMNPQURSUVWXYZ'+
'abcdefghjkmnpqursuvwxyz'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'23456789'+
'@#$^*+=<>?'+
'@#$^*+=<>?'+
'@#$^*+=<>?'+
'@#$^*+=<>?'
) bb
) b
order by
newid()
--select * from @PASSWORD_DATA
declare @password varchar(100)
select @password = ''
select @password = @password+PASSWORD_CHAR
from @PASSWORD_DATA
select @password =
-- Random length from MIN to MAX characters
substring(@password,1,
@PASSWORD_MIN_LENGTH +
(abs(convert(int,convert(varbinary(100),newid()))))%(@PASSWORD_MAX_LENGTH -@PASSWORD_MIN_LENGTH+1))
print
'
PASSWORD = '+@password+'
exec master.dbo.sp_password
@old = NULL ,
@new = '''+@password+''',
@loginame = '''+isnull(@LOGIN,'''')+'''
'
----------------------------------------------
SELECT @TSQL = '
exec master.dbo.sp_password
@old = NULL ,
@new = '''+@password+''',
@loginame = '''+isnull(@LOGIN,'''')+''''
EXECUTE sp_executesql @TSQL
----------------------------------------------