在sp_executesql中,select語句用於連接變量本身和臨時表中的列值。將索引添加到臨時表導致單行結果集
當我將此索引應用於此臨時表並在上述select語句中使用排序時,會給出奇怪的結果。
這隻發生在臨時表的行數大於50行時。
我不喜歡粘貼大代碼示例,但我無法進一步減少它。
如果@maxjob> = 8,則生成的@htmllog包含67個字符。這是意想不到的結果。
如果@maxjob < 8,則生成的@htmllog始終包含超過67個字符。這是預期的結果。
此外,
當我從#acl_log表中刪除索引idx_key,那麼問題消失時@maxjob> = 8 或 當我從刪除「通過[鍵] ASC順序」 @執行sql_sql,那麼問題也會消失。
爲什麼?
declare @logtable as varchar(max)
set @logtable = '#acl_log'
if (OBJECT_ID('[tempdb]..#acl_log')) is not null
drop table #acl_log
create table #acl_log(
[key] int identity,
[message] nvarchar(max) not null,
index idx_key ([key])
)
declare @job as int
declare @maxjob as int
set @job = 0
set @maxjob = 8
while (@job < @maxjob + 1)
begin
insert into #acl_log([message])
values
('Internet Explorer is currently running without add-ons')
,('All Internet Explorer add-ons, such as ActiveX controls or toolbars, are turned off. Some webpages might not display correctly.')
,('To continue to your home page, click the Home button.')
,('To browse using add-ons, close Internet Explorer and then start it again.')
,('Forward Arrow Check for the latest Windows updates. ')
,('Question Icon How do browser add-ons affect my browsing experience? ')
set @job = @job + 1
end
declare @executesql_sql as nvarchar(max)
declare @executesql_param as nvarchar(max)
declare @htmllog as varchar(max)
set @executesql_sql = '
set @htmllog = ''''
select @htmllog = @htmllog + [message]
from ' + @logtable + '
order by [key] asc'
set @executesql_param = '@htmllog varchar(max) output'
exec master..sp_executesql @executesql_sql, @executesql_param, @htmllog = @htmllog output
select len(@htmllog), @htmllog
索引應該對輸出的長度沒有影響。我懷疑還有其他事情正在進行 - 可能是因爲你一遍又一遍地運行相同的代碼,它使用了可能存在的表。但是,當你可以使用'for xml path'時,爲什麼使用動態SQL來連接字符串呢? –
我看到了'for xml path'的例子,但是如果我想讓一個單元在發生錯誤時點亮,則無法添加bgcolor屬性。所以我擴展了'for xml路徑'並去了一個更可讀的關係選擇語句。 – Ronald
。 。如果你需要幫助,你可以問另一個問題。 –