Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jaeden99
Starting Member
2 Posts |
Posted - 2006-01-27 : 17:10:38
|
| Is there a way to delete records from table passing parameter as tablename? I won't to delete all records from a table dependent on table selected. i'm trying to do this with stored procedure...Table to delete depends on the checkbox selected.Current code(works)Public Function DelAll() MZKDB = MZKHRFin If sloption = "L" Then sqlConn.ConnectionString = "Server=" & MZKSrv & ";Initial Catalog=" & MZKDB & ";Integrated Security=SSPI;" ElseIf sloption = "S" Then sqlConn.ConnectionString = "Server=" & MZKSrv & ";User id=sa;Password=" & MZKPswd & "; Initial Catalog=" & MZKDB & ";" End If sqlConn.Open() sqlTrans = sqlConn.BeginTransaction() sqlCmd.Connection = sqlConn sqlCmd.Transaction = sqlTrans Try sqlCmd.CommandText = sqlStr sqlCmd.ExecuteNonQuery() sqlTrans.Commit() frm2.txtResult.Text = frm2.txtResult.Text & " " & TableName & " Prior records have been deleted from the database." & vbCrLf SetCursor() Catch e As Exception Try sqlTrans.Rollback() Catch ex As SqlException If Not sqlTrans.Connection Is Nothing Then frm2.txtResult.Text = frm2.txtResult.Text & " " & TableName & " An exception of type " & ex.GetType().ToString() & " was encountered while attempting to roll back the transaction." & vbCrLf SetCursor() End If End Try frm2.txtResult.Text = frm2.txtResult.Text & " " & TableName & " Records were NOT deleted from the database." & vbCrLf SetCursor() Finally sqlConn.Close() End Try ResetID() End FunctionIf cbGenFY.Checked Then sqlStr = "DELETE FROM FIN_FiscalYear" TableName = "dbo.FIN_FiscalYear" DelAll() ClearCounts() timeStepStart = Date.Now GenFY() timeStepStop = Date.Now DispOneCounts() End If If cbGenFund.Checked Then sqlStr = "DELETE FROM FIN_Fund" TableName = "dbo.FIN_Fund" DelAll() ClearCounts() timeStepStart = Date.Now GenFund() timeStepStop = Date.Now DispOneCounts() End If If cbGenFunc.Checked Then sqlStr = "DELETE FROM FIN_Function" TableName = "dbo.FIN_Function" DelAll() ClearCounts() timeStepStart = Date.Now GenFunc() timeStepStop = Date.Now DispOneCounts() End If If cbGenObject.Checked Then sqlStr = "DELETE FROM FIN_Object" TableName = "dbo.FIN_Object" DelAll() ClearCounts() timeStepStart = Date.Now GenObject() timeStepStop = Date.Now DispOneCounts() End If If cbGenCenter.Checked Then sqlStr = "DELETE FROM FIN_Center" TableName = "dbo.FIN_Center" DelAll() ClearCounts() timeStepStart = Date.Now GenCenter() timeStepStop = Date.Now DispOneCounts() End If If cbGenProject.Checked Then sqlStr = "DELETE FROM FIN_CodeBook" TableName = "dbo.FIN_CodeBook" DelAll() sqlStr = "DELETE FROM FIN_BudgetAccnt" TableName = "dbo.FIN_BudgetAccnt" DelAll() sqlStr = "DELETE FROM FIN_Budget" TableName = "dbo.FIN_Budget" DelAll() sqlStr = "DELETE FROM FIN_Project" TableName = "dbo.FIN_Project" DelAll() ClearCounts() timeStepStart = Date.Now GenProject() timeStepStop = Date.Now TableName = "dbo.FIN_Project" DispOneCounts() End If If cbGenProgram.Checked Then sqlStr = "DELETE FROM FIN_Program" TableName = "dbo.FIN_Program" DelAll() ClearCounts() timeStepStart = Date.Now GenProgram() timeStepStop = Date.Now DispOneCounts() End If If cbGenGL.Checked Then sqlStr = "DELETE FROM FIN_gl" TableName = "FIN_gl" DelAll() ClearCounts() timeStepStart = Date.Now GenGL() timeStepStop = Date.Now DispOneCounts() End If If cbGenRevenue.Checked Then sqlStr = "DELETE FROM FIN_Revenue" TableName = "FIN_Revenue" DelAll() ClearCounts() timeStepStart = Date.Now GenRevenue() timeStepStop = Date.Now DispOneCounts() End If If cbGenBank.Checked Then sqlStr = "DELETE FROM FIN_VendorBankAccnt" TableName = "dbo.FIN_VendorBankAccnt" DelAll() sqlStr = "DELETE FROM FIN_VendorBank" TableName = "dbo.FIN_VendorBank" DelAll() sqlStr = "DELETE FROM FIN_bankAdd" TableName = "dbo.FIN_bankAdd" DelAll() sqlStr = "DELETE FROM FIN_bankTERMS" TableName = "dbo.FIN_bankTerms" DelAll() sqlStr = "DELETE FROM FIN_bank" TableName = "dbo.FIN_bank" DelAll() ClearCounts() timeStepStart = Date.Now GenBank() timeStepStop = Date.Now TableName2 = "dbo.FIN_bankTERMS" TableName3 = "dbo.FIN_BankAdd" TableName4 = "dbo.FIN_VendorBank" TableName5 = "dbo.FIN_VendorBankAccnt" DispTwoCounts() End If If cbFinAP.Checked Then sqlStr = "DELETE FROM FIN_Period" TableName = "FIN_Period" DelAll() ClearCounts() timeStepStart = Date.Now GenPeriod() timeStepStop = Date.Now DispOneCounts() End If If cbFinVM.Checked Then sqlStr = "DELETE FROM FIN_vendorClass" TableName = "FIN_vendorClass" DelAll() sqlStr = "DELETE FROM FIN_vendorAdd" TableName = "FIN_vendorAdd" DelAll() sqlStr = "DELETE FROM FIN_vendor" TableName = "FIN_vendor" DelAll() sqlStr = "DELETE FROM FIN_AddressType" TableName = "FIN_AddressType" DelAll() sqlStr = "DELETE FROM FIN_VendorStatus" TableName = "FIN_VendorStatus" DelAll() sqlStr = "DELETE FROM States" TableName = "States" DelAll() sqlStr = "DELETE FROM Country" TableName = "Country" sqlStr = "DELETE FROM FIN_IndustrialCodes" TableName = "FIN_IndustrialCodes" DelAll() ClearCounts() timeStepStart = Date.Now GenIndCodes() timeStepStop = Date.Now DispOneCounts() DelAll() ClearCounts() timeStepStart = Date.Now FinVendStat() timeStepStop = Date.Now TableName = "FIN_VendorStatus" DispOneCounts() ClearCounts() timeStepStart = Date.Now FinAddrType() timeStepStop = Date.Now TableName = "FIN_AddressType" DispOneCounts() ClearCounts() timeStepStart = Date.Now GenCountry() timeStepStop = Date.Now TableName = "Country" DispOneCounts() ClearCounts() timeStepStart = Date.Now GenState() timeStepStop = Date.Now TableName = "States" DispOneCounts() ClearCounts() timeStepStart = Date.Now FinVM() timeStepStop = Date.Now TableName = "FIN_Vendor" TableName2 = "FIN_VendorAdd" DispTwoCounts() End If If cbFinbudget.Checked Then sqlStr = "DELETE FROM FIN_BudgetAccnt" TableName = "FIN_BudgetAccnt" DelAll() sqlStr = "DELETE FROM FIN_Budget" TableName = "FIN_Budget" DelAll() sqlStr = "DELETE FROM FIN_CodeBook" TableName = "FIN_CodeBook" DelAll() ClearCounts() TableName = "FIN_Budget" timeStepStart = Date.Now FinBudget() timeStepStop = Date.Now DispOneCounts() ClearCounts() TableName = "FIN_Codebook" TableName2 = "FIN_budgetAccnt" timeStepStart = Date.Now FinCodeBook() timeStepStop = Date.Now DispTwoCounts() ClearCounts() End If |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-28 : 02:25:02
|
"Is there a way to delete records from table passing parameter as tablename?"Something like this?CREATE PROCEDURE dbo.usp_DeleteAllInTable @strTableName varchar(8000)ASDECLARE strSQL nvarchar(4000)SELECT @strSQL = 'DELETE FROM dbo.[' + @strTableName + ']'EXEC (@strSQL)GO Do NOT pass it the wrong table name by mistake!You might be able to use TRUNCATE TABLE instead of DELETE, which will be faster and use less log space. But it will fail if the table has Foreign Key constraints.Kristen |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-28 : 02:43:13
|
| >> But it will fail if the table has Foreign Key constraints.But this doesn't mean DELETE FROM table_name won't. It will also failed on same constaints----------------------------------'KH'I come I saw I post |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-28 : 02:51:31
|
| Yeah, but you can alter the sequence you do your deletes to work around that. TRUNCATE TABLE will still fail if there are FK Constrains - even if there are no conflicting FK valuesKristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-30 : 00:55:05
|
| http://sqljunkies.com/WebLog/madhivanan/archive/2005/11/25/17459.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|