【发布时间】:2016-06-25 06:40:03
【问题描述】:
我是这个 SQL 脚本的新手。
尝试执行删除命令时出错。
消息 8169,级别 16,状态 2,行 52 转换时转换失败 从字符串到唯一标识符。
而我要执行的脚本是:
-- attachments cleardown script
-- SJM 18/09/2009
set nocount on
declare @tableName nvarchar(200)
declare @columnName nvarchar(200)
declare @moduleName nvarchar(200)
declare @objectName nvarchar(200)
declare @attributeName nvarchar(200)
declare @dynamicSQL nvarchar(500)
declare @attachXML varchar(2000)
declare @fileGuid varchar(36)
declare @deletedXML varchar(100)
set @deletedXML = '<DataObjectAttachment><FileName>Deleted</FileName></DataObjectAttachment>'
declare attachCursor cursor fast_forward for
select t5.md_name, t4.md_name, t3.md_title, t2.md_title, t1.md_title
from md_attribute_type t1
join md_class_type t2 on t1.md_class_type_guid = t2.md_guid
join md_module t3 on t2.md_module_guid = t3.md_guid
join md_database_column t4 on t1.md_database_column_guid = t4.md_guid
join md_database_table t5 on t2.md_database_table_guid = t5.md_guid
where t1.md_data_type = 16 and (
t1.md_defined_by = 2 or
t1.md_guid in ('103DBEAB-252F-4C9A-952A-90A7800FFC00', '2515E980-788D-443D-AA89-AA7CC3653867',
'2D29495E-785E-4062-A49C-712A8136EBC7', '6A204C77-1007-48CC-B3BC-077B094540AE',
'9A24BFEF-2CAB-4604-8814-656BA0B9ECC3', 'C368CB18-B4C4-4C4F-839C-F7E6E1E17AA8',
'0814586B-A11E-4129-AF9B-9EC58120C9AF', 'BD4F73C4-07CA-4DC2-86AD-D713FBC6E7BA')
)
and t2.md_behaviour not in (2, 4, 8) -- exclude reference lists
and t2.md_guid not in (select b.md_class_type_guid from md_class_behaviour b where b.md_behaviour_type_guid in
(select bt.md_guid from md_behaviour_type bt where bt.md_name in ('Category','Ranked'))) -- exclude categories and ordered lists
and t3.md_name != 'Lifecycle'
order by t3.md_title, t2.md_title, t1.md_title
open attachCursor
fetch next from attachCursor into @tableName, @columnName, @moduleName, @objectName, @attributeName
while (@@FETCH_STATUS = 0)
begin
print 'Deleting from ' + @moduleName + ' -> ' + @objectName + ' (' + @tableName + ') -> ' + @attributeName + ' (' + @columnName + ')...'
set @dynamicSQL = 'declare attachCursor1 cursor fast_forward for'
set @dynamicSQL = @dynamicSQL + ' select ' + @columnName + ' from ' + @tableName
set @dynamicSQL = @dynamicSQL + ' where ' + @columnName + ' != ''' + @deletedXML + ''''
exec sp_executesql @dynamicSQL
open attachCursor1
fetch next from attachCursor1 into @attachXML
while (@@FETCH_STATUS = 0)
begin
set @fileGuid = substring(@attachXML, charindex('<Guid>', @attachXML) + 6, 36)
delete from tps_attachment_data where tps_guid = convert(uniqueidentifier, @fileGuid)
fetch next from attachCursor1 into @attachXML
end
close attachCursor1
deallocate attachCursor1
set @dynamicSQL = 'update ' + @tableName + ' set ' + @columnName + ' = ''' + @deletedXML + ''''
set @dynamicSQL = @dynamicSQL + ' where ' + @columnName + ' != ''' + @deletedXML + ''''
exec sp_executesql @dynamicSQL
print '- Deleted ' + convert(varchar(10),@@Rowcount) + ' records.'
fetch next from attachCursor into @tableName, @columnName, @moduleName, @objectName, @attributeName
end
close attachCursor
deallocate attachCursor
set nocount off
print char(13) + 'Attachment cleardown complete.'
print char(13) + 'Next steps to reclaim data file space:'
print '1. Take a backup!'
print '2. Shrink the database using the command: DBCC SHRINKDATABASE(''' + convert(varchar(100),SERVERPROPERTY('ServerName')) + ''')'
print '3. Put the database into SINGLE_USER mode (Properties -> Options -> Restrict Access)'
print '4. Detach the database'
print '5. Rename the database .LDF log file'
print '6. Re-attach the database using single file method'
当我解析上面的脚本时,它会成功完成,但在执行时会抛出错误。
【问题讨论】:
-
让我们谈谈您的 TPS 报告...第 52 行是
tps_guid = convert(uniqueidentifier, @fileGuid)所以@fileGuid/tps_guid不包含可以转换为 GUID 的内容,打印出来看看有什么变化。 -
我尝试查找 tps_attachment_data 中是否有任何内容并执行了命令: select * from tps_attachment_data 这给了我 311473 行并且所有行都有 tps_guid。
-
@fileGuid 呢?
-
如何检查是否有任何@fileGuid?请帮帮我。
-
赋值后打印出来
标签: sql-server tsql uniqueidentifier