我是这个SQL脚本的新手。
尝试执行删除命令时出错。
消息8169,级别16,状态2,行52转换时转换失败 从字符串到uniqueidentifier。
我要执行的脚本是:
-- 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'
当我解析上面的脚本然后它成功完成但在执行时它会抛出错误。
答案 0 :(得分:0)
自SQL 2102以来,有两个新功能可用:TRY_CAST和TRY_CONVERT,它们是对现有CAST和CONVERT函数的补充。
如果将空字符串强制转换为唯一标识符:
SELECT CAST('' as uniqueidentifier) uid
这将导致您看到的错误。对于不是GUID的其他字符串也是如此。使用TRY_CAST代替,该函数返回NULL而不是引发错误。然后,您可以跳过没有有效GUID的记录。
begin
set @fileGuid = substring(@attachXML, charindex('<Guid>', @attachXML) + 6, 36)
DECLARE @fileGuidOrNull uniqueidentifier = try_convert(uniqueidentifier, @fileGuid)
IF @fileGuidOrNull IS NOT NULL
delete from tps_attachment_data where tps_guid = @fileGuidOrNull
fetch next from attachCursor1 into @attachXML
end