"S" SQL Server checks for temporary table space usage

Source: Internet
Author: User

--Check for temporary tables that have been marked as needing to be deleted
SELECT * FROM
T_bas_temporarytablename;

--Temporary tables and views created by all systems
SELECT * from Sys.tables
WHERE name like ' tmp% '

--View the space occupied by all the tables in the system
CREATE TABLE Tmpspace
(Fname varchar (50),
Frows int,
freserved varchar (50),
Fdata
varchar (50),
Findex_size varchar (50),
Funused
varchar (50));

INSERT INTO Tmpspace
(Fname,frows,freserved, fdata,findex_size,funused)
EXEC sp_msforeachtable
@Command1 = "sp_spaceused '? '"

SELECT * from Tmpspace where Fname like
' tmp% ' ORDER by Fdata Desc

--Total size occupied by temporary tables (M)
Select
SUM (CONVERT (Decimal,replace (fdata, ' KB ', '))/1024 M from Tmpspace where Fname
Like ' tmp% ';
--drop table Tmpspace;

--Delete all temporary tables that have been marked as needing to be deleted
Declare
@sql as varchar (max)
Set @sql = ' '
Select @[email protected]+ ' drop table ' +name+ '; '
From Sys.tables u
Join T_bas_temporarytablename V on U.name=v.ftablename and

(V.fprocesstype=1 or
V.fcreatedate<getdate ()-1);
EXEC (@sql);
Delete u from
T_bas_temporarytablename u where
Not EXISTS (select 1 from Sys.tables where
U.ftablename=name);

"S" SQL Server checks for temporary table space usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.