Database stored procedure instance in ASP.net

Source: Internet
Author: User

The second is that some of the programs written in Transaction-sql can be stored in SQL Server as stored procedures, and an application is created to invoke the stored procedure, and the data processing stored procedure can return the result set to the caller by receiving parameters, and the format of the result set is determined by the caller ; Returns the status value to the caller, indicating whether the call was successful or failed, including an action statement against the database, and can invoke another stored procedure in one stored procedure.


Cases

The code is as follows Copy Code

--Table structure
Use [Onlytest]
Go

/****** object:table [dbo].    [Stock] Script date:11/30/2012 18:10:05 ******/
IF EXISTS (SELECT * from sys.objects WHERE object_id = object_id (N ' [dbo].[ ] and type in (N ' U '))
DROP TABLE [dbo]. [Stock]
Go

Use [Onlytest]
Go

/****** object:table [dbo].    [Stock] Script date:11/30/2012 18:10:05 ******/
SET ANSI_NULLS on
Go

SET QUOTED_IDENTIFIER ON
Go

CREATE TABLE [dbo]. [Stock] (
[Stockid] [bigint] IDENTITY (1,1) not NULL,
[ProductID] [INT] Not NULL,
[Stockdate] [Date] Not NULL,
[Stockqty] [INT] Not NULL,
CONSTRAINT [Pk_stock] PRIMARY KEY CLUSTERED
(
[ProductID] Asc
[Stockdate] Asc
With (Pad_index = off, Statistics_norecompute = off, Ignore_dup_key = off, Allow_row_locks = on, Allow_page_locks = O N) on [PRIMARY]
) on [PRIMARY]

Go




Use [Onlytest]
Go

/****** object:storedprocedure [dbo].    [Usp_optstock] Script date:11/30/2012 18:09:35 ******/
IF EXISTS (SELECT * from sys.objects WHERE object_id = object_id (N ' [dbo].[ Usp_optstock] and type in (n ' P ', n ' PC '))
DROP PROCEDURE [dbo]. [Usp_optstock]
Go

/****** object:storedprocedure [dbo].    [Usp_optstock_smallcode] Script date:11/30/2012 18:09:35 ******/
IF EXISTS (SELECT * from sys.objects WHERE object_id = object_id (N ' [dbo].[ Usp_optstock_smallcode] and type in (n ' P ', n ' PC '))
DROP PROCEDURE [dbo]. [Usp_optstock_smallcode]
Go

Use [Onlytest]
Go

/****** object:storedprocedure [dbo].    [Usp_optstock] Script date:11/30/2012 18:09:35 ******/
SET ANSI_NULLS on
Go

SET QUOTED_IDENTIFIER ON
Go


--Concurrent inventory, standard practice
CREATE PROCEDURE [dbo]. [Usp_optstock]
@ProductID INT,
@StartDate DATE,
@EndDate DATE,
@OptNumber INT
As
DECLARE @OptDays INT;
DECLARE @HaveDays INT;
SET @OptDays = DATEDIFF (Day, @StartDate, @EndDate) + 1;

BEGIN TRANSACTION;

SELECT @HaveDays = COUNT (*)
FROM dbo. Stock
WHERE Stockqty >= @OptNumber
and Stockdate >= @StartDate
and Stockdate <= @EndDate
and ProductID = @ProductID

IF @HaveDays = @OptDays
BEGIN
UPDATE dbo. Stock
SET Stockqty = stockqty-@OptNumber
WHERE Stockqty >= @OptNumber
and Stockdate >= @StartDate
and Stockdate <= @EndDate
and ProductID = @ProductID
COMMIT;
return 0;
End
ELSE
BEGIN
ROLLBACK;
RETURN-110;
End

Go

/****** object:storedprocedure [dbo].    [Usp_optstock_smallcode] Script date:11/30/2012 18:09:35 ******/
SET ANSI_NULLS on
Go

SET QUOTED_IDENTIFIER ON
Go


--Concurrent inventory, reckless practices.
CREATE PROCEDURE [dbo]. [Usp_optstock_smallcode]
@ProductID INT,
@StartDate DATE,
@EndDate DATE,
@OptNumber INT
As
DECLARE @OptDays INT;
SET @OptDays = DATEDIFF (Day, @StartDate, @EndDate) + 1;

BEGIN TRANSACTION

UPDATE dbo. Stock
SET Stockqty = stockqty-@OptNumber
WHERE Stockqty >= @OptNumber
and Stockdate >= @StartDate
and Stockdate <= @EndDate
and ProductID = @ProductID

IF @ @ROWCOUNT = @OptDays
BEGIN
COMMIT
return 0
End
ELSE
BEGIN
ROLLBACK
RETURN-110
End

Go







--Test code
Use Onlytest

Go

TRUNCATE TABLE dbo. Stock

INSERT into dbo. Stock VALUES
(1, ' 2012-1-1 ', 1),
(1, ' 2012-1-2 ', 2),
(1, ' 2012-1-3 ', 3),
(1, ' 2012-1-4 ', 4),
(1, ' 2012-1-5 ', 5)


DECLARE @return_value int

EXEC @return_value = [dbo]. [Usp_optstock]
@ProductID = 1,
@StartDate = ' 2012-1-2 ',
@EndDate = ' 2012-1-4 ',
@OptNumber = 2

SELECT ' sufficient stock return ' = @return_value
SELECT * FROM dbo. Stock

EXEC @return_value = [dbo]. [Usp_optstock]
@ProductID = 1,
@StartDate = ' 2012-1-2 ',
@EndDate = ' 2012-1-4 ',
@OptNumber = 2

SELECT ' insufficient stock return ' = @return_value
SELECT * FROM dbo. Stock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.