473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Returning an existing column OR the scope_identity

I'm having a seriously hard time trying to figure this one out.

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
  1.  [dbo].[CreateNewInvoice]
  2. (
  3.       @user varchar(50)
  4.     , @invoiceNumber varchar(50)
  5. )
  6.  
  7. AS
  8.  
  9.  
  10. DECLARE @ID int;
  11.  
  12.         IF (( SELECT InventoryInvoiceNumber FROM InventoryInvoices WHERE InventoryInvoiceNumber = @invoiceNumber) > 0  )
  13.         --return the existing id #
  14.             BEGIN
  15.                 SELECT InventoryInvoiceID FROM InventoryInvoices WHERE InventoryInvoiceNumber = @invoiceNumber
  16.             END
  17.  
  18.         ELSE
  19.         --make an entry into the InventoryInvoice table first
  20.             BEGIN
  21.                 INSERT INTO InventoryInvoices
  22.                 (
  23.                       InventoryInvoiceNumber
  24.                     , UserWhoEntered
  25.                 )
  26.                 VALUES
  27.                 (
  28.                       @invoiceNumber
  29.                     , @user
  30.                 )
  31.                 RETURN SCOPE_IDENTITY()
  32.  
  33.             END
Feb 19 '07 #1
0 835

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Member | last post by:
Hi All. Maybe someone in here could help on this too.... Uusally I can return a value from a stored procedure without any problem. Today I ran into something I cannot figure out. ...
4
by: Scrappy | last post by:
I have an ASP front end on SQL 2000 database. I have a form that submits to an insert query. The entry field is an "identity" and the primary key. I have used scope_identity() to display the...
4
by: vassone | last post by:
Dear All, My newly insert GUID is not return from a store procedure. I turned this around and around and can't understand why. The records are inserted but no GUID is returned. I use...
2
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name)...
6
by: Hardy Wang | last post by:
Hi all, I have the following codes, but SCOPE_IDENTITY() just returns NULL to me. If I comment out SCOPE_IDENTITY() line and run @@IDENTITY line, it works fine!! Since I have a trigger on the...
3
by: Dylan Parry | last post by:
Hi folks, This is really confusing me. I am trying to insert a row into a database, and then use scope_identity() to return the value of the ID column for the record I have just created. The...
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
1
by: David Lozzi | last post by:
Howdy, ASP.Net 2.0 using VB on SQL 2005 This is a two fold issue. I have a DetailsView control which users can insert or edit items. Editing works great. Insert works great however I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.