I was given this function
CREATE FUNCTION [dbo].[GET_WEBGIS_ISSUE_NUM]
()
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @v_new_num int, @v_new_issue_num varchar(50);
SET @v_new_num = (SELECT COUNT(*) + 1
FROM [dbo].[WEBGIS_ISSUE]
WHERE [ISSUE_NUM] LIKE CONCAT(FORMAT(GETDATE(), 'yyMM'), '%'));
IF @v_new_num < 10
SET @v_new_issue_num = CONCAT(FORMAT(GETDATE(), 'yyMM'), '00', @v_new_num);
ELSE IF @v_new_num < 100
SET @v_new_issue_num = CONCAT(FORMAT(GETDATE(), 'yyMM'), '00', @v_new_num);
ELSE
SET @v_new_issue_num = CONCAT(FORMAT(GETDATE(), 'yyMM'), @v_new_num);
RETURN @v_new_issue_num
END;
I tried calling it from the following C# code
SqlConnection cnn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "[NEPS].[dbo].[GET_WEBGIS_ISSUE_NUM]";
//add any parameters the stored procedure might require
if (cmd.Connection.State == ConnectionState.Closed) //cmd.Connection.Open();
{
cnn.Open();
var o = cmd.ExecuteScalar();
//blabla
cnn.Close();
}
but when I debug the code, I kept on receiving null.
Notes: the connection is ok, it is connected, when I tried changing the function’s name it yields an error and when I checked through the SQL Server it also returns an appropriate return value.
2
Answers
It is more common to use a
SELECT
statement to return a scalar function result. When you useEXECUTE
(due toCommandType.StoredProcedure
), you need to also specify a return parameter and retrieve the result from the parameter after execution:As noted in the comments to your question, consider the concurrency implications of this approach. Duplicate values will be returned until the row count changes.
You’re treating a scalar function as a stored procedure, which is the wrong type for this type of execution. You need to ‘CommandType.Text’ with scalar functions.
Other notes on the
C#
part :using
blocks withSqlConnection
andSqlCommand
(let the using clause handles the dispose and close connection parts for you).const string
Here is the C# code :
For the function
GET_WEBGIS_ISSUE_NUM
perhaps you can avoid the extraIF
s with this line :