How to subtact previous value from recent value that value displayed in another column
in my database i have 4 columns
actually my task is differnt so iam taking some example
i have to insert data like
EmpID,EMpname,Sal,Expectedsal
when i insert data first like sal 300
the table should be
Empid,name,sal,expsal
1 k 300 0
again wheni insert data like sal 500
table should be
EMpid,name,sal,expsal
1 k 500 0
2 k 300 200
if their more than 500 records how to write storedprocedure
plz help me
Loading

Guest UserRank #1,422 · 1.2K reputationPosted Jul 25, 2011, 7:32 AM EDT
In that case you need to follow Zoran's example, because you do need to go through the table and find the MAX(empid).
New table DDL:
CREATE TABLE [dbo].[Karthik](
[Empid] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[sal] [money] NOT NULL,
[expsal] [money] NOT NULL
) ON [PRIMARY]
New proc:
ALTER PROCEDURE InsertSalary
-- Add the parameters for the stored procedure here
@name varchar(50),
@newsal money
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @maxempid int
DECLARE @lastsal money
SELECT @maxempid = MAX(empid) FROM Karthik
IF (@maxempid IS NOT NULL)
BEGIN
SELECT @lastsal = sal FROM Karthik WHERE empid = @maxempid
INSERT INTO Karthik (name, sal, expsal)
SELECT @name, @lastsal, @newsal - @lastsal FROM Karthik WHERE empid = @maxempid
UPDATE Karthik SET sal = @newsal WHERE empid = @maxempid
END
ELSE
BEGIN
-- this is a new employee record, so insert it
INSERT INTO Karthik (name, sal)
VALUES (@name, @newsal)
-- default value is already set for expsal, no need to insert it here
END
END
GO
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 5:38 AM EDT
Zoran
karthik parcha150 reputationPosted Jul 25, 2011, 5:51 AM EDT
if recent value lessthan present value then value should be displayed in anothercolumn and i have to and update the previous record's DELTA_CONTACTS' column with that value.
CustometID,Sendmessage,Description,COntacts,Delta_contacts(delta means difference)
1 karthik Hyd 20 0
2 parcha hyd 10 10
and i have to and update the previous record's DELTA_CONTACTS' column with that value.
plz can u write query for this iam tryng 2hrs but iam to unable getting that query
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 5:54 AM EDT
Zoran
karthik parcha150 reputationPosted Jul 25, 2011, 5:57 AM EDT
CustometID,Sendmessage,Description,COntacts,Delta_contacts(delta means difference) this is my actual table
i just take the example of empployee
My database is Sqlserver
Thanks,
Karthik
Guest UserRank #1,422 · 1.2K reputationPosted Jul 25, 2011, 6:10 AM EDT
CREATE TABLE [dbo].[Karthik](
[Empid] [int] NOT NULL,
[name] [varchar](50) NOT NULL,
[sal] [money] NOT NULL,
[expsal] [money] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Karthik] ADD CONSTRAINT [DF_Karthik_expsal] DEFAULT ((0.0)) FOR [expsal]
GO
This proc will handle the substractions:
ALTER PROCEDURE InsertSalary
-- Add the parameters for the stored procedure here
@empid int,
@name varchar(50),
@newsal money
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT 1 FROM Karthik WHERE empid = @empid
IF (@@ROWCOUNT = 1)
BEGIN
-- record already exists for this employee, so update it
UPDATE Karthik SET expsal = @newsal - sal
WHERE Empid = @empid
END
ELSE
BEGIN
-- this is a new employee record, so insert it
INSERT INTO Karthik (Empid, name, sal)
VALUES (@empid, @name, @newsal)
-- default value is already set for expsal, no need to insert it here
END
END
GO
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 6:14 AM EDT
CREATE PROCEDURE InsertRow(
@SendMessage VARCHAR(100),
@Description VARCHAR(100),
@Contacts INT)
AS
BEGIN
DECLARE @lastID INT
SELECT @lastID=MAX(CustomerID) FROM Customer;
IF (@lastID IS NOT NULL)
UPDATE Customer SET Delta_contacts = @Contacts - Contacts WHERE CustomerID=@lastID;
INSERT INTO Customer(SendMessage, Description, Contacts, Delta_contacts) VALUES (@SendMessage, @Description, @Contacts, 0)
END
In this script change VARCHAR(100) declarations with appropriate declarations from your database, and change table name from Customer to whatever it is in your database.
Zoran
karthik parcha150 reputationPosted Jul 25, 2011, 6:17 AM EDT
Thanq i will try it if their any doubtsi will contact you
Thanks,
karthik
karthik parcha150 reputationPosted Jul 25, 2011, 6:18 AM EDT
Thanq i will try it if their any doubts i will contact you
Thanks,
karthik
Guest UserRank #1,422 · 1.2K reputationPosted Jul 25, 2011, 6:19 AM EDT
To call the proc:
exec InsertSalary 1, 'k', 300
results:
Empid name sal expsal
1 k 300.00 0.00
calling it again with a new salary:
exec InsertSalary 1, 'k', 500
results:
Empid name sal expsal
1 k 300.00 200.00
karthik parcha150 reputationPosted Jul 25, 2011, 6:22 AM EDT
if recent value lessthan present value then value should be displayed in anothercolumn and i have to and update the previous record's DELTA_CONTACTS' column with that value.
CustometID,Sendmessage,Description,COntacts,Delta_contacts(delta means difference)
1 karthik Hyd 20 0
2 parcha hyd 10 10
and i have to and update the previous record's DELTA_CONTACTS' column with that value.
karthik parcha150 reputationPosted Jul 25, 2011, 6:26 AM EDT
SELECT 1 FROM Karthik WHERE empid = @empid
in this query you write 1 from table name i didnt understand
why you have write 1 here
i want to update based on ID
so plz can u clarify my doubt
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 25, 2011, 6:30 AM EDT
CREATE PROCEDURE InsertRow(
@SendMessage VARCHAR(100),
@Description VARCHAR(100),
@Contacts INT)
AS
BEGIN
DECLARE @lastID INT
DECLARE @lastContacts INT
SELECT @lastID=MAX(CustomerID) FROM Customer;
SELECT @lastContacts=Contacts FROM Customer WHERE CustomerID=@lastID
IF (@lastID IS NOT NULL AND @lastContacts < @contacts)
UPDATE Customer SET Delta_contacts = @Contacts - Contacts WHERE CustomerID=@lastID;
INSERT INTO Customer(SendMessage, Description, Contacts, Delta_contacts) VALUES (@SendMessage, @Description, @Contacts, 0)
END
This procedure will not update last record if its Contacts is not smaller than new value. However, new record will be inserted every time.
Zoran
karthik parcha150 reputationPosted Jul 25, 2011, 6:41 AM EDT
Delta_contacts invalid columns
if u dont mind plz can u explain your storedprocedure
Guest UserRank #1,422 · 1.2K reputationPosted Jul 25, 2011, 6:46 AM EDT
You can't have a SELECT statement without fields, so that's why I have "SELECT 1". The "1" is just a dummy constant value.
Some people use aggregate functions like COUNT() or MAX() but this is not as efficient because you're forcing the db engine to go through the entire table counting or calculating values, when all you really need to know is if the row exists or not.
karthik parcha150 reputationPosted Jul 25, 2011, 6:48 AM EDT
Awesome Explanation
Thanks,
Karthik
Guest UserRank #1,422 · 1.2K reputationPosted Jul 25, 2011, 6:48 AM EDT
ALTER PROCEDURE InsertSalary
-- Add the parameters for the stored procedure here
@empid int,
@name varchar(50),
@newsal money
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS (SELECT 1 FROM Karthik WHERE empid = @empid)
BEGIN
-- record already exists for this employee, so update it
UPDATE Karthik SET expsal = @newsal - sal
WHERE Empid = @empid
END
ELSE
BEGIN
-- this is a new employee record, so insert it
INSERT INTO Karthik (Empid, name, sal)
VALUES (@empid, @name, @newsal)
-- default value is already set for expsal, no need to insert it here
END
END
GO
karthik parcha150 reputationPosted Jul 25, 2011, 6:54 AM EDT
Actually iam subtracting only column i.e Sal column
Example:
1 K 500 0
2 k 300 200
In your procedure you have talen
where Expsal=@newsal - sal
why you have written like this can u tell plzzzz
karthik parcha150 reputationPosted Jul 25, 2011, 7:48 AM EDT
thanq for ur patience i will check it now you and zoran helping me a lot
thanq you very much
Guest UserRank #1,422 · 1.2K reputationPosted Jul 27, 2011, 4:01 AM EDT
.
.
.
IF (@maxempid IS NOT NULL)
BEGIN
SELECT @lastsal = sal FROM Karthik WHERE empid = @maxempid
INSERT INTO Karthik (name, sal, expsal)
VALUES (@name, @lastsal, @newsal - @lastsal)
UPDATE Karthik SET sal = @newsal WHERE empid = @maxempid
END
ELSE
.
.
.