BEGIN TRY
Create Table #Temp_Table_Record_Stat
(
....
)
DECLARE table_Cursor CURSOR FOR
SELECT object_id,t.name,convert(nvarchar,isnull(tn.Value,t.name)) as caption
FROM sys.tables t
left join sys.extended_properties tn on t.object_id=tn.major_id and tn.minor_id=0
WHERE t.Type='U' and t.is_ms_shipped=0 AND t.name<>'sysdiagrams';
OPEN table_Cursor
FETCH NEXT FROM table_Cursor INTO @v_Table_ID,@v_Table_Name,@v_Table_Caption;
WHILE @@FETCH_STATUS = 0
BEGIN
-- make sure the object exists before calling sp_spacedused
IF EXISTS(SELECT id FROM sysobjects WHERE id = @v_Table_ID)
BEGIN
--相关游标操作
UPDATE #Temp_Table_Record_Stat SET Table_Caption=@v_Table_Caption WHERE Table_Name=@v_Table_Name;
END
FETCH NEXT FROM table_Cursor INTO @v_Table_ID,@v_Table_Name,@v_Table_Caption;
END
CLOSE table_Cursor
DEALLOCATE table_Cursor
RETURN 0;
END TRY
-- 结束Try
-- 开始对抛出的例外处理
BEGIN CATCH
BEGIN TRY
IF CURSOR_STATUS('global','table_Cursor')<>-3
DEALLOCATE table_Cursor;
END TRY
BEGIN CATCH
PRINT '关闭游标出错';
END CATCH;
PRINT ERROR_MESSAGE();
RETURN ERROR_NUMBER();
END CATCH;