How to check if value is inserted successfully or not?
You can use @@ROWCOUNT
server variable immediately after the insert query to check number of affected rows by the insert operation.
declare @fName varchar(50) = 'Abcd',
@lName varchar(50) = 'Efgh'
INSERT INTO myTbl(fName,lName) values(@fName,@lName)
PRINT @@ROWCOUNT --> 0- means no rows affected/nothing inserted
--> 1- means your row has been inserted successfully
For your requirement, you could use a Case
statement(as per comment):
--If you need @check as a bit type please change Int to bit
DECLARE @check Int = CASE WHEN @@ROWCOUNT = 0 THEN 1 ELSE 0 END
Check if record inserted successfully
Like stated in MSDN :
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
int a = cmd.ExecuteNonQuery();
if (a > 0) {
//at least one record has been affected
}
Validate if the insert was completed successfully
You can use @@ROWCOUNT
after your insert statement. it Returns the number of rows affected by the last statement.
if it returns grater than 0
it means your rows has been inserted successfully
SQL syntax for checking to see if an INSERT was successful?
I would use try/catch myself
begin try
insert query
print message
end try
begin catch
print message
end catch
You should be able to take it from here.
How to check whether the value is inserted or not in SQLite?
Check By DataBase : https://stackoverflow.com/a/21151598/8448886
You can also use Log to check data is inserted on or not on your DataBaseHelper class.
public boolean addData(String name , String sku, String upc , String price, String displaySize, int displaySizeYes, String status){
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Constants.NAME, name );
contentValues.put(Constants.SKU, sku );
contentValues.put(Constants.UPC, upc );
contentValues.put(Constants.PRICE, price );
contentValues.put(Constants.DISPLAY_SIZE, displaySize );
contentValues.put(Constants.DISPLAY_SIZE_YES, displaySizeYes );
contentValues.put(Constants.STATUS, status );
long result = db.insert(Constants.TABLE_NAME,null,contentValues);
Log.e(TAG,""+upc+" Inserted");
if(result == -1) {
return false;
}else{
// Log.e(TAG,"value inserted");
return true;
}
}
How do I check that values are being inserted into a table when the result of the insert shows nothing?
Seems like what you likely need is a couple of OUTPUT
clauses and some table variables:
ALTER PROCEDURE [dbo].[StoreDetails] @FirstName varchar(50),
@Surname varchar(50),
@Password varchar(100),
@PhoneNumber varchar(50),
@Email varchar(100),
@IsAdmin bit,
@Address varchar(100),
@DOB date
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Salt uniqueidentifier = NEWID();
DECLARE @AddressID table (ID int);
DECLARE @DOBID table (ID int);
INSERT INTO dbo.OAddress ([Address])
OUTPUT inserted.ID
INTO @AddressID (ID) --Guessed named for inserted
SELECT @Address;
INSERT INTO dbo.ODOB (DOB)
OUTPUT inserted.ID
INTO @DOBID (ID) --Guessed named for inserted
SELECT @DOB;
INSERT INTO dbo.OUsers (FirstName,
Surname,
Password,
Salt,
PhoneNumber,
Email,
IsAdmin,
AddressID,
DOBID)
SELECT @FirstName,
@Surname,
@Password,
@Salt,
@PhoneNumber,
@Email,
@IsAdmin,
A.ID,
D.ID
FROM @AddressID A
CROSS JOIN @DOBID D;
END;
Note the comments I make on the OUTPUT
clauses.
How to Check Data is Inserted Successfully in Firebase in Array
How can I check data has been inserted successfully?
Try this
$query1 = '...';
$query2 = '...';
$query3 = '...';
if(mysql_query($query1)) {
if(mysql_query($query2)) {
if(mysql_query($query3)) {
echo "success";
}
else { echo "error"; }
}
else { echo "error"; }
}
else { echo "error"; }
Related Topics
How to Check If Field Is Null or Empty in MySQL
How to Delete a MySQL Record After a Certain Time
How to Get Textual Contents from Blob in Oracle SQL
Search Text in Fields in Every Table of a MySQL Database
Laravel Update Multiple Records At Once
Fill a Field With the Values from Another Field in Ms Access
Way to Find Data of a SQL Table With Same Status for Consecutive 3 Days
How Two Join to Tables Based on the Highest Value in One of Them
How to Tell If a Value Is Not Numeric in Oracle
How to Identify Rows Where Two Columns Have Match Exactly in SQL Server
Getting Student Name With Highest Total Mark in SQL
Best Way to Iterate Through Columns in a SQL Table
Sql Query to Get the Employee Name and Their Manager Name from the Same Table
Select to Get Rows Based on Minimum Value of a Column
How to Select True/False Based on Column Value
Row Numbers in Query Result Using Microsoft Access
Copy and Insert to Same Table No Duplication and With Minor Changes to Value
Select Multiple Columns Count One Column and Group by One Column in One Table