473,402 Members | 2,053 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,402 software developers and data experts.

Execute Scalar

Is it OK to use ExecuteScalar if there is a possibility that the SQL it
uses may not return a value? I'm asking this because I have a table
with a particular field (call it Department) and then another field
(call it Department number). Now I want to find the last record with a
particular Department, so that for the one I am going to add, I can add
1 to the Department number I have found to give the new record the next
ascending Department number.

I am using ExecuteScalar, but in the situation where there are not yet
any members of that Department, I need to check this before assigning
the return value of ExecuteScalar. How do I do this?
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
3 7350
Mike,

In this case, the call to ExecuteScalar should return null, or DbNull,
as that is what the underlying DB is going to return to you in this
situation. Or, at least, this is what you should craft your selects to
return to you.

Also, as a side note, this seems to be a very non-scalable approach. If
you are looking to generate IDs, then you might want to keep them in a
separate table, especially if you are performing transactions. The locks
required for the transaction (especially if it becomes long running) can
have a very detrimental effect on this kind of design. If you keep the ids
in another table, then you can increment the value in that table. You can
also create a component that creates a new transaction which is separate
from any other. That way, the operation is very quick (and you don't have
to worry if the calling transaction fails, you just have to accept that your
ids might not be non-sequential).

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

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
Is it OK to use ExecuteScalar if there is a possibility that the SQL it
uses may not return a value? I'm asking this because I have a table
with a particular field (call it Department) and then another field
(call it Department number). Now I want to find the last record with a
particular Department, so that for the one I am going to add, I can add
1 to the Department number I have found to give the new record the next
ascending Department number.

I am using ExecuteScalar, but in the situation where there are not yet
any members of that Department, I need to check this before assigning
the return value of ExecuteScalar. How do I do this?
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Yes you can. You get a null object reference back if
there are no rows in the result set - or maybe DBNull I
forget which - but it's ok to do what you want to do...

--Richard
-----Original Message-----
Is it OK to use ExecuteScalar if there is a possibility that the SQL ituses may not return a value? I'm asking this because I have a tablewith a particular field (call it Department) and then another field(call it Department number). Now I want to find the last record with aparticular Department, so that for the one I am going to add, I can add1 to the Department number I have found to give the new record the nextascending Department number.

I am using ExecuteScalar, but in the situation where there are not yetany members of that Department, I need to check this before assigningthe return value of ExecuteScalar. How do I do this?
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!
.

Nov 15 '05 #3
I% SAY :::
Yes you can. You get a null object reference back if
there are no rows in the result set - or maybe DBNull I
forget which - but it's ok to do what you want to do...

--Richard


what i do is step my sproc to return -1 if there
are no matches.

-----Original Message-----
Is it OK to use ExecuteScalar if there is a possibility

that the SQL it
uses may not return a value? I'm asking this because I

have a table
with a particular field (call it Department) and then

another field
(call it Department number). Now I want to find the last

record with a
particular Department, so that for the one I am going to

add, I can add
1 to the Department number I have found to give the new

record the next
ascending Department number.

I am using ExecuteScalar, but in the situation where

there are not yet
any members of that Department, I need to check this

before assigning
the return value of ExecuteScalar. How do I do this?
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com

***
Don't just participate in USENET...get rewarded for it!
.


Nov 15 '05 #4

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

Similar topics

2
by: Mountain Man | last post by:
Hi, I have an array derived from a set of radio buttons that I want to break down into a single variable for use with a database. How can I do this? $gender is the array, and I want $gender2 to...
7
by: Steve Jorgensen | last post by:
Hi all, I've been using scalar functions as a way to perform some complex data transformation operations, and I've noticed that scalar functions reaaaaalllllyyyy sloooowwwwww thiiiiiings...
7
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop...
2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
5
by: Bob Stearns | last post by:
When I run the following query with the two sections commented out, the response time is between 1 an 2 seconds; with the first indicated section enabled, the response goes up to 15 seconds even...
4
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
5
by: Eli | last post by:
Hi, I want to check whether a value is a scalar. A scalar can be: - None (null) - string - number (integer, float) - boolean How can I validate a value is one of these types? I care about...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
2
by: jabernet | last post by:
In DB2 LUW 'till Version 9 it is not possible to execute dynamic SQL in a UDF (at least as I understand it; maybe I'm just doing something wrong). Is this planed to be included in Future Versions?...
2
by: nshishir | last post by:
In oracle, there is a performance improvement if scalar subqueries are used instead of joins. Does this hold good for Db2 (8.2) too?
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: 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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.