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

Explicit Parameters for Stored Procedures

I created a test to check the execution time difference between executing a
SQL Server stored procedured using explicit parameters versus not. In one
case I created new SqlParameters in the code, and added the parameters to
the SqlParametersCollection of the SqlCommand object. In the second, I just
made it all into long execution string. I found that both executed with
the same speed. I would claim that the second (shorter) method is easier to
maintain AND quicker to write. Why wouldn't we always use the second
method?

If you're interested, I've included the code same below that illustrates my
test. Thanks in advance.

Mark

******

SqlConnection sqlConn = new SqlConnection(strConn);
SqlCommand sqlComm = new SqlCommand("p_test", sqlConn);

// Mark the Command as a SPROC
sqlComm.CommandType = CommandType.StoredProcedure;

sqlComm.Parameters.Add(new SqlParameter("@param1", SqlDbType.VarChar, 30));
sqlComm.Parameters.Add(new SqlParameter("@param2", SqlDbType.VarChar, 50));
sqlComm.Parameters.Add(new SqlParameter("@param3", SqlDbType.VarChar, 30));
sqlComm.Parameters.Add(new SqlParameter("@param4", SqlDbType.VarChar, 10));
sqlComm.Parameters.Add(new SqlParameter("@param5", SqlDbType.VarChar, 255));
sqlComm.Parameters.Add(new SqlParameter("@param6", SqlDbType.VarChar, 255));
sqlComm.Parameters.Add(new SqlParameter("@param7", SqlDbType.VarChar, 50));

//Test fields
sqlComm.Parameters.Add(new SqlParameter("@param8", SqlDbType.Char, 1));
sqlComm.Parameters.Add(new SqlParameter("@param9", SqlDbType.DateTime));

sqlComm.Parameters["@param1"].Value = "whatever";
sqlComm.Parameters["@param2"].Value = "whatever";
sqlComm.Parameters["@param3"].Value = "whatever";
sqlComm.Parameters["@param4"].Value = "whatever";
sqlComm.Parameters["@param5"].Value = "whatever";
sqlComm.Parameters["@param6"].Value = "whatever";
sqlComm.Parameters["@param7"].Value = "whatever";

//Test params
sqlComm.Parameters["@param8"].Value = "Y";

for (Int32 i = 0; i < 10; i++)
{
//Execute the command
sqlComm.Parameters["@param9"].Value = DateTime.Now;
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}

// ***** NOW TEST THE SECOND (QUICKER) METHOD *****

sqlComm.CommandType = CommandType.Text;
String strExecute = "exec p_test 'whatever', 'whatever', 'whatever',
'whatever', 'whatever', 'whatever', 'whatever','N'";

for (Int32 j = 0; j < 10; j++)
{
//Execute the command
sqlComm.CommandText = strExecute + ", '" + DateTime.Now.ToString() + "'";
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}
Nov 15 '05 #1
2 3871

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one
case I created new SqlParameters in the code, and added the parameters to
the SqlParametersCollection of the SqlCommand object. In the second, I just made it all into long execution string. I found that both executed with
the same speed. I would claim that the second (shorter) method is easier to maintain AND quicker to write.
Why wouldn't we always use the second
method?


But you are in charge of converting all of the parameters to strings, which
can be a source of errors. And (turn on the profiler) you will be using SQL
Batches instead of RPC's to invoke the stored procedure. You probably won't
notice the difference running just one at a time, but the RPC mechanism is
more efficient on the network.
Moreover, the string method is not really any easier or shorter. Look at
the Data Access Application block for how to call a stored procedure with
bound parameters in a single, compact line of code.

David
Nov 15 '05 #2
Depending on how you are implementing the second, you may not be taking
advantage of cached execution plans. moreoever, you can't really conclude
from one test that they run the same speed and even if they do, that
everything always will. In addition, just let someone pass in the name
O'Reilly or anything with an apostrophe just once. The time it takes you to
acknwoledge the error then find it and fix it will be more time than you'll
ever save by using abbreviated syntax without params. I've heard sooo many
people claim that they'd never forget to check for an apostrophe, and
invariably they do. Then there's the issue of passing really long strings
which invariably occur over the network.

IMHO, the little bit of syntax saved is scarcely worth all of the headaches
that come with it.

HTH,

Bill

"Mark" <mf****@idonotlikespam.cce.umn.edu> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one
case I created new SqlParameters in the code, and added the parameters to
the SqlParametersCollection of the SqlCommand object. In the second, I just made it all into long execution string. I found that both executed with
the same speed. I would claim that the second (shorter) method is easier to maintain AND quicker to write. Why wouldn't we always use the second
method?

If you're interested, I've included the code same below that illustrates my test. Thanks in advance.

Mark

******

SqlConnection sqlConn = new SqlConnection(strConn);
SqlCommand sqlComm = new SqlCommand("p_test", sqlConn);

// Mark the Command as a SPROC
sqlComm.CommandType = CommandType.StoredProcedure;

sqlComm.Parameters.Add(new SqlParameter("@param1", SqlDbType.VarChar, 30)); sqlComm.Parameters.Add(new SqlParameter("@param2", SqlDbType.VarChar, 50)); sqlComm.Parameters.Add(new SqlParameter("@param3", SqlDbType.VarChar, 30)); sqlComm.Parameters.Add(new SqlParameter("@param4", SqlDbType.VarChar, 10)); sqlComm.Parameters.Add(new SqlParameter("@param5", SqlDbType.VarChar, 255)); sqlComm.Parameters.Add(new SqlParameter("@param6", SqlDbType.VarChar, 255)); sqlComm.Parameters.Add(new SqlParameter("@param7", SqlDbType.VarChar, 50));
//Test fields
sqlComm.Parameters.Add(new SqlParameter("@param8", SqlDbType.Char, 1));
sqlComm.Parameters.Add(new SqlParameter("@param9", SqlDbType.DateTime));

sqlComm.Parameters["@param1"].Value = "whatever";
sqlComm.Parameters["@param2"].Value = "whatever";
sqlComm.Parameters["@param3"].Value = "whatever";
sqlComm.Parameters["@param4"].Value = "whatever";
sqlComm.Parameters["@param5"].Value = "whatever";
sqlComm.Parameters["@param6"].Value = "whatever";
sqlComm.Parameters["@param7"].Value = "whatever";

//Test params
sqlComm.Parameters["@param8"].Value = "Y";

for (Int32 i = 0; i < 10; i++)
{
//Execute the command
sqlComm.Parameters["@param9"].Value = DateTime.Now;
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}

// ***** NOW TEST THE SECOND (QUICKER) METHOD *****

sqlComm.CommandType = CommandType.Text;
String strExecute = "exec p_test 'whatever', 'whatever', 'whatever',
'whatever', 'whatever', 'whatever', 'whatever','N'";

for (Int32 j = 0; j < 10; j++)
{
//Execute the command
sqlComm.CommandText = strExecute + ", '" + DateTime.Now.ToString() + "'";
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
}

Nov 15 '05 #3

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

Similar topics

5
by: Steve Holden | last post by:
Has anyone, with any driver whatsoever, managed to retrieve output parameters from a SQL Server stored procedure? I've just been rather embarrassed to find out it's not as easy as it might seem,...
3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
1
by: zlatko | last post by:
I use a stored procedure that is calling several other stored procedure which update or append values in several tables. All of them are stored procedures with input parameters by which they filter...
2
by: zlatko | last post by:
There is a form in an Access Project (.adp, Access front end with SQL Server) for entering data into a table for temporary storing. Then, by clicking a botton, several action stored procedures...
13
by: dawatson833 | last post by:
I have several stored procedures with parameters that are defined with user defined data types. The time it takes to run the procedures can take 10 - 50 seconds depending on the procedure. If I...
3
by: Zlatko | last post by:
A question concerning Access Project with SQL Server: I use a stored procedure that is calling several other stored procedure which update or append values in several tables. All of them are...
6
by: Woody Splawn | last post by:
I am using SQL Server 2000 as a back-end to a VS.net Client/Server app. In a certain report I use a view as part of the query spec. For the view, at present, I am querying for all the records in...
5
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored...
3
by: leesquare | last post by:
Hello, I need some help getting output values from my stored procedures when using adodbapi. There's an example testVariableReturningStoredProcedure in adodbapitest.py, and that works for my...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.