473,799 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SqlParametersCo llection 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(s trConn);
SqlCommand sqlComm = new SqlCommand("p_t est", sqlConn);

// Mark the Command as a SPROC
sqlComm.Command Type = CommandType.Sto redProcedure;

sqlComm.Paramet ers.Add(new SqlParameter("@ param1", SqlDbType.VarCh ar, 30));
sqlComm.Paramet ers.Add(new SqlParameter("@ param2", SqlDbType.VarCh ar, 50));
sqlComm.Paramet ers.Add(new SqlParameter("@ param3", SqlDbType.VarCh ar, 30));
sqlComm.Paramet ers.Add(new SqlParameter("@ param4", SqlDbType.VarCh ar, 10));
sqlComm.Paramet ers.Add(new SqlParameter("@ param5", SqlDbType.VarCh ar, 255));
sqlComm.Paramet ers.Add(new SqlParameter("@ param6", SqlDbType.VarCh ar, 255));
sqlComm.Paramet ers.Add(new SqlParameter("@ param7", SqlDbType.VarCh ar, 50));

//Test fields
sqlComm.Paramet ers.Add(new SqlParameter("@ param8", SqlDbType.Char, 1));
sqlComm.Paramet ers.Add(new SqlParameter("@ param9", SqlDbType.DateT ime));

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

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

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

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

sqlComm.Command Type = CommandType.Tex t;
String strExecute = "exec p_test 'whatever', 'whatever', 'whatever',
'whatever', 'whatever', 'whatever', 'whatever','N'" ;

for (Int32 j = 0; j < 10; j++)
{
//Execute the command
sqlComm.Command Text = strExecute + ", '" + DateTime.Now.To String() + "'";
sqlConn.Open();
sqlComm.Execute NonQuery();
sqlConn.Close() ;
}
Nov 15 '05 #1
2 3900

"Mark" <mf****@idonotl ikespam.cce.umn .edu> wrote in message
news:Oh******** ******@TK2MSFTN GP09.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 SqlParametersCo llection 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****@idonotl ikespam.cce.umn .edu> wrote in message
news:Oh******** ******@TK2MSFTN GP09.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 SqlParametersCo llection 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(s trConn);
SqlCommand sqlComm = new SqlCommand("p_t est", sqlConn);

// Mark the Command as a SPROC
sqlComm.Command Type = CommandType.Sto redProcedure;

sqlComm.Paramet ers.Add(new SqlParameter("@ param1", SqlDbType.VarCh ar, 30)); sqlComm.Paramet ers.Add(new SqlParameter("@ param2", SqlDbType.VarCh ar, 50)); sqlComm.Paramet ers.Add(new SqlParameter("@ param3", SqlDbType.VarCh ar, 30)); sqlComm.Paramet ers.Add(new SqlParameter("@ param4", SqlDbType.VarCh ar, 10)); sqlComm.Paramet ers.Add(new SqlParameter("@ param5", SqlDbType.VarCh ar, 255)); sqlComm.Paramet ers.Add(new SqlParameter("@ param6", SqlDbType.VarCh ar, 255)); sqlComm.Paramet ers.Add(new SqlParameter("@ param7", SqlDbType.VarCh ar, 50));
//Test fields
sqlComm.Paramet ers.Add(new SqlParameter("@ param8", SqlDbType.Char, 1));
sqlComm.Paramet ers.Add(new SqlParameter("@ param9", SqlDbType.DateT ime));

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

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

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

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

sqlComm.Command Type = CommandType.Tex t;
String strExecute = "exec p_test 'whatever', 'whatever', 'whatever',
'whatever', 'whatever', 'whatever', 'whatever','N'" ;

for (Int32 j = 0; j < 10; j++)
{
//Execute the command
sqlComm.Command Text = strExecute + ", '" + DateTime.Now.To String() + "'";
sqlConn.Open();
sqlComm.Execute NonQuery();
sqlConn.Close() ;
}

Nov 15 '05 #3

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

Similar topics

5
8310
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, and people are saying bad things about Python as a result :-( mx.ODBC, which I regard as a highly-capable module, does not support the callproc() API, and suggests use of the ODBC call format. It has caveats in (some of) the documentation...
3
16947
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 create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
1
2091
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 rows to be updated or inserted into other tables. Filtration is based on certain actual values on forms (form with several subforms). My question is following: How to pass parameters to those stored procedures that are triggered by a button?...
2
17359
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 (update, append) should be activated in order to transfer data to other tables. I tried to avoid any coding in VB, as I am not a professional, but I have found a statement in an article, that, unlike select queries, form's Input Property can't be...
13
10450
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 change the parameter data types to the actual data type such as varchar(10), etc., the stored procedure takes less that a second to return records. The user defined types are mostly varchar, but some others such as int. They are all input type...
3
2145
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 stored procedures with input parameters by which they filter rows to be updated or inserted into other tables. Filtration is based on certain actual values on forms (form with several subforms). My question is following: How to pass parameters to...
6
3578
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 the table. But I am wondering if there is a way, at runtime, to pass values to the View (like start date and end date) so that I don't have to return every record in the table of my view. If I can't pass a parameter, perhaps I can create a view,...
5
2361
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 Procedures with a data reader to add various parameters. However, if I have a stored procedure such as CREATE usp_SelectfromJobNumbers (@par1 datatype, @par2 datatype, @par3 datatype)
3
5123
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 system. But my stored procedure also inserts and accesses a table in the database. Here's what I have it boiled down to: So, when I have
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.