Microsoft
Software
Hardware
Network
Question : Scope_Identity in SELECT statement with a WHERE clause
I am a little lost with the Scope_Identity function.
When you use an output parameter from a stored procedure, and
ExecuteNonQuery(). What do you, "and most importantly", how do you
use the "ID" ? It seems from the code below that the "ID" was
retrieved but was never used. Can you please show me a simple example
how to use this "ID" in a new SELECT statement with a WHERE clause?
For example, is the following allowed?
//--My Example----//
SELECT FROM Categories [CategoryName] WHERE CategoryID = ID;
//------------------------
-Insert record and retrieve
Scope_Identity------------
----------
------//
string query = "AddCategory";
int ID;
string connect =
@"Server=.\SQLExpress;Data
base=North
wind;Trust
ed_Connect
ion=Yes;";
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.CommandType = CommandType.StoredProcedur
e;
cmd.Parameters.AddWithValu
e("@Catego
ry", Category.Text);
cmd.Parameters.Add("@Categ
oryID", SqlDbType.Int, 0, "CategoryID");
cmd.Parameters["@CategoryI
D"].Direct
ion = ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
ID = (int)cmd.Parameters["@Cate
goryID"].V
alue;
}
}
//------------------------
----Stored
Procedure-----------------
----------
-//
CREATE PROCEDURE AddCategory
-- Add the parameters for the stored procedure here
@Category NVARCHAR(15),
@CategoryID INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO Categories (CategoryName) VALUES (@Category)
SET @CategoryID = SCOPE_IDENTITY()
END
Answer : Scope_Identity in SELECT statement with a WHERE clause
you must use it like this:
SqlDataSource1.Select
Command = "SELECT FROM Categories [CategoryName] WHERE CategoryID = "+ID.ToString();
Random Solutions
Visual Basic Code - Excel - Merge multiple worksheets in an excel workbook into single worksheet
Autofill Date Field in Access 2007
sql server login
The report server is not responding. Verify that the report server is running and can be accessed from this computer.
Arrays in Excel macro
When establishing an ADO connection to a database, is it necessary to have a file path that is free of blank spaces?
how to store emails from exchange server into a tale in access?
Animationed GIFs don't animate in Outlook 2007
Custom Ribbon vba tab selection
How to tell if Windows server 2008 is in Native mode