我有以下查询:

DECLARE @userrole as VARCHAR(100)

SELECT @userrole = UserRole
FROM UserMenuRights
WHERE idAdmin = 36

Execute('Select idRole,RoleName from UserRoles WHERE idRole IN ('+@userrole+')')

我收到错误消息:



我已经在第一行中检查了DECLARE @userrole as VARCHAR(100)似乎没有语法问题。

还检查了最后一行:
Execute('Select idRole,RoleName from UserRoles WHERE idRole IN ('+@userrole+')')

也似乎正确的字符串附件。

我在哪里弄错了?

请帮我。

最佳答案

试试这个,您需要添加单引号

DECLARE @userrole as VARCHAR(100)
SELECT @userrole =UserRole FROM UserMenuRights WHERE idAdmin =36
Execute('Select idRole,RoleName from UserRoles WHERE idRole IN ('''+@userrole+''')')

或者你像这样构造它
SELECT  idRole
        ,RoleName
FROM    UserRoles
WHERE   idRole IN (
            SELECT UserRole FROM UserMenuRights WHERE idAdmin =36
            )

关于sql - ')'附近的语法不正确。在sql的变量声明中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21300858/

10-10 16:12