I have a stored proc that must so one of the following:
based on a varchar value, if that value exists, return the rowID of it. If it does NOT exist, create a new row and return the new rowID
I have the following code and I have not been able to get it to work. I get the error ' Syntax error converting the varchar value 'XXX' to a column of data type int.' ( XXX is the @invoiceNumber value I'm supplying )
Could someone help me figure out what I'm doing wrong? :D
Expand|Select|Wrap|Line Numbers
- [dbo].[CreateNewInvoice]
- (
- @user varchar(50)
- , @invoiceNumber varchar(50)
- )
- AS
- DECLARE @ID int;
- IF (( SELECT InventoryInvoiceNumber FROM InventoryInvoices WHERE InventoryInvoiceNumber = @invoiceNumber) > 0 )
- --return the existing id #
- BEGIN
- SELECT InventoryInvoiceID FROM InventoryInvoices WHERE InventoryInvoiceNumber = @invoiceNumber
- END
- ELSE
- --make an entry into the InventoryInvoice table first
- BEGIN
- INSERT INTO InventoryInvoices
- (
- InventoryInvoiceNumber
- , UserWhoEntered
- )
- VALUES
- (
- @invoiceNumber
- , @user
- )
- RETURN SCOPE_IDENTITY()
- END