473,385 Members | 1,461 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.

select and insert in one sp

I want to write a single sp where I pass in the column values and if
it finds an exact match it returns the refid of that match else if it
doesn't find it, it adds a new row using the passed in values and
returns the RefID (primary key) of the new row (identity). I only
want one return value, being the RefID of the found or new row.

I currently am doing a select and then testing the @@ROWCOUNT. If < 1
I do an INSERT and return the identity. It doesn't give the desired
results.

Thanks,
RickN
Jul 20 '05 #1
5 1655
Something like this maybe?

SET @refid =
(SELECT refid
FROM YourTable
WHERE col1 = @col1
AND col2 = @col2, ...
)

IF @refid IS NULL
INSERT INTO YourTable (col1, col2, ...)
VALUES (@col1, @col2, ...)

SET @refid = SCOPE_IDENTITY()

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2
Hi

Posting your code would show exactly what you are doing, but at a guess you
are not calling the SCOPE_IDENTITY() function (@@IDENTITY on versions of SQL
server prior to 2000) to return the Identity value inserted.

John

"Rick" <ri**@abasoftware.com> wrote in message
news:28**************************@posting.google.c om...
I want to write a single sp where I pass in the column values and if
it finds an exact match it returns the refid of that match else if it
doesn't find it, it adds a new row using the passed in values and
returns the RefID (primary key) of the new row (identity). I only
want one return value, being the RefID of the found or new row.

I currently am doing a select and then testing the @@ROWCOUNT. If < 1
I do an INSERT and return the identity. It doesn't give the desired
results.

Thanks,
RickN

Jul 20 '05 #3
"David Portas" <RE****************************@acm.org> wrote in message news:<fK********************@giganews.com>...
Something like this maybe?

SET @refid =
(SELECT refid
FROM YourTable
WHERE col1 = @col1
AND col2 = @col2, ...
)

IF @refid IS NULL
INSERT INTO YourTable (col1, col2, ...)
VALUES (@col1, @col2, ...)

SET @refid = SCOPE_IDENTITY()


Thanks, this set me off in the right direction.
Added a @RefID OUTPUT parameter (not sure if this is the preferred method)
Changed the above slightly to :
Set @RefID = (SELECT ....
IF @RefID IS NULL
BEGIN
INSERT INTO...
SET @RefID = SCOPE_IDENTITY()
END
SELECT @RefID
This gives me the results I wanted.
If there are any added improvements, let me know.
Thanks again.
RickN
Jul 20 '05 #4
ri**@abasoftware.com (Rick) wrote in
news:28*************************@posting.google.co m:
"David Portas" <RE****************************@acm.org> wrote in
message news:<fK********************@giganews.com>...
Something like this maybe?

SET @refid =
(SELECT refid
FROM YourTable
WHERE col1 = @col1
AND col2 = @col2, ...
)

IF @refid IS NULL
INSERT INTO YourTable (col1, col2, ...)
VALUES (@col1, @col2, ...)

SET @refid = SCOPE_IDENTITY()


Thanks, this set me off in the right direction.
Added a @RefID OUTPUT parameter (not sure if this is the preferred
method) Changed the above slightly to :
Set @RefID = (SELECT ....
IF @RefID IS NULL
BEGIN
INSERT INTO...
SET @RefID = SCOPE_IDENTITY()
END
SELECT @RefID
This gives me the results I wanted.
If there are any added improvements, let me know.
Thanks again.
RickN


If @RefID is an output parameter, then your final SELECT @RefID is
superfluous. You're getting your result from the output parameter; no
need to return a rowset as well.
Jul 20 '05 #5
Ross Presser (rp******@NOSPAM.imtek.com.invalid) writes:
If @RefID is an output parameter, then your final SELECT @RefID is
superfluous. You're getting your result from the output parameter; no
need to return a rowset as well.


And to add to this: for a single scalar value in this case, you should
use an output parameter, and produce a result set. There is usually lower
overhead with a parameter. For an occasional call it may not matter, but
if you are making lots of them, there can be a considerable difference.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Chirico | last post by:
I found the following interesting and wanted to pass it along Reference (TIP 12): http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download Connect and Select from Java //...
11
by: Jeff Sandler | last post by:
I need a MySQL select statement as part of a PHP script. I want to find rows where a certain column either starts with or equals a user-supplied string. The string will be 1 or more characters in...
2
by: Eric | last post by:
please help to select these rows from these tables my tables are table1 table1Id groupId table2id price 1 1 1 10 2 1 3 1000 3 1 ...
7
by: Sunny K | last post by:
Hi guys, whilst working on a project which I thought was nearly complete I have come across a problem which was some how over seen, which I am hoping one of you guys know how to resovle. ...
9
by: Mike R | last post by:
Hi, I cant figure out how to do this.... for example: Select name from mytab order by col1 could return Mike
0
by: jtocci | last post by:
I'm having a big problem with CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE when I want to INSERT several (20~50) records based on a single INSERT to a view. Either I get a 'too...
3
by: Shelby Cain | last post by:
The select statements return different data for most_commons_vals depending on whether n_distinct is included in the select clause or not. I only seem to get the behavior below against int8...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
2
by: unabogie | last post by:
I have a table with entries tied to a membership database. The problem is that I want to select a limit of sixteen entries per member, per day, where some members have 16+ entries per day. I...
5
by: Lennart | last post by:
I really like the construction: select * from new table (update ....) X but I noticed that it cant be used as: insert into T select * from new table (update ....) X because of:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.