0

我有一個SQL Server 2008 R2 Express數據庫。 Windows XP。如何在SQL Server 2008 R2數據庫中創建事件/函數而不是VBA宏?

我寫了這個宏來請求表大小和刪除行,如果表大於我需要,我每1小時從SCADA調用VBA。

Option Explicit 
Dim strConn As String 
Dim strSQL As String 
Dim objConn As Object 
Dim objRecSet As Object 
Dim Command As Object 
Dim objRecSetValue as Double 

On Error Resume Next 
Err.Number = 0 
strConn = "Provider=SQLOLEDB;Data Source=……….." 

Set objConn = CreateObject("ADODB.Connection") 
objConn.ConnectionString = strConn 
objConn.CursorLocation = 3 
objConn.Open 

Set objRecSet = CreateObject("ADODB.Recordset") 
Set Command = CreateObject("ADODB.Command") 
Command.CommandType = 1 
Set Command.ActiveConnection = objConn 

strSQL = "SELECT SUM(a.total_pages) * 8/1024 AS TotalSpaceMB FROM sys.tables t " 
strSQL = strSQL & "INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id " 
strSQL = strSQL & "INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id " 
strSQL = strSQL & "INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id " 
strSQL = strSQL & "WHERE t.NAME LIKE 'Statistic' AND i.index_id <= 1 GROUP BY t.NAME " 

Command.CommandText = strSQL 
Set objRecSet = Command.Execute 

objRecSet.MoveFirst 
objRecSetValue = objRecSet.Fields(0).Value 
objRecSet.Close 

If objRecSetValue> 5000 then 
    strSQL = "delete from dbo.statistic where dateandtime BETWEEN (SELECT MIN(dateandtime) FROM dbo.statistic) AND (SELECT MIN(dateandtime) FROM dbo.statistic) + 1" 
Command.CommandText = strSQL 
Set objRecSet = Command.Execute 
End if 

objConn.Close 

ErrorHandler1: 
Set Command = Nothing 
Set objConn = Nothing 
Set objRecSet = Nothing 

現在我想避免VBA,並在SQL Server Management Studio中(SSMS)創建過程/函數,與Windows Sheduler調用它,SQLCMD每隔1小時,但我不知道T-SQL和程序/功能很好。有人可以幫助我,在T-SQL中製作相同的代碼嗎? 謝謝。

+1

您已經編寫了所有的SQL代碼。你在掙扎什麼?以下是關於如何創建過程的鏈接(底部的示例):https://docs.microsoft.com/en-us/sql/relational-databases/stored-procedures/create-a-stored-procedure;這裏是關於如何編寫'IF'語句的鏈接:https://docs.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql – Alex

回答

1

下面是程序代碼

CREATE PROCEDURE proPurgeTableData 
AS 
BEGIN TRY 
    --variable for storing tablesize 
    DECLARE @tablesizeMB INT 

    --setting size to variable 
    SELECT 
     @tablesizeMB = SUM(a.total_pages) * 8/1024 
    FROM 
     sys.tables t 
     INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id 
     INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id 
     INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id 
    WHERE 
     t.NAME LIKE 'Statistic' AND i.index_id <= 1 
    GROUP BY 
     t.NAME 

    --if size greater than 5000 delete from table 
    IF (ISNULL(@tablesizeMB,0)>5000) 
    BEGIN 
     BEGIN TRAN 
      DELETE FROM dbo.statistic 
      WHERE dateandtime BETWEEN (SELECT MIN(dateandtime) FROM dbo.statistic) AND (SELECT MIN(dateandtime) FROM dbo.statistic) + 1 
     COMMIT TRAN 
    END 
END TRY 
BEGIN CATCH 
    IF @@TRANCOUNT>0 ROLLBACK TRAN 
END CATCH 

您可以執行此作爲EXEC proPurgeTableData,只是在SQL作業添加此。

+0

輝煌! :' - )MS SQL Express沒有Agent SQL,但我將使用sqlcmd和調度程序 –

相關問題