我试图为我创建一种方法,同时在多个表中应用truncate。
我尝试了以下代码:
SELECT CONCAT('TRUNCATE TABLE ',table_schema,'.',TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema in ('mytable1','mytable2','mytable3');
更不成功的是,它没有截断表格。
有谁知道有任何方法可以做到这一点?
答案 0 :(得分:0)
如果你想让它保持原子状态,请使用事务来完成。
Dim Sale_Amount, Sales_Tax_Rate, Sales_Tax_Amount, Total_Price
Dim MySheet, RowCount, i
'Create connection b/w UFT and Excel
Set MyExcel = CreateObject("Excel.Application")
MyExcel.Workbooks.Open "C:\Users\PIIT\Desktop\UFT\BootCamp2\BootCamp2 Question 4.xlsx"
MyExcel.Application.Visible = True
Set MySheet = MyExcel.ActiveWorkbook.Worksheets ("Sheet1")
RowCount = MySheet.UsedRange.Rows.Count
For i = 2 To RowCount
'Read Values from Excel
Sale_Amount = MySheet.Cells(i, "A").Value
Sales_Tax_Rate = MySheet.Cells(i, "B").Value
'Call Function
fnTax_Calculator
Next
Function fnTax_Calculator()
Dim Arry(1)
'Function should return Sales_Tax_Amount when it is called in the form of %x.xx
Sales_Tax_Amount = Sale_Amount * Sales_Tax_Rate
'Function should return Total_Price when it is called in the form of $xxxx.xx
Total_Price = Sale_Amount + Sales_Tax_Amount
Arry(0) = Sales_Tax_Amount
Arry(1) = Total_Price
End Function
答案 1 :(得分:0)
DECLARE @CODE NVARCHAR(MAX) = '', @XML XML
SET @XML = STUFF((
SELECT ' ' + data.CODE_DELETE
FROM (
SELECT CONCAT ('TRUNCATE TABLE ', table_schema, '.', TABLE_NAME, '') AS CODE_DELETE
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME IN ('CITY', 'WORKERS')
) [data]
FOR XML PATH('')
), 1, 1, '')
SET @CODE = CAST(@XML AS VARCHAR(MAX))
SELECT @CODE
EXECUTE sp_executesql @CODE;
变量@XML
是能够将行转换为单个值,然后我们必须将sql代码转换为varchar
才能执行它
EXECUTE sp_executesql @CODE;
您可以看到执行此操作时将执行的代码:
SELECT @CODE
city
和workers
是我在数据库中的表格,如果您看起来我在table_schema
子句中将TABLE_NAME
更改为WHERE
。