我有以下查询:
DECLARE @userrole as VARCHAR(100)
SELECT @userrole = UserRole
FROM UserMenuRights
WHERE idAdmin = 36
Execute('Select idRole,RoleName from UserRoles WHERE idRole IN ('+@userrole+')')
我收到错误:
Msg 102,Level 15,State 1,Line 1
')'附近的语法不正确。
我已检查第一行DECLARE @userrole as VARCHAR(100)
似乎没有语法问题。
同时检查最后一行:
Execute('Select idRole,RoleName from UserRoles WHERE idRole IN ('+@userrole+')')
也似乎是正确的字符串附件。
我在哪里犯了错误?
请帮帮我。
答案 0 :(得分:2)
试试这个,你需要添加单引号
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
)
答案 1 :(得分:2)
DECLARE @userrole as VARCHAR(100)
SELECT @userrole =UserRole FROM UserMenuRights WHERE idAdmin =36
-- Use sp_executesql instead
DECLARE @Statement NVARCHAR(MAX)
SET @Statement = N'SELECT idRole, RoleName FROM UserRoles WHERE idRole = @UserRole'
EXEC sp_executesql @statement, N'@UserRole varchar(100)', @UserRole