2013-08-29 166 views
3

這與我最後一個問題有關。我有這樣的代碼:必須聲明標量變量錯誤

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的SET值並執行它時,它會顯示「Command(s)completed successfully。」。任何幫助?謝謝。試試這個fiddle

回答

7

嘗試改變這種說法 -

SELECT @query = ' 
SELECT * 
FROM( 
    SELECT 
     PatientId, 
     RTRIM(AttributeGroup) + ''_'' + RTRIM(AttributeId) AS ColName, 
     FormId, 
     Description 
    FROM dbo.DVItems 
    WHERE PatientId = ''' + CAST(@PatientId AS VARCHAR(36)) + ''' 
     AND FormId = ''' + @FormId + ''' 
) p 
PIVOT 
(
    MAX(Description) 
    FOR [ColName] IN (' + @columns + ') 
) AS p' 

完整的示例 -

DECLARE 
     @PatientId UNIQUEIDENTIFIER 
    , @FormId NCHAR(16) 
    , @columns VARCHAR(MAX) 
    , @query NVARCHAR(MAX) 

SELECT 
     @PatientId = '3387F956-EA0A-4660-8647-1096E25A3C6F' 
    , @FormId = 'form111' 

SELECT @columns = STUFF((
    SELECT DISTINCT ', [' + RTRIM(AttributeGroup) + '_' + RTRIM(AttributeId) + ']' 
    FROM dbo.DVItems 
    WHERE PatientId = @PatientId 
     AND FormId = @FormId 
    FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '') 

SELECT @query = ' 
SELECT * 
FROM( 
    SELECT 
     PatientId, 
     RTRIM(AttributeGroup) + ''_'' + RTRIM(AttributeId) AS ColName, 
     FormId, 
     Description 
    FROM dbo.DVItems 
    WHERE PatientId = ''' + CAST(@PatientId AS VARCHAR(36)) + ''' 
     AND FormId = ''' + @FormId + ''' 
) p 
PIVOT 
(
    MAX(Description) 
    FOR [ColName] IN (' + @columns + ') 
) AS p' 

EXEC sys.sp_executesql @query 

輸出 -

PatientId       FormId   test_01  test_02  test_03 
------------------------------------ ---------------- ------------- ------------ ------------ 
3387F956-EA0A-4660-8647-1096E25A3C6F form111   Test only  Test only Test only 
+2

試圖在小提琴;我想你可以在動態查詢中'CAST''UNIQUEIDENTIFIER',如下所示:'CAST(@PatientId AS VARCHAR(MAX))'真正使它工作(檢查[this](http://stackoverflow.com/ questions/1036572/how-to-concatenate-uniqueidentifier-in-a-dynamic-query)question/answer)。 – Josien

+0

@Josien你是對的。謝謝:) – Devart

+0

謝謝@Devart,但我也期待結果表..或者我想我的代碼中的東西?請幫忙。 – user2721415

0

像這樣

SqlCommand comb = new SqlCommand("INSERT INTO [Bolum] ([Id],[BolumId],[FakulteId], [Ad]) VALUES ('"+id+"','"+bolumid+"','"+fakulteid+"','"+txbAd.Text+"')", conb);