To create a stored procedure, in Microsoft Visual Studio, in the Server Explorer, under the database connection, expand the database that will own the procedure, right-click Stored Procedures and click Add New Stored Procedure. An empty window would open in the Code Editor, waiting for you to do your thing CREATE PROCEDURE [dbo].[Procedure] @param1 int = 0, @param2 int AS SELECT @param1, @param2 RETURN 0 To create a procedure in Transact-SQL, start with the CREATE PROCEDURE expression. You can also use CREATE PROC. Like everything in your database, you must name your procedure:
After the name of the procedure, type the keyword AS. The section, group of words, or group of lines after the AS keyword is called the body of the stored procedure. It states what you want the procedure to do or what you want it to produce. Based on this, the simplest syntax to create a stored procedure is: CREATE PROCEDURE [SchemaName.]ProcedureName AS Body of the Procedure You can also start the body of the stored procedure with BEGIN and end it with END. The formula to use would be: CREATE PROCEDURE [SchemaName.]ProcedureName AS BEGIN Body of the Procedure END It is important to keep in mind that there are many other issues related to creating a stored procedure but for now, we will consider that syntax. Probably the simplest procedure you can create would consist of selecting fields from a table or a view. To do this, after the AS operator, enter a SELECT expression and apply the techniques we reviewed for data analysis. For example, to create a stored procedure that would hold a list of students from a table named Students, you would create the procedure as follows: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
Text = "Stored Procedures";
Load += new EventHandler(FormLoaded);
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE PROCEDURE Personnel.GetEmployees " +
"AS " +
" SELECT FirstName, " +
" LastName, " +
" HourlySalary " +
" FROM Employees;",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
MessageBox.Show("The stored procedure has been created.",
"Exercise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
Besides SELECT operations, in a stored procedure, you can perform any of the database operations we have applied so far. These include creating and maintaining records, etc.
When a stored procedure produces its results (that is, when it executes), the database engine must keep sending messages back and forth between the server and the client. An example of those messages is that every time the stored procedure produces its result(s), the server must specify the number of records. These relentless interactions may create too much processing and in most cases are not necessary. To avoid them, after the AS keyword, add a SET NOCOUNT ON expression before starting the body of the stored procedure. The formula to follow is: CREATE PROC/PROCEDURE [SchemaName.]ProcedureName AS SET NOCOUNT ON; Body of the Procedure Here is an example: CREATE PROCEDURE Registration.GetIdentification
AS
SET NOCOUNT ON;
BEGIN
SELECT FirstName,
LastName,
DateOfBirth,
Gender
FROM Registration.Students;
END
GO
Although you can create and use a stored procedure without specifying a schema, it is recommended that you always use a schema. In fact, you should always create your own schema in your database and create your stored procedure in it.
To get the results of a stored procedure, you must execute it (in other words, to use a stored procedure, you must call it). To execute a stored procedure in Microsoft Visual Studio:
To execute a procedure in a Windows Forms application, you have two options. You can use the EXECUTE command followed by the name of the procedure. Although there are some other issues related to executing a procedure, for now, we will consider that the simplest syntax to call a procedure is: EXECUTE [SchemaName.]ProcedureName Instead of EXECUTE, you can use the EXEC keyword: EXEC [SchemaName.]ProcedureName As an example, if you have a procedure named GetStudentIdentification, to execute it, you would type: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
ListView lvwEmployees;
ColumnHeader colFirstName;
ColumnHeader colLastName;
ColumnHeader colHourlySalary;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
colFirstName = new ColumnHeader();
colFirstName.Width = 80;
colFirstName.Text = "First Name";
colLastName = new ColumnHeader();
colLastName.Text = "Last Name";
colLastName.Width = 80;
colHourlySalary = new ColumnHeader();
colHourlySalary.Text = "Salary";
colHourlySalary.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
lvwEmployees = new ListView();
lvwEmployees.GridLines = true;
lvwEmployees.FullRowSelect = true;
lvwEmployees.Location = new Point(12, 12);
lvwEmployees.View = System.Windows.Forms.View.Details;
lvwEmployees.Size = new System.Drawing.Size(244, 164);
lvwEmployees.Columns.Add(colFirstName);
lvwEmployees.Columns.Add(colLastName);
lvwEmployees.Columns.Add(colHourlySalary);
Controls.Add(lvwEmployees);
Text = "Stored Procedures";
Controls.Add(this.lvwEmployees);
ClientSize = new System.Drawing.Size(266, 188);
StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
Load += new EventHandler(FormLoaded);
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise = new SqlCommand("EXECUTE Personnel.GetEmployees;",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
SqlDataAdapter sdaExercise = new SqlDataAdapter(cmdExercise);
DataSet dsExercise = new DataSet("ExerciseSet");
sdaExercise.Fill(dsExercise);
foreach (DataRow person in dsExercise.Tables[0].Rows)
{
ListViewItem lviPerson = new ListViewItem(person["FirstName"].ToString());
lviPerson.SubItems.Add(person["LastName"].ToString());
lviPerson.SubItems.Add(person["HourlySalary"].ToString());
lvwEmployees.Items.Add(lviPerson);
}
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
Another option to execute a stored procedure in a Windows Forms Application is to simply pass its name to the command.
As a regular SQL Server database object, you can modify a stored procedure without recreating it. In SQL, the basic formula to modify a stored procedure is: ALTER PROCEDURE ProcedureName
AS
BEGIN
Body of the Procedure
END
You can create an expression and use it in the SELECT statement of a stored procedure. Here is an example: CREATE PROCEDURE GetStudentIdentification
AS
BEGIN
SELECT FullName = FirstName + N' ' + LastName,
DateOfBirth, Gender
FROM Students
END
Also, a stored procedure can call a function in its body. Here is an example: CREATE PROCEDURE GetStudentsAges
AS
BEGIN
SELECT FullName = FirstName + N' ' + LastName,
DATEDIFF(year, DateOfBirth, GETDATE()) AS Age,
Gender
FROM Students
END
One of the characteristics of a stored procedure is that it is treated like an object in its own right. To delete a stored procedure in Transact-SQL, the syntax to use is: DROP PROCEDURE [SchemaName.]ProcedureName Of course, you should make sure you are in the right database and also that the ProcedureName exists.
Like a method of a class in traditional programming languages (C#, Java, etc), and like a function in Transact-SQL (and C++, Visual Basic, Pascal, etc), a stored procedure can take 0, 1, 2, or more arguments. To create a stored procedure that takes an argument, type the formula CREATE PROCEDURE or CREATE PROC followed by the name of the procedure. Then, type the name of an argument starting with the @ symbol. The parameter must have a name, a data type and an optional length depending on the type. Here is the formula you would use: CREATE PROCEDURE [SchemaName.]ProcedureName
@ParameterName DataType
AS
BEGIN
Body of the Procedure
END
Here is an example: CREATE PROC Registrations.GetListOfStudentsByGender
@gdr NVARCHAR(12)
AS
SELECT FirstName, LastName,
DateOfBirth, HomePhone, Gender
FROM Students
WHERE Gender = @gdr
There are two types of actions a SQL command can process. When using a command, before executing it, you must specify its type of action. To assist you with this, the command class is equipped with a property named CommandType. The CommandType property is based on the CommandType enumeration. One of the members of the CommandType enumeration is Text. This is applied for a regular SELECT operation and this is the default type used by the command. This means that if you don't specify the type, Text is applied. That has been the case for all the commands (SELECT statements and functions) we have used in previous sections and lessons. To support stored procedured, the CommandType enumeration is equipped with a member named StoredProcedure. Therefore, if your command statement deals with a stored procedure, specify its CommandType as such. Here is an example: private void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection cnnStudents =
new SqlConnection(strConnection))
{
SqlCommand cmdStudents = new SqlCommand(strStatement, cnnStudents);
cmdStudents.CommandType = CommandType.Text;
cmdStudents.CommandType = CommandType.StoredProcedure;
. . .
}
}
As seen in previous sections, to call a stored procedure in Microsoft Visual Studio, right-click it in the Server Explorer and click Execute. If the procedure takes at least one argument, you must supply it. If you are working visually, a dialog box would come up to allow you to provide a value for the argument:
In the same way, if you execute a stored procedure that takes more than one argument, in the Execute Stored Procedure dialog box, specify the desired but right value for each argument. The value must be of the appropriate type. To programmatically deal with a stored procedure that takes an argument, first call it in your command as seen already. Then, create a SqlParameter object and pass it to the command. Here is an example: private void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection cnnStudents =
new SqlConnection(strConnection))
{
SqlCommand cmdStudents = new SqlCommand(strStatement, cnnStudents);
cmdStudents.CommandType = CommandType.Text;
cmdStudents.CommandType = CommandType.StoredProcedure;
prmStudent.ParameterName = "@StdNbr";
prmStudent.DbType = DbType.String;
prmStudent.Value = "60283";
cmdStudents.Parameters.Add(prmStudent);
cnnStudents.Open();
. . .
}
}
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
As opposed to one, a stored procedure can take many parameters. To create such a procedure, make the list of arguments, each with its own name that starts with @, a name, and a data type. When calling the procedure, process each parametert as we have done so far. Here are examples: private void btnSubmit_Click(object sender, EventArgs e)
{
using (SqlConnection cntWattsALoan =
new SqlConnection("Data Source=(local); " +
"Database='WattsALoan1'; " +
"Integrated Security='SSPI';"))
{
SqlCommand cmdWattsALoan =
new SqlCommand("Loans.SpecifyCurrentBalance",
cntWattsALoan);
cmdWattsALoan.CommandType = CommandType.StoredProcedure;
SqlParameter prmWattsALoan = new SqlParameter();
prmWattsALoan.ParameterName = "@PmtDate";
prmWattsALoan.DbType = DbType.DateTime;
prmWattsALoan.Value = dtpPaymentDate.Value.ToString("d");
cmdWattsALoan.Parameters.Add(prmWattsALoan);
prmWattsALoan = new SqlParameter();
prmWattsALoan.ParameterName = "@EmplNbr";
prmWattsALoan.DbType = DbType.String;
prmWattsALoan.Value = txtEmployeeNumber.Text;
cmdWattsALoan.Parameters.Add(prmWattsALoan);
prmWattsALoan = new SqlParameter();
prmWattsALoan.ParameterName = "@LoanNbr";
prmWattsALoan.DbType = DbType.Int32;
prmWattsALoan.Value = int.Parse(txtLoanNumber.Text);
cmdWattsALoan.Parameters.Add(prmWattsALoan);
prmWattsALoan = new SqlParameter();
prmWattsALoan.ParameterName = "@PmtAmt";
prmWattsALoan.DbType = DbType.Double;
prmWattsALoan.Value = double.Parse(txtPaymentAmount.Text);
cmdWattsALoan.Parameters.Add(prmWattsALoan);
prmWattsALoan = new SqlParameter();
prmWattsALoan.ParameterName = "@Comments";
prmWattsALoan.DbType = DbType.String;
prmWattsALoan.Value = txtNotes.Text;
cmdWattsALoan.Parameters.Add(prmWattsALoan);
cntWattsALoan.Open();
cmdWattsALoan.ExecuteNonQuery();
}
}
As you should know from C#, a method takes various types of arguments. Like a method in C#, A stored procedure can take various types of arguments. To support that concept, the SqlParameter class is equipped with a property named Direction, which is of type ParameterDirection. As seen in the methods of a C# class, a stored procedure can take reference parameters meant to hold a value when the procedure ends. Such an argument is referred to as out. The members of the ParameterDirection enumeration are:
To specify the direction of a parameter, access its Direction and call the appropriate member of the ParameterDirection enumeration. Here is an example from our New Loan Allocation form: private void txtEmployeeNumber_Leave(object sender, EventArgs e)
{
using (SqlConnection cntWattsALoan =
new SqlConnection("Data Source=(local);" +
"Database='WattsALoan1';" +
"Integrated Security=SSPI;"))
{
SqlParameter spEmployee = new SqlParameter();
SqlCommand cmdWattsALoan = new SqlCommand("Personnel.GetEmployeeName", cntWattsALoan);
cmdWattsALoan.CommandType = CommandType.StoredProcedure;
spEmployee.ParameterName = "@EmplNbr";
spEmployee.DbType = DbType.String;
spEmployee.Direction = ParameterDirection.Input;
spEmployee.Value = txtEmployeeNumber.Text;
cmdWattsALoan.Parameters.Add(spEmployee);
cntWattsALoan.Open();
SqlDataReader rdrWattsALoan = cmdWattsALoan.ExecuteReader();
while (rdrWattsALoan.Read())
txtEmployeeName.Text = rdrWattsALoan[0].ToString();
}
}
If you want to pass an output argument to a stored procedure, when creating the procedure, add the OUTPUT keyword to the argument. Here is an example: CREATE PROCEDURE Loans.GetCurrentBalance @LoanNbr int,
@PreviousBalance money OUTPUT
AS
BEGIN
END;";
When creating the SQL parameter in your code, specify its direction but don't assign a value to the parameter.
Stored procedures use the concept of optional arguments as done in C#. To illustrate, imagine you create a database for a department store and a table that holds the list of items sold in the store:
using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvStoreItems;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
dgvStoreItems = new DataGridView();
dgvStoreItems.Location = new Point(12, 12);
dgvStoreItems.Size = new System.Drawing.Size(268, 248);
Text = "Department Store";
Controls.Add(dgvStoreItems);
Load += new EventHandler(FormLoaded);
dgvStoreItems.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntStoreItems =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdStoreItems =
new SqlCommand("SELECT ALL * FROM StoreItems;",
cntStoreItems);
cntStoreItems.Open();
cmdStoreItems.ExecuteNonQuery();
SqlDataAdapter sdaStoreItems = new SqlDataAdapter(cmdStoreItems);
DataSet dsStoreItems = new DataSet("StoreItemsSet");
sdaStoreItems.Fill(dsStoreItems);
dgvStoreItems.DataSource = dsStoreItems.Tables[0];
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
Imagine you want to create a mechanism of calculating the price of an item after a discount has been applied to it. Such a procedure can be created as follows: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
Text = "Stored Procedures";
Load += new EventHandler(FormLoaded);
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE PROCEDURE CalculateNetPrice @discount Decimal " +
"AS " +
" SELECT ItemName, UnitPrice - (UnitPrice * @discount / 100) "+
" FROM StoreItems;",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
MessageBox.Show("The stored procedure has been created.",
"Exercise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
This can be executed as follows: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
Text = "Stored Procedures";
Load += new EventHandler(FormLoaded);
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("EXECUTE CalculateNetPrice;",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
This would produce:
If you are planning to create a procedure that takes an argument and you know that the argument will likely have the same value most of the time, you can provide that value as a parameter but leave a room for other values of that argument. A value given to an argument is referred to as default. What this implies is that, when the user calls that stored procedure, if the user does not provide a value for the argument, the SQL interpreter would use the default value. To create a stored procedure that takes an argument that carries a default value, after declaring the value, on its right side, type = followed by the desired value. Here is an example applied to the above database: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
Text = "Stored Procedures";
Load += new EventHandler(FormLoaded);
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("DROP PROCEDURE CalculateNetPrice;", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE PROCEDURE CalculateNetPrice @discount Decimal = 10.00 " +
"AS " +
" SELECT ItemName, UnitPrice - (UnitPrice * @discount / 100) " +
" FROM StoreItems;",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
MessageBox.Show("The stored procedure has been created.",
"Exercise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
When executing a procedure that takes a default argument, you do not have to provide a value for the argument if the default value suits you. Here is an example: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvStoreItems;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
dgvStoreItems = new DataGridView();
dgvStoreItems.Location = new Point(12, 12);
dgvStoreItems.Size = new System.Drawing.Size(268, 248);
Text = "Department Store";
Controls.Add(dgvStoreItems);
Load += new EventHandler(FormLoaded);
dgvStoreItems.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntStoreItems =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdStoreItems =
new SqlCommand("EXECUTE CalculateNetPrice;",
cntStoreItems);
cntStoreItems.Open();
cmdStoreItems.ExecuteNonQuery();
SqlDataAdapter sdaStoreItems = new SqlDataAdapter(cmdStoreItems);
DataSet dsStoreItems = new DataSet("StoreItemsSet");
sdaStoreItems.Fill(dsStoreItems);
dgvStoreItems.DataSource = dsStoreItems.Tables[0];
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
If the default value does not apply to your current calculation, you can provide a value for the argument. You can create a procedure that takes more than one argument with default values. To provide a default value for each argument, after declaring it, type the desired value to its right side. Here is an example of a procedure that takes two arguments, each with a default value: using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
public class Exercise : System.Windows.Forms.Form
{
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
Text = "Stored Procedures";
Load += new EventHandler(FormLoaded);
}
void FormLoaded(object sender, EventArgs e)
{
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("DROP PROCEDURE CalculateNetPrice;", cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
}
using (SqlConnection cntExercise =
new SqlConnection("Data Source=(local);" +
"Database='Exercise1';" +
"Integrated Security=yes;"))
{
SqlCommand cmdExercise =
new SqlCommand("CREATE PROCEDURE CalculateNetPrice " +
" @Discount Decimal = 20.00, " +
" @TaxRate Decimal = 7.75 " +
"AS " +
" SELECT ItemName As [Item Description]," +
" UnitPrice As [Marked Price]," +
" UnitPrice * @Discount / 100 As [Discount Amt]," +
" UnitPrice - (UnitPrice * @Discount / 100) As [After Discount]," +
" UnitPrice * @TaxRate / 100 As [Tax Amount]," +
" (UnitPrice * @TaxRate / 100) + UnitPrice - " +
" (UnitPrice * @Discount / 100) + (@TaxRate / 100) As [Net Price] " +
" FROM StoreItems;",
cntExercise);
cntExercise.Open();
cmdExercise.ExecuteNonQuery();
MessageBox.Show("The stored procedure has been created.",
"Exercise",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
When calling a procedure that takes more than one argument with all arguments having default values, you do not need to provide a value for each argument, you can provide a value for only one or some of the arguments. The above procedure can be called with one argument as follows: EXEC CalculateSalePrice2 55.00 In this case, the other argument(s) would use its(their) default value(s). We saw that, when calling a procedure that takes more than one argument, you did not have to provide the values of the argument in the exact order they appeared in the procedure, you just had to type the name of each argument and assign it the desired value. In the same way, if a procedure takes more than one argument and some of the arguments have default values, when calling it, you can provide the values in the order of your choice, by typing the name of each argument and assigning it the desired value. Based on this, the above procedure can be called with only the value of the second argument as follows: EXEC CalculateSalePrice2 @TaxRate = 8.55 In this case, the first argument would use its default value. |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|