这与我的最后一个问题有关。我有这个代码:
DECLARE @PatientId UNIQUEIDENTIFIER
DECLARE @FormId NCHAR(16)
DECLARE @columns VARCHAR(8000)
DECLARE @query VARCHAR(8000)
SET @PatientId = '3387F956-EA0A-4660-8647-1096E25A3C6F'
SET @FormId = 'form111'
SELECT @columns = COALESCE(
@columns + ',[' + RTRIM(AttributeGroup) + '_' + RTRIM(AttributeId) + ']',
'[' + RTRIM(AttributeGroup) + '_' + RTRIM(AttributeId) + ']'
)
FROM DVItems
WHERE PatientId = @PatientId AND FormId = @FormId
GROUP BY AttributeGroup, AttributeId
SET @query = '
SELECT *
FROM(
SELECT
PatientId,
RTRIM(AttributeGroup) + ''_'' + RTRIM(AttributeId) AS ColName,
FormId,
Description
FROM DVItems
WHERE PatientId = @PatientId AND FormId = @FormId
) p
PIVOT
(
MAX(Description)
FOR [ColName]
IN (' + @columns + ')
) AS p'
EXECUTE (@query)
当我执行这个。它说“必须声明标量变量“@PatientId”。” 但是当我注释掉我为@PatientId 和@FormId 设置值并执行它的行时,它说“命令成功完成”。有什么帮助吗?谢谢你。试试这个小提琴。