尽管是nvarchar(max)

时间:2016-04-22 06:42:19

标签: sql-server dynamic-sql

declare @sql6 nvarchar(max) = ''

set @sql6 = @sql6 + ' CODE'

EXEC sp_executesql @SQL6

是否有任何情况下以这种方式声明动态SQL会被切断?

这是一堆代码,不想粘贴文本墙,但print语句的结尾是:

GROUP BY 
    T1.TreatmentID, T1.SamplingRandomNumber,
    T1.ProductID, T1.OptimizationGroupID,
    T1.CurrencyCode, C.CalculatedFromText,
    T1.TreatmentOrdinal, T1.SourceCode,
    T1.[IsPriorBoo

它应该是更多。

错误是

  

','附近的语法不正确。

查询特定部分的完整代码(在大型动态查询中)

SELECT 
    T1.TreatmentID,
    T1.SamplingRandomNumber,
    T1.ProductID, 
    T1.OptimizationGroupID,
    T1.CurrencyCode,
    C.CalculatedFromText,
    CONVERT(FLOAT,COUNT(1)) CountCalculatedFromText,
    T1.TreatmentOrdinal,
    Sum(T1.Revenue) AS Revenue,
    T1.SourceCode
    '+replace(@ColumnsPB,'t.','T1.') +'
INTO 
    #RevenueDetailCount 
FROM 
    #RevenueDetailCountTmp T1
INNER JOIN 
    #CalculatedFromText C ON  T1.ProductID = C.ProductID 
                          AND T1.OptimizationGroupID = C.OptimizationGroupID
                          AND T1.TreatmentOrdinal = C.TreatmentOrdinal
                          AND T1.SamplingRandomNumber = C.SamplingRandomNumber
GROUP BY 
    T1.TreatmentID,
    T1.SamplingRandomNumber,
    T1.ProductID, 
    T1.OptimizationGroupID,
    T1.CurrencyCode,
    C.CalculatedFromText,
    T1.TreatmentOrdinal,
    T1.SourceCode
     '+replace(@ColumnsPB,'t.','T1.') +'

编辑: 全印刷动态sql

DECLARE @CalculatedFromText AS TABLE
(
    CalculatedFromText varchar(100), 
    CalculatedFrom     smallint
)

INSERT INTO @CalculatedFromText 
( 
    CalculatedFromText, 
    CalculatedFrom
)
VALUES 
    ('Control' ,1), 
    ('Treatment' ,2), 
    ('Dynamic' ,3)




select 
    TP.TreatmentID,
    TP.ProductID,
    TP.OptimizationGroupID,
    MIN(TP.CalculatedFromText) AS CalculatedFromText,
    TP.TreatmentOrdinal,
    TP.SamplingRandomNumber,
    TP.CurrencyCode,
    TP.SourceCode
    , TP.[IsPriorBooking]
INTO #CalculatedFromText
FROM #CalculatedFromTextTmp TP
GROUP BY  
    TP.TreatmentID,
    TP.ProductID, 
    TP.OptimizationGroupID,
    TP.TreatmentOrdinal,
    TP.SamplingRandomNumber,    
    TP.CurrencyCode ,
    TP.SourceCode
    , TP.[IsPriorBooking]



SELECT  
    SamplingRandomNumber,
    TreatmentID,
    CalculatedFromText, 
    ProductID, 
    OptimizationGroupID,
    CONVERT(FLOAT,COUNT(1)) AS CountCalculatedFromText,
    TreatmentOrdinal,
    CurrencyCode,

    SourceCode
    , [IsPriorBooking]
INTO #CountCalculatedFromText
FROM #CalculatedFromText
GROUP BY
    SamplingRandomNumber,
        TreatmentID, 
        CalculatedFromText,
         ProductID,
         OptimizationGroupID,
         TreatmentOrdinal,
         CurrencyCode,
         SourceCode
         , [IsPriorBooking]
 SELECT 
    SamplingRandomNumber,
    TreatmentID,
    ProductID, 
    OptimizationGroupID,
    SUM(CountCalculatedFromText) AS AllCalculatedFromText,
    CurrencyCode,

    SourceCode
    , [IsPriorBooking]
INTO #AllCalculatedFromText
FROM #CountCalculatedFromText AS cps2
GROUP BY 
    SamplingRandomNumber,
    TreatmentID,
    ProductID,
    OptimizationGroupID,
    CurrencyCode,
    SourceCode
     , [IsPriorBooking]


SELECT 
    CPS.SamplingRandomNumber,
    CPS.TreatmentID,
    CPS.CalculatedFromText, 
    CPS.TreatmentOrdinal,
    CPS.ProductID, 
    CPS.OptimizationGroupID,
    CPS.CountCalculatedFromText,
    CPS.CountCalculatedFromText/T.AllCalculatedFromText AS PercentCalculatedFromText,
    CPS.CurrencyCode,
    CPS.SourceCode
    , CPS.[IsPriorBooking]
INTO #PercentCalculatedFromText
FROM #CountCalculatedFromText AS CPS
    INNER JOIN #AllCalculatedFromText T ON
    T.TreatmentID=CPS.TreatmentID AND T.ProductID = CPS.ProductID
        AND T.OptimizationGroupID = CPS.OptimizationGroupID
        AND T.CurrencyCode = CPS.CurrencyCode

SELECT 
    T.SamplingRandomNumber,
    T.TreatmentID,
    T.ProductID,
    T.OptimizationGroupID,
    T.TreatmentOrdinal,
    T.CurrencyCode, 

    CASE WHEN T.CalculatedFromText = 2 THEN PS.CalculatedFromText + ' + CAST(T.TreatmentOrdinal AS char) 
        ELSE PS.CalculatedFromText END AS PricingType,
    T.CalculatedFromText,
    T.CountCalculatedFromText AS QuantityLooks,
    T.PercentCalculatedFromText  AS LooksPercentageOfTransactions,
    T.SourceCode
    , T.[IsPriorBooking]
INTO #CountCalculatedFromTextPivot
FROM #PercentCalculatedFromText T
    INNER JOIN @CalculatedFromText AS PS ON T.CalculatedFromText = PS.CalculatedFrom



SELECT 
    T1.TreatmentID,
    T1.ProductID, 
    T1.GroupCode,

    T1.OptimizationGroupID,
    CASE WHEN T1.CalculatedFrom = 2 THEN T1.TreatmentOrdinal ELSE 0 END AS TreatmentOrdinal,
    T1.CurrencyCode,
    T1.SamplingRandomNumber,
    T1.Revenue Revenue,
    T1.SourceCode
    , T1.[IsPriorBooking]
INTO #RevenueDetailCountTmp 
FROM #TreatmentDetails T1
WHERE T1.Stage=2

SELECT 
    T1.TreatmentID,
    T1.SamplingRandomNumber,
    T1.ProductID, 
    T1.OptimizationGroupID,
    T1.CurrencyCode,

    C.CalculatedFromText,
    CONVERT(FLOAT,COUNT(1)) CountCalculatedFromText,
    T1.TreatmentOrdinal,
    Sum(T1.Revenue) AS Revenue,
    T1.SourceCode
    , T1.[IsPriorBooking]
INTO #RevenueDetailCount    
FROM #RevenueDetailCountTmp T1
    INNER JOIN #CalculatedFromText C ON  T1.ProductID = C.ProductID 
        AND T1.OptimizationGroupID = C.OptimizationGroupID
        AND T1.TreatmentOrdinal = C.TreatmentOrdinal
        AND T1.SamplingRandomNumber = C.SamplingRandomNumber
GROUP BY 
    T1.TreatmentID,
    T1.SamplingRandomNumber,
    T1.ProductID, 
    T1.OptimizationGroupID,
    T1.CurrencyCode,
    C.CalculatedFromText,
    T1.TreatmentOrdinal,
    T1.SourceCode
     , T1.[IsPriorBoo

1 个答案:

答案 0 :(得分:0)

这里你可能有两个问题。您可以参考PRINT语句 - 这将停止在8000字节(因为您使用的是nvarchar时为4000个字符)。如果你想在SSMS中显示整个查询,你最好把它转换为XML输出,这将为你提供一个你可以在SSMS中点击并显示整个事物的字段。

在可见的查询的短位中,您实际上在THEN的第一部分中得到了额外的引用,这至少打破了您查询的那部分:

CASE WHEN T.CalculatedFromText = 2 THEN PS.CalculatedFromText + ' + CAST(T.TreatmentOrdinal AS char)