MSSQL - 当列名动态时显示数据
我正在向员工提交最终的权威评估分数。 每行都是员工的数据,并且由于要评估的类别可能会随时间段的不同而变化,因此列名称不能在存储过程中进行硬编码。 我已经设计了以下解决方案。
1 创建临时表
2 动态使用Alter Table命令添加所有适用的列(存储在@ColumnNames中)
3 在游标内使用动态 SQL 为每个获得正确分数的员工编写一条插入(IE N 员工意味着 N 次插入)
(SELECT @ECMScores = COALESCE(@ECMScores + ',', '') + CAST(EIS.ECMScore AS NVARCHAR(1000)) (FROM...))
SET @SQLString = ''
SET @SQLString = @SQLString + 'INSERT INTO #ResultSet ('
SET @SQLString = @SQLString + 'EvaluationScoreID,'
SET @SQLString = @SQLString + 'EmployeeID,'
SET @SQLString = @SQLString + 'EmployeeName,'
SET @SQLString = @SQLString + @ColumnNames
SET @SQLString = @SQLString + ') '
SET @SQLString = @SQLString + 'VALUES ('
SET @SQLString = @SQLString + ''+CAST(@EvaluationScoreID AS NVARCHAR(MAX))+','
SET @SQLString = @SQLString + ''+CAST(@EmployeeID AS NVARCHAR(MAX))+','
SET @SQLString = @SQLString + '"'+@EmployeeName+'",'
SET @SQLString = @SQLString + @ECMScores
SET @SQLString = @SQLString + ')'
EXECUTE sp_executesql @SQLString
问题是每 100 名员工大约需要 1 秒。 这很快就变得不可接受……
有人对如何进行有更好的想法吗? 删除光标(显然),并使用一个插入(也许选择进入)是我的第一个想法,也许是从动态创建的 XML 变量中读取......
谢谢,
I am presenting to a final authority evaluation scores for employees. Each row is an employee’s data and since the categories to be evaluated can change from period to period the column names cannot be hardcoded in the Stored Procedures. I have already devised the following solution.
1 Create a temp table
2 Dynamically use the Alter Table command to add all applicable columns (Stored in @ColumnNames)
3 Use Dynamic SQL inside a cursor to write an insert for each employee that gets the correct scores (IE N employees means N inserts)
(SELECT @ECMScores = COALESCE(@ECMScores + ',', '') + CAST(EIS.ECMScore AS NVARCHAR(1000)) (FROM...))
SET @SQLString = ''
SET @SQLString = @SQLString + 'INSERT INTO #ResultSet ('
SET @SQLString = @SQLString + 'EvaluationScoreID,'
SET @SQLString = @SQLString + 'EmployeeID,'
SET @SQLString = @SQLString + 'EmployeeName,'
SET @SQLString = @SQLString + @ColumnNames
SET @SQLString = @SQLString + ') '
SET @SQLString = @SQLString + 'VALUES ('
SET @SQLString = @SQLString + ''+CAST(@EvaluationScoreID AS NVARCHAR(MAX))+','
SET @SQLString = @SQLString + ''+CAST(@EmployeeID AS NVARCHAR(MAX))+','
SET @SQLString = @SQLString + '"'+@EmployeeName+'",'
SET @SQLString = @SQLString + @ECMScores
SET @SQLString = @SQLString + ')'
EXECUTE sp_executesql @SQLString
The problem is it takes approx 1 second for every 100 employees. This quickly becomes unacceptable…
Does anyone have any better ideas on how to proceed? Removing the cursor (obviously), and using one insert (Perhaps Select into) is my first idea perhaps reading from a dynamically created XML variable…
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在架构中存储数据,
创建这样的表:
您现在可以创建“作业”并插入“分数”,而无需构建动态查询,因为您的列总是会发生变化。 您的列现在将保持不变,并且数据将发生变化。
don't store data in the schema
create tables like this:
you can now create "assignments" and insert "scores" without building dynamic queries because your columns always change. your columns will now stay the same and the data will change.
首先,你有机会改变数据库结构吗? 如果我理解正确,字段 EIS.ECMScore 包含多个值,这违反了规范化规则。 我个人会重新设计表EIS并将其规范化,因为这将使您的任务变得更加容易。
First of all, would you have the opportunity to alter the database structure? If I understand correctly, field EIS.ECMScore contains multiple values, which it's a violation of normalization rules. I personally would redesign table EIS and normalize it, as it would make your task much easier.