gpt4 book ai didi

SQL CREATE LOGON - 不能使用@parameter 作为用户名

转载 作者:行者123 更新时间:2023-12-04 21:55:18 24 4
gpt4 key购买 nike

我是一名开发人员,我很擅长 SQL :) 请在这里帮助我。

我想创建自己的存储过程,在我的 SaaS 数据库中创建租户。为此,我需要为租户创建一个新的 SQL 登录名,然后将其添加到预定义的 SQL 角色中。

我已经为尝试创建登录名感到难过。这是我尝试过的...

CREATE PROCEDURE [MyScheme].[Tenants_InsertTenant]
@username nvarchar(2048),
@password nvarchar(2048)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

CREATE LOGIN @username WITH PASSWORD = @password
END

Msg 102, Level 15, State 1, Procedure Tenants_InsertTenant, Line 16 Incorrect syntax near '@username'.

Msg 319, Level 15, State 1, Procedure Tenants_InsertTenant, Line 16 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.



我意识到这应该很简单,但是当您的 SQL 新手和 SQL 管理器错误对我来说似乎很神秘时,最好寻求帮助:)

谢谢,
贾斯汀

最佳答案

显然 CREATE LOGIN 只接受文字。
您可以尝试将其包装在 exec 中并将其构建为字符串:

EXEC('CREATE LOGIN' + quotename(@username) + ' WITH PASSWORD = ' + quotename(@password, ''''))

编辑:添加引号名称以防止 sql 注入(inject)攻击

关于SQL CREATE LOGON - 不能使用@parameter 作为用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/837358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com