3

我有必须从其值填充的本地表和字符串:

    DECLARE @#SomeTable  TABLE ( some columns ..)
    DECLARE @SomeString  varchar(8000) = 'init string'

在迭代它时

    WHILE EXISTS(SELECT * FROM  @#SomeTable)
     BEGIN
        // [somecolumn] is declared temp variable
        SELECT TOP 1 @somecolumn = somecolumn FROM  @#SomeTable

        PRINT 'before ' + @SomeString // 'init string'
        PRINT [some values from SomeTable]  // this OK
        SET @SomeString += [some values from SomeTable] 
        PRINT 'after ' +  @SomeString //'init string' UPDATE NOT TAKE PLACE!!!!

        DELETE  @#SomeTable Where somecolumn = @somecolumn
     END

我发现连接失败。为什么?

编辑:

这是一段原始代码:

    /*Represents [WHERE] clause for retrieving values from specifyed range*/
DECLARE @WHEREclause nchar(1000) = 'WHERE '
/*Represents [ORDER BY] clause for sorting in right order and direction {ASC|DESC}*/
DECLARE @ORDERBYclause nchar(1000) = 'ORDER BY '
/*Dynamic query that returns end result*/
DECLARE @sqlCmd varchar(8000) = 
'SELECT 
        img,
        capacity,
        price,
        Id
 FROM HDD  '
/* -a- filling table for input values*/
INSERT INTO @#SequenceTable(columnName,columnValue,comparator,isASC,columnOrder)
SELECT 
    columnName,
    columnValue,
    comparator,
    isASC,
    ROW_NUMBER() OVER (ORDER BY  outOrder) AS columnOrder
FROM
(
        SELECT 'buffer' as columnName, CAST(@buffer AS nchar(20)) as columnValue, @bufferCmp as comparator, @bufferASC as isASC, @bufferOrder as outOrder
    UNION  
        SELECT 'capacity', CAST(@capacity AS nchar(20)), @capacityCmp, @capacityASC, @capacityOrder  
    UNION  
        SELECT 'price', STR(@price,20,2),  @priceCmp, @priceASC, @priceOrder
    UNION  
        SELECT 'angle_speed', CAST(@angleSpeed AS nchar(20)), @angleSpeedCmp ,@angleSpeedASC,@angleSpeedOrder
) AS AnyName
ORDER BY columnOrder 

/*---/a-----------------------------------------------------------------------------------*/

/*variables for above fields*/
DECLARE @columnName nchar(20)
DECLARE @comparator char
DECLARE @columnValue nchar(20)
DECLARE @isASC char

WHILE EXISTS(SELECT * FROM  @#SequenceTable)
BEGIN

    SELECT TOP 1 @columnName = columnName FROM  @#SequenceTable
    SELECT TOP 1 @comparator = comparator FROM  @#SequenceTable 
    SELECT TOP 1 @columnValue = columnValue FROM  @#SequenceTable 
    SELECT TOP 1 @isASC = isASC FROM  @#SequenceTable 

    IF @WHEREclause != 'WHERE '
        BEGIN
            SET @WHEREclause += ' AND '
        END
    PRINT 'before ' + @WHEREclause
    PRINT CONCAT(RTRIM(@columnName), @comparator, @columnValue)
    SET @WHEREclause += CONCAT(RTRIM(@columnName), @comparator, @columnValue)
    PRINT 'after ' +  @WHEREclause

    IF @ORDERBYclause != 'ORDER BY '
        BEGIN
            SET @ORDERBYclause += ','
        END
    IF @isASC = '1'
       SET @ORDERBYclause += CONCAT(RTRIM(@columnName),' ASC ')
    ELSE
       SET @ORDERBYclause += CONCAT(RTRIM(@columnName),' DESC ')

    Delete  @#SequenceTable Where columnName = @columnName

END

这是一段打印结果:

        before WHERE 
        angle_speed=7400 
        after WHERE 

+=没有按预期工作。如果我使用

@SomeString = 'some value' 

那么更新是可见的,但如果我改为使用

@SomeString += 'some value' 

更新不可见

4

3 回答 3

10

我可以向您保证,SQL Server 中没有错误,并且+=完全按预期工作。我尝试了以下代码:

DECLARE @#SomeTable TABLE (somecolumn varchar(8000));

INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');

DECLARE @SomeString  varchar(8000) = 'init string',
        @somecolumn  varchar(8000);

WHILE EXISTS (SELECT * FROM  @#SomeTable)
BEGIN
    SELECT TOP 1 @somecolumn = somecolumn FROM @#SomeTable;

    SET @SomeString += @somecolumn;

    PRINT @SomeString; -- Works fine!!!

    DELETE  @#SomeTable Where somecolumn = @somecolumn;
END

这是我的结果:

init stringa
init stringabbb
init stringabbbccccc

由于无法准确说明您在代码中正在做什么(您已经混淆了最重要的部分),也许您可​​以从那里开始?当然,要么你在表中有一个 NULL 值,要么你分配不正确,或者你分配给了错误的变量。同样,无法分辨,因为您隐藏了代码的关键部分!

此外,由于您似乎不关心订单,您也可以在不循环的情况下执行此操作:

DECLARE @#SomeTable TABLE (somecolumn varchar(8000));

INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');

DECLARE @SomeString  varchar(8000) = 'init string',
        @somecolumn  varchar(8000);

SELECT @SomeString += somecolumn FROM @#SomeTable;

PRINT @SomeString;

结果:

init stringabbbccccc

如果您关心顺序,您仍然可以在不循环的情况下执行此操作 - 使用 XML 技巧按该顺序连接,然后将其附加到初始化字符串:

DECLARE @#SomeTable TABLE (somecolumn varchar(8000));

INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');

DECLARE @SomeString  varchar(8000) = 'init string',
        @somecolumn  varchar(8000) = '';

SELECT @somecolumn = (SELECT '' + somecolumn FROM @#SomeTable
ORDER BY somecolumn DESC
FOR XML PATH(''), TYPE).value(N'./text()[1]', N'varchar(max)');

PRINT @SomeString + @somecolumn;

结果:

init stringcccccbbba

在更现代的版本(SQL Server 2017+)上,您可以这样做:

DECLARE @#SomeTable TABLE (somecolumn varchar(8000));

INSERT @#SomeTable VALUES('a'), ('bbb'), ('ccccc');

DECLARE @SomeString  varchar(8000) = 'init string',
        @somecolumn  varchar(8000);

SELECT @somecolumn = STRING_AGG(somecolumn, '')
  WITHIN GROUP (ORDER BY somecolumn DESC)
  FROM @#SomeTable;

PRINT @SomeString + @somecolumn;
于 2013-01-13T03:14:30.300 回答
8

SQL Server 中两个值之间的任何计算都会导致NULL所涉及的值之一是NULL。在您的情况下,@SomeString未初始化。这意味着它的值为NULL。因此SET @SomeString =+ 'somevalue'导致NULL.

要解决此问题,请''在开始时将变量初始化为。

另外,你有这行代码:

SET @somecolumn += [some values from SomeTable]

但你可能打算拥有这个:

SET @SomeString += [some values from SomeTable]
于 2013-01-12T16:17:00.593 回答
1

我有同样的问题,这肯定是 T-SQL 的一个已知问题。这里详细解释:https ://marc.durdin.net/2015/07/concatenating-strings-in-sql-server-or-undefined-behaviour-by-design/

总结一下:

带有 SELECT 语句的变量赋值是一种专有语法(仅限 T-SQL),如果产生多行,则行为未定义或依赖于计划。

在带有 ORDER BY 子句的查询中使用赋值操作(本示例中的连接)具有未定义的行为。由于查询计划的更改,这可能会因版本而异,甚至在特定服务器版本中也会发生变化。即使有变通方法,您也不能依赖此行为。

唯一保证的机制如下: 1. 使用游标以特定顺序遍历行并连接值 2. 使用 ORDER BY 进行 xml 查询以生成连接值 3. 使用 CLR 聚合(这不适用于 ORDER BY 子句)

于 2016-08-10T01:49:21.737 回答