?
Solved

Delete a record in a stored procedure

Posted on 2011-04-21
6
Medium Priority
?
238 Views
Last Modified: 2012-05-11
This is for a school project.  I'm new at this and I'm short on time.  If someone can help, I would appreciate it!
I am working in Visual Studio 2010, connected to a SQL Server database.  I have a stored procedure that keeps giving me errors

This is the procedure:
ALTER PROCEDURE spDeleteExpenses
      (
      @owner_id int,
      @date date,
      @expense_type varchar(20)
      )
AS
EXEC      ('
      DELETE FROM Expenses
      WHERE owner_id = ('+ @owner_id +')
      AND   date = ('+ @date +')
      AND   expense_type = ('+ @expense_type +')
      ');
      RETURN

This is what I see in the output window:
Running [spDeleteExpenses] ( @owner_id = 1, @date = 4/18/2011, @expense_type = Fuel ).

Operand type clash: date is incompatible with int
Invalid column name 'Fuel'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [spDeleteExpenses].

If I take out the date and expense_type parameters, it runs fine.  The columns in the database are of the same type as I have declared in the stored procedure.

0
Comment
Question by:IBC-MES
  • 4
  • 2
6 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 200 total points
ID: 35445498
Can't see why you need to use exec try :

ALTER PROCEDURE spDeleteExpenses
      (
      @owner_id int,
      @date date,
      @expense_type varchar(20)
      )
AS
      DELETE FROM Expenses
      WHERE owner_id = @owner_id
      AND   date = @date
      AND   expense_type = @expense_type

      RETURN

0
 

Author Comment

by:IBC-MES
ID: 35445570
That got rid of the errors, but didn't actually delete the row from the table.
0
 

Author Comment

by:IBC-MES
ID: 35445586
After the changes, the current output is:
Running [spDeleteExpenses] ( @owner_id = 1, @date = 4/18/2011, @expense_type = Fuel ).

No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [jhill].[spDeleteExpenses].
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35445601
That suggests no matching record found, is the date stored in the same format in the database and is the datatype of the Date column Date.

Probably worth putting some square brackets round Date as it will be a reserved word:

ALTER PROCEDURE spDeleteExpenses
      (
      @owner_id int,
      @date date,
      @expense_type varchar(20)
      )
AS
      DELETE FROM Expenses
      WHERE owner_id = @owner_id
      AND   [date] = @date
      AND   expense_type = @expense_type

      RETURN

0
 

Author Comment

by:IBC-MES
ID: 35445611
I found an something that I missed.  After following your instructions, it worked properly and deleted the row!  Thank you very much!
0
 

Author Closing Comment

by:IBC-MES
ID: 35445612
Thanks for the help!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question