我有必须从其值填充的本地表和字符串:
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'
更新不可见