为员工担任不同的角色

时间:2019-09-12 08:16:28

标签: sql-server tsql

我正在使用动态查询来检索员工的不同角色:

DECLARE @roles NVARCHAR(MAX)='';
    DECLARE @EmployeeId NVARCHAR(MAX)='';
    DECLARE  @Roles_Employee TABLE
(
     EmployeeId NVARCHAR(MAX),
     Scope NVARCHAR(MAX)
);


    SELECT @EmployeeId=@EmployeeId+(SELECT DISTINCT CAST(e.EmployeeId as nvarchar(255))  FROM Employee e
    LEFT JOIN Employee_Role er2 ON e.EmployeeId=er2.EmployeeId
    WHERE e.EmployeeId=54)
    ,
    @roles=@roles+ ('Select '+isnull(er.ColumnName,'*')+' from '+er.SchemaName+'.'+er.TableName+' where '+kcu.COLUMN_NAME +'='+er1.ValueId )

       from [Function].[Role] er 
        left join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on 
            tc.TABLE_NAME=er.TableName and tc.TABLE_SCHEMA=er.SchemaName
       left join INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on kcu.CONSTRAINT_NAME=tc.CONSTRAINT_NAME
        left JOIN Employee_Role er1 ON er.EntityRoleId = er1.RoleId
        left  JOIN Employee e ON er1.EmployeeId = e.EmployeeId
    where e.EmployeeId=54 AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND er1.ValueId !=0 AND er1.ValueId IS NOT NULL;
    INSERT INTO @Roles_Employee(EmployeeId,Scope)

  EXEC sp_executesql @roles;
  SELECT EmployeeId,Scope FROM @Roles_Employee;

我收到此错误:

  

列名或提供的值数与表不匹配   定义。

我的预期输出:

EmployeeId Scope
54         Commercial Collaboration - Head Of
54         Gardening
54         Portage

编辑  我可以为员工担任不同的角色,如下所示:

DECLARE @roles NVARCHAR(MAX)=''
        DECLARE @table TABLE
    (
        Scope NVARCHAR(MAX)
    )

    SELECT @roles=@roles+ 'Select '+isnull(er.ColumnName,'*')+' from '+er.SchemaName+'.'+er.TableName+' where '+kcu.COLUMN_NAME +'='+er1.ValueId 


       from [Function].[Role] er 
        left join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on 
            tc.TABLE_NAME=er.TableName and tc.TABLE_SCHEMA=er.SchemaName
       left join INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on kcu.CONSTRAINT_NAME=tc.CONSTRAINT_NAME
        left JOIN Employee_Role er1 ON er.EntityRoleId = er1.RoleId
        left  JOIN Employee e ON er1.EmployeeId = e.EmployeeId
    where e.EmployeeId=54 AND tc.CONSTRAINT_TYPE='PRIMARY KEY' AND er1.ValueId !=0 AND er1.ValueId IS NOT NULL

    INSERT INTO @table(Scope)
  EXEC sp_executesql @roles;

  SELECT * FROM @table

查询结果:

Scope
Commercial Collaboration - Head Of
Gardening
Portage

我想在带有范围的EmployeeId列中获取EmployeeId,这样我的输出将是:

EmployeeId Scope
    54         Commercial Collaboration - Head Of
    54         Gardening
    54         Portage

打印结果:

545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454545454
Select Label from dbo.Employee_Department where DepartmentId=156Select Label from dbo.Employee_Department where DepartmentId=207Select Label from dbo.Employee_Department where DepartmentId=60Select Label from dbo.Employee_Department where DepartmentId=74Select Label from dbo.Employee_Department where DepartmentId=89Select Label from dbo.Employee_Department where DepartmentId=187Select Label from dbo.Employee_Department where DepartmentId=34Select Label from dbo.Employee_Department where DepartmentId=155Select Label from dbo.Employee_Department where DepartmentId=17Select Label from dbo.Employee_Department where DepartmentId=18Select Label from dbo.Employee_Department where DepartmentId=20Select Label from dbo.Employee_Department where DepartmentId=26Select Label from dbo.Employee_Department where DepartmentId=34Select Label from dbo.Employee_Department where DepartmentId=57Select Label from dbo.Employee_Department where DepartmentId=58Select Label from dbo.Employee_Department where DepartmentId=62Select Label from dbo.Employee_Department where DepartmentId=155Select Label from dbo.Employee_Department where DepartmentId=17Select Label from dbo.Employee_Department where DepartmentId=74Select AccountId from Treasury.Account where AccountId=111Select AccountId from Treasury.Account where AccountId=128Select AccountId from Treasury.Account where AccountId=148Select AccountId from Treasury.Account where AccountId=149Select AccountId from Treasury.Account where AccountId=150Select AccountId from Treasury.Account where AccountId=161Select AccountId from Treasury.Account where AccountId=170Select AccountId from Treasury.Account where AccountId=171Select AccountId from Treasury.Account where AccountId=172Select AccountId from Treasury.Account where AccountId=181Select AccountId from Treasury.Account where AccountId=182Select AccountId from Treasury.Account where AccountId=187Select AccountId from Treasury.Account where AccountId=231Select AccountId from Treasury.Account where AccountId=246Select AccountId from Treasury.Account where AccountId=249Select AccountId from Treasury.Account where AccountId=255Select AccountId from Treasury.Account where AccountId=258Select AccountId from Treasury.Account where AccountId=259Select AccountId from Treasury.Account where AccountId=260Select AccountId from Treasury.Account where AccountId=261Select AccountId from Treasury.Account where AccountId=282Select AccountId from Treasury.Account where AccountId=284Select AccountId from Treasury.Account where AccountId=285Select AccountId from Treasury.Account where AccountId=292Select AccountId from Treasury.Account where AccountId=30Select AccountId from Treasury.Account where AccountId=314Select AccountId from Treasury.Account where AccountId=317Select AccountId from Treasury.Account where AccountId=320Select AccountId from Treasury.Account where AccountId=322Select AccountId from Treasury.Account where AccountId=323Select AccountId from Treasury.Account where AccountId=324Select AccountId from Treasury.Account where AccountId=325Select AccountId from Treasury.Account where AccountId=332Select AccountId from Treasury.Account where AccountId=333Select AccountId from Treasury.Account where AccountId=346Select AccountId from Treasury.Account where AccountId=347Select AccountId from Treasury.Account where AccountId=348Select AccountId from Treasury.Account where AccountId=349Select AccountId from Treasury.Account where AccountId=358Select AccountId from Treasury.Account where AccountId=359Select AccountId from Treasury.Account where AccountId=4Select AccountId from Treasury.Account where AccountId=413Select AccountId from Treasury.Account where AccountId=421Select AccountId from Treasury.Account where AccountId=422Select AccountId from Treasury.Account where AccountId=423Select AccountId from Treasury.Account where AccountId=445Select AccountId from Treasury.Account where AccountId=476Select AccountId from Treasury.Account where AccountId=50Select AccountId f

1 个答案:

答案 0 :(得分:1)

问题在于您的动态SQL为@roles,并且不包含EmployeeID

由于employeeID似乎是所有Scope的常量值,因此一种解决方案是将该值硬编码在动态SQL上作为附加列。

首先将EmployeeID SELECT与角色SELECT分开。

SET @EmployeeId = (
    SELECT DISTINCT 
        CAST(e.EmployeeId as nvarchar(255)) 
    FROM 
        Employee e
        LEFT JOIN Employee_Role er2 ON e.EmployeeId=er2.EmployeeId
    WHERE 
        e.EmployeeId=54)

然后将其值刻在角色SELECT上:

SELECT 
    @roles = @roles 
        + 'Select '
        + '''' + @EmployeeId + ''' AS EmployeeID, ' -- Add a new hard-coded column on the DynamicSQL
        + isnull(er.ColumnName,'*')
        + ' from ' + er.SchemaName + '.' + er.TableName
        + ' where '
        + kcu.COLUMN_NAME + '=' + er1.ValueId 
from 
    [Function].[Role] er 
    left join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on tc.TABLE_NAME=er.TableName and tc.TABLE_SCHEMA=er.SchemaName
    left join INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on kcu.CONSTRAINT_NAME=tc.CONSTRAINT_NAME
    left JOIN Employee_Role er1 ON er.EntityRoleId = er1.RoleId
    left  JOIN Employee e ON er1.EmployeeId = e.EmployeeId
where 
    e.EmployeeId=54 AND 
    tc.CONSTRAINT_TYPE='PRIMARY KEY' AND 
    er1.ValueId !=0 AND er1.ValueId IS NOT NULL;