views:

292

answers:

4

I have a requirement to run a query against a database that will return either a zero or one (Checking for existance of specific criteria). The Tech specs I've been given for review state that I should be creating a stored procedure, that will return a single row, with a single column called "result" that will contain a bit value of 0 or 1. However, I'm not sure that a stored procedure would be the best approach, but am a little unsure so thought I'd ask for you opinions. The two options I can think of are:

1: Create a SQL scalar-valued function that performs the query and returns a bit. This could then be called directly from within the .Net client application using a "TEXT" SqlCommand object, and it would return a bool from the "ExecuteScalar()" method.

2: Create a stored procedure as described in the tech specs. This would then be called from the .Net Client app in the normal manner, and would return a DataTable with a single row and single column, that contains the bit value.

To me, option one seems the best. However, something in the back of my head is saying this isn't such a good idea.

Please could you give your opinions and help relieve my concerns? :)

Cheers, Ian

+2  A: 

The calling scalar-valued function is absolutely correct solution.

TcKs
Could you explain why?
Sk93
Because, you have limited options how to get any result from database engine. Basicaly you have only "SELECT" command. The method "ExecuteScalar" is smart and can retrieve value from scalar-valued function or constant ("SELECT @@SERVERNAME") or value of first column in first row ("SELECT A, B, C FROM MyTable").
TcKs
So, to just double-check what you're saying, executing a stored-procedure with the "sqlCommand.ExecuteScalar()" method (as shown by Barry) is the correct solution?Sorry - just want to ensure I understand precisely :)
Sk93
Commonly is better use a scalar-valued function than stored procedure. It's because scalar-valued function has strongly typed result. The one procedure can has multiple results (multiple table results and one scalar result and multiple output parameters). The stored procedures can not be used in "WHERE" clausule for example. And so on.However the procedure has more options what can do. The decision of function/procedure is depended on scenario, you want solve.
TcKs
Then I believe that a scalar-valued function is the correct answer, as this task is effectively a "FileExists" function.Thank you.
Sk93
+5  A: 

Execute the Stored Procedure using the ExecuteScalar() method. You can then cast the result of this to a boolean.

e.g

   SqlConnection con = new SqlConnection(connectionString);
    SqlCommand com = new SqlCommand("Execute dbo.usp_MyStoredProc", con);
    return (Boolean)com.ExecuteScalar();
Barry
Whilst I like this method, TcKs' comments on his answer have convinced me that, in this scenario, I shouldn't be using a stored procedure as a function, but rather a function as a function.But thanks anyway - I've learned something new from your answer regardless :)
Sk93
A: 

solving it with a stored procedure is better in the long run because it´s more flexible and adaptable

Patrick Säuerl
Better and more flexible are purely subjective based upon the logic to determine the result. Without knowing the logic, one cannot make that assertion with accuracy.
Jeff Schumacher
That´s true.I just thought if you implement it as a stored procedure, you can update the logic in multiple ways, for example you can use a scalar value function in itI see the stored procedure in this case more as a kind of wrapper
Patrick Säuerl
A: 

I suppose it depends on the logic the corresponding db function (sp/udf) has to execute.

If for e.g. we are interested in the number of times the particular db function has executed we'd definitely need to do some data manipulation and updates on various tables. Hence we'd have to go for stored procs here. If its a simple retrieval a udf will do.

deostroll