logo
down
shadow

If-else condition in visual basic to delete a record from SQL Server if record exist


If-else condition in visual basic to delete a record from SQL Server if record exist

By : eashwar
Date : November 22 2020, 02:42 PM
Any of those help As others have already mentioned, you really need to look into parameterizing your query. You should also look to encapsulate this into a stored procedure for easier reading and maintainablity. Those things aside, you can accomplish all of this in one database call.
This snippet should produce the expected results. Please double check the data type on the SqlParameter -- I guessed what it could be.
code :
cmd = New SqlCommand("IF EXISTS(SELECT * FROM book WHERE isbn = @isbn) " _
    & " BEGIN " _
    & " delete from published_by where isbn = @isbn; " _
    & " delete from book_return where isbn = @isbn; " _
    & " delete from memberbook_issue where isbn = @isbn; " _
    & " delete from book where isbn = @isbn;" _
    & " SELECT 1; " _
    & " END " _
    & " ELSE SELECT 0", cn)

cmd.Parameters.Add(New SqlParameter("@isbn", SqlDBType.VarChar, 20) With {.Value = isbn})

cn.Open()
Dim returnValue as Integer = CInt(cmd.ExecuteScalar())
If returnValue = 1 Then
    MessageBox.Show("Record Successfully Deleted from current table & dependant table(s)", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End


Share : facebook icon twitter icon
Delete the 'first' record from a table in SQL Server, without a WHERE condition

Delete the 'first' record from a table in SQL Server, without a WHERE condition


By : Pritpal
Date : March 29 2020, 07:55 AM
With these it helps Is it possible to delete the 'first' record from a table in SQL Server, without using any WHERE condition and without using a cursor?
code :
WITH  q AS
        (
        SELECT TOP 1 *
        FROM    mytable
        /* You may want to add ORDER BY here */
        )
DELETE
FROM    q
DELETE TOP (1)
FROM   mytable
hibernate delete parent record if only no child record exist

hibernate delete parent record if only no child record exist


By : Adrian Mihai
Date : March 29 2020, 07:55 AM
seems to work fine Your catch is never executed as delete is just a "marker" call. Hibernate will not issue the queries until a flush is called. In the normal case this happens on the end of a transaction and not when you call the delete or persist methods.
Assuming your public delete method is annotated with @Transactional this method is wrapped and on exit of that method the wrapper will call commit. This is where Hibernate flushes all changes to the datasource and commits the changes. So the exception happens outside of your method.
Trying to delete a record from my access database in visual basic studio 2010

Trying to delete a record from my access database in visual basic studio 2010


By : Rama Sai
Date : March 29 2020, 07:55 AM
this will help Notes:
1)Never leave your OleDbConnection Open. Only allow it to be opened when you actually need it. This will save you from a lot of headaches later on. The reasons why can be found on following stackoverflow question.
code :
Private Sub CreatePlayerAdapter(ByVal playerDBconnection As OleDbConnection) 

    Dim myCommand As OleDbCommand

    Dim sql As String = "DELETE * FROM Players WHERE ID =" & CInt(PlayerIDTextBox.Text) 
    myCommand = New OleDbCommand(sql, playerDBconnection)

    playerDBconnection.Open()
    myCommand.ExecuteNonQuery()
    playerDBconnection.Close()
End Sub
Private Sub CreatePlayerAdapter(ByVal playerDBconnection As OleDbConnection) 

    Dim myCommand As OleDbCommand

    Dim sql As String = "DELETE * FROM Players WHERE ID = @playerId"
    myCommand = New OleDbCommand(sql, playerDBconnection)


    Dim param As New OleDb.OleDbParameter(@playerId", CInt(PlayerIDTextBox.Text))
    myCommand.Add(param)

    playerDBconnection.Open()
    myCommand.ExecuteNonQuery()
    playerDBconnection.Close()
End Sub
Delete if record exists and Insert if record doesn't exist

Delete if record exists and Insert if record doesn't exist


By : Ігор Сосновський
Date : March 29 2020, 07:55 AM
this will help In my for loop, I'm writing an Insert and delete query which does the following:
code :
valsql2 = "INSERT INTO CompanyReportListTable(CompanyID, ReportID) Values (valCompanyId, rpti) WHERE NOT EXISTS(SELECT * FROM CompanyReportListTable WHERE ReportID <> rpti and companyid <> valCompanyId and " & chk.Checked & "= 1)"
Delete rows from SQL Server database by using BETWEEN condition just delete one record (the last one in range)

Delete rows from SQL Server database by using BETWEEN condition just delete one record (the last one in range)


By : Marvin Padilla
Date : March 29 2020, 07:55 AM
this will help Your trigger is only changing one row. In SQL Server, triggers are set operations, so you should be thinking as sets:
code :
ALTER TRIGGER [dbo].[TrgDltDate] ON [dbo].[Date] INSTEAD OF DELETE
AS
BEGIN
    UPDATE dbo.Date
        SET DLT = 1,
        LastModificationDate = GETDATE()
    WHERE DateID IN (SELECT d.DateID FROM Deleted d)
END;
Related Posts Related Posts :
  • SQl server Dynamic query
  • How can I make the legend in an SSRS chart correspond with custom chart fill colors?
  • The MSDTC transaction manager was unable to push the transaction to the destination transaction manager due to communica
  • How to retrieve all rows with a date in this and the next week? SQL Server
  • Visual Studio + localdb on windows 10
  • pymssql : How to get distinct rows when a column has XML values
  • How to set up a Join according to a value for one Column?
  • How to insert specific datetime into dataset
  • Rolling calendar pivot in sql server?
  • Subtract variable from column while variable has remaining value
  • Error 0x80070005 Access is denied
  • Part of the row to columns
  • Sql Server Updating column by generating random number
  • Delete Rows not between Min and Max dates from another column
  • How select data where they have on second difference?
  • update the table with the previous column with out any loops
  • Output complex xml from SQL select query
  • Can not insert arabic data in SQL Server
  • Joining SQL Tables and display columns as rows to generate a report
  • Reduce Hash Match Cost in sql code
  • Data Insertion from one sql server to another
  • Return a Hourly minimum count of column?
  • SQL Query - Year by Year Growth of Sales
  • LINQ & SQL Server : order by from starting row
  • creating access to SQL Server Native Client OLE DB provider using CoCreateInstance fails with 80040154 error
  • SSIS SQL task Query creating error while casting parameters (string to Datetime)
  • run query against multiple databases on different server
  • Find all rows that have changed and write old-Newvalue to different table in SQL Server 2012
  • SQL Sum() Query error
  • How child record overrides parent record columns?
  • Calculate Type of the member in a table (Self Join/Case When or any other possible way)
  • Employee, Departments, and ManagerID Table Structures
  • Stored procedures in SQL Server 2012
  • How retrieve collection of objects from stored procedure which are in database connected with foreign key
  • Microsoft SQL Server 2014 no server
  • How to SUM up 2 Columns into 1 Column SQL Server
  • Best approach to merge three distinct TSQL select statments
  • How to display multiple data from the same table depending on one column value
  • How to create sequential numbering against two select statements that are unioned?
  • SQL Server : Second maximum value per group
  • T-SQL : xml how to add default namespace and root path with inner join
  • T-SQL - Using a parameter in where and clause
  • SSPI and SQL Server Windows Authentication
  • Do I NEED a subquery on this OVER example?
  • Check constraint to allow only one combination of two columns
  • How to id with foreign key in my table in SQL Server?
  • Unpivoting multiple columns displaying incorrect data and duplicates
  • Unable to store value in a variable
  • Wildcard in ContainsTable of full-text search
  • SQL count where between dates by month
  • Datatier applications not deploying filegroups on creation of database, only upgrade process
  • Azure SQL Server VM OR Azure Windows Server with SQL Server install
  • Convert from 4 digit Military time to Standard time
  • SQL inner join on Microsoft SQL server
  • Deny Read-Only Users to see DDL of a Stored Procedure MS SQL
  • How do I exclude rows when an incremental value starts over?
  • TSQL - compare adjacent rows
  • MSSQL DataBase Backup without a specific table
  • Find All Empty Tables Named MAP_ALERT In All Databases On Server
  • Ugly table query
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co