473,396 Members | 1,982 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,396 software developers and data experts.

SqlDataReader Stored Procedure

I have an SQL Server 2005 Express Stored Procedure .. which first I
select if that record exists, if it exists I update that record .. if
not I insert. It then selects the record with the new data updated or
inserted...

however the .NET SqlDataReader always takes the first query results
(which the query that first ran to check if that record exists) .. How
can I take the second query results?. I can use the Try Catch but that
will reduce the system performance..

thanks all!

Aug 10 '06 #1
6 3044
Varangian,

This archive thread on google will answer your question (although it
does degenerate towards the end):

http://groups.google.com/group/micro...2dbb3be315f8ec

Basically, you would create a temp table and insert into that table,
then, select from that table in the end.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Varangian" <of****@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>I have an SQL Server 2005 Express Stored Procedure .. which first I
select if that record exists, if it exists I update that record .. if
not I insert. It then selects the record with the new data updated or
inserted...

however the .NET SqlDataReader always takes the first query results
(which the query that first ran to check if that record exists) .. How
can I take the second query results?. I can use the Try Catch but that
will reduce the system performance..

thanks all!

Aug 10 '06 #2
hmmm...yes ok thanks for the reply.. however if I need to check in a
table, I need to insert all the records from the true one to the Temp
one... which means overhead over the SQL Server no?

Nicholas Paldino [.NET/C# MVP] wrote:
Varangian,

This archive thread on google will answer your question (although it
does degenerate towards the end):

http://groups.google.com/group/micro...2dbb3be315f8ec

Basically, you would create a temp table and insert into that table,
then, select from that table in the end.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Varangian" <of****@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
I have an SQL Server 2005 Express Stored Procedure .. which first I
select if that record exists, if it exists I update that record .. if
not I insert. It then selects the record with the new data updated or
inserted...

however the .NET SqlDataReader always takes the first query results
(which the query that first ran to check if that record exists) .. How
can I take the second query results?. I can use the Try Catch but that
will reduce the system performance..

thanks all!
Aug 10 '06 #3
Hi,

If the record is inserted all you may need is the generated ID, all the
other info being inserted you already have it. In this case you can use a
parameter.

If the record exists then you already have all the info you need, why are
you getting back the same info?
unless you do some transformation of the data you already have the info
being inserted .

Additionally IIRC the DataReader can access all the result sets, it does has
a NextResult that advance to the next recordset
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Varangian" <of****@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>I have an SQL Server 2005 Express Stored Procedure .. which first I
select if that record exists, if it exists I update that record .. if
not I insert. It then selects the record with the new data updated or
inserted...

however the .NET SqlDataReader always takes the first query results
(which the query that first ran to check if that record exists) .. How
can I take the second query results?. I can use the Try Catch but that
will reduce the system performance..

thanks all!

Aug 10 '06 #4
which first I select if that record exists,

OK - more SQL than C#, but:

Can I verify: so you do a select and then test @@ROWCOUNT or something?
That's a bit ropy...

You should be able to do all of this without the first select, either by
testing IF EXISTS (SELECT ...), or by treating e.g. the identity / PK
parameter as the toggle - meaning if it is NULL do an INSERT, else do an
UPDATE. You could then do a single SELECT at the bottom, which SqlDataReader
will handle happily.

Finally - why SELECT this data at all? Since you just told it the values,
your code already knows (unless this is an update of a limited set of data,
followed by a SELECT of everything including e.g. computed values - but I
don't like that design anyway ;-p). If the aim is to return e.g. the new
identity value, then a better approach is to declare the param as OUT in the
SQL (InputOutput in the C#), and then set the value to SCOPE_IDENTITY()
after the INSERT. Likewise with any Timestamp columnn you may have.

Does that help at all?

Marc
Aug 10 '06 #5
BTW, if UPDATEs vastly outnumber INSERTs, and it is suitably indexed on the
identity, then you could try (as below) being optimistic on the data already
being there (for updating) - but I must stress that I prefer toggling this
based on the @ID being e.g. 0/NULL, as the caller should know (in advance)
whether they are attempting an insert or an update, and may not expect an
"UPDATE" on an incorrect ID to perform an insert and change the current id.
I also quite like using the return value to indicate the rows affected -
which should always be 1 in this case (with NOCOUNT ON).

Marc

-- blah params... @ID int OUTPUT = NULL... blah

UPDATE ...
WHERE [ID] = @ID

IF @@ROWCOUNT = 0
BEGIN
INSERT ...

SET @ID = SCOPE_IDENTITY()
END

-- the bit I don't like anyway
SELECT ...
WHERE [ID] = @ID
Aug 10 '06 #6
Varangian,

You might be able to get away with using "select into", but either way,
you will have to store it in a temp table.

The thing is, that's to be expected. Queries either have to be
delivered back to the user, or they have to be stored in a temp cursor/table
on the server. There is no other place for them to be put.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Varangian" <of****@gmail.comwrote in message
news:11**********************@q16g2000cwq.googlegr oups.com...
hmmm...yes ok thanks for the reply.. however if I need to check in a
table, I need to insert all the records from the true one to the Temp
one... which means overhead over the SQL Server no?

Nicholas Paldino [.NET/C# MVP] wrote:
>Varangian,

This archive thread on google will answer your question (although it
does degenerate towards the end):

http://groups.google.com/group/micro...2dbb3be315f8ec

Basically, you would create a temp table and insert into that table,
then, select from that table in the end.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Varangian" <of****@gmail.comwrote in message
news:11**********************@m79g2000cwm.googleg roups.com...
>I have an SQL Server 2005 Express Stored Procedure .. which first I
select if that record exists, if it exists I update that record .. if
not I insert. It then selects the record with the new data updated or
inserted...

however the .NET SqlDataReader always takes the first query results
(which the query that first ran to check if that record exists) .. How
can I take the second query results?. I can use the Try Catch but that
will reduce the system performance..

thanks all!

Aug 10 '06 #7

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

Similar topics

0
by: Zach Corera | last post by:
If I want to return both a recordset to a SqlDataReader and an output parameter from a stored procedure to .Net, I must close the SqlDataReader first or the value of the output parameter is not...
1
by: Neo Chou | last post by:
Greetings! I met the same question as in ADO a few months ago. I'm working on MS SQL Server 2000. I have a stored procedure that returns a return value as well as a record set (by "select"...
2
by: Binny | last post by:
I have a stored procedure which returns a recordset and also returns a value. I know how to access the recordset. How can i access the integer value returned by the stored procedure Iam using...
2
by: Cameron Frasnelly | last post by:
I emulated the code from the .Net Framework help (Titled "Using Stored Procedures with a Command") and I still receive and error... Error Received = "Invalid attempt to read when no data is...
2
by: rn5a | last post by:
The following code calls a function in a DLL which returns a SqlDataReader (the function in the DLL calls a stored procedure which returns 6 columns): Dim boCart As Cart Dim sqlReader As...
6
by: rn5a | last post by:
Suppose a SQL Server 2005 stored procedure looks like this: ALTER PROCEDURE SPName @UserID int SELECT COUNT(*) FROM Table1 WHERE UserID = @UserID SELECT COUNT(*) FROM Table1 In the ASPX...
3
by: Sam | last post by:
Hi All, I have a very strange issue with ms sql stored procedure and sqlDataReader and I hope that someone can tell me what's going on . I have an asp.net application which has one of its page...
13
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the...
4
by: Cirene | last post by:
I have a sqldatareader that I use to read some data. Later I do a dr.close. In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm trying to reuse the var with a totally different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.