30. september 2004 - 10:41
#7
Her er koden:
/*
This script creates all the necessary tables and stored
procedures to use the ASP.NET Unleashed Sample Store.
Notice that it uses the Categories and Products tables
from the Northwind database. If you don't have this
database installed, or you don't have access to this database,
this script will fail miserably.
*/
Print 'Creating AspNETStore Database...'
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'AspNETStore')
DROP DATABASE [AspNETStore]
GO
CREATE DATABASE [AspNETStore]
GO
Print 'Okay, database created'
use [AspNETStore]
GO
/* Create Categories Table */
CREATE TABLE Categories
(
CategoryID int IDENTITY NOT NULL Primary Key ,
CategoryName varchar(15) NOT NULL ,
Description ntext
)
GO
/* Copy Contents from Northwind Category Table */
INSERT Categories ( CategoryName, Description )
SELECT CategoryName, Description
FROM Northwind..Categories
ORDER BY CategoryID
GO
/* Create Products Table */
CREATE TABLE Products
(
ProductID int IDENTITY NOT NULL Primary Key,
ProductName nvarchar(40) NOT NULL ,
SupplierID int NULL ,
CategoryID int NULL ,
QuantityPerUnit nvarchar(20) NULL ,
UnitPrice money NULL ,
UnitsInStock smallint NULL ,
UnitsOnOrder smallint NULL ,
ReorderLevel smallint NULL ,
Discontinued bit NOT NULL ,
Template varchar(50) NOT NULL Default 'Default'
)
GO
/* Copy Contents from Northwind Products Table */
INSERT Products
(
ProductName,
SupplierID ,
CategoryID ,
QuantityPerUnit,
UnitPrice,
UnitsInStock,
UnitsOnOrder ,
ReorderLevel ,
Discontinued
)
SELECT
ProductName,
SupplierID ,
CategoryID ,
QuantityPerUnit,
UnitPrice,
UnitsInStock,
UnitsOnOrder ,
ReorderLevel ,
Discontinued
FROM Northwind..Products
ORDER BY ProductID
GO
/* Create ShoppingCarts Table */
CREATE TABLE ShoppingCarts
(
ItemID int IDENTITY NOT NULL Primary Key ,
UserID uniqueidentifier NOT NULL ,
ProductID int NOT NULL ,
ProductName varchar(200) NOT NULL ,
UnitPrice money NOT NULL ,
Quantity int NOT NULL
)
GO
/* Create OrderDetails Table */
CREATE TABLE OrderDetails
(
ItemID int IDENTITY NOT NULL Primary Key,
UserID uniqueidentifier NOT NULL ,
ProductID int NOT NULL ,
ProductName varchar(200) NOT NULL ,
UnitPrice money NOT NULL ,
Quantity int NOT NULL
)
GO
/* Create Orders Table */
CREATE TABLE Orders
(
OrderID int IDENTITY NOT NULL Primary Key,
UserID uniqueidentifier NULL ,
FirstName varchar(50) NULL ,
LastName varchar(50) ,
billStreet varchar(50) NULL ,
billCity varchar(50) NULL ,
billState varchar(50) NULL ,
billZIP varchar(50) NULL ,
shipStreet varchar(50) NULL ,
shipCity varchar(50) NULL ,
shipState varchar(50) NULL ,
shipZIP varchar(50) NULL ,
EntryDate datetime NOT NULL Default GetDate()
)
GO
/* Create PlaceOrder Stored Procedure */
CREATE PROCEDURE PlaceOrder
(
@userID UniqueIdentifier,
@firstname Varchar( 50 ),
@lastname Varchar( 50 ),
@billStreet Varchar( 50 ),
@billCity Varchar( 50 ),
@billState CHAR( 2 ),
@billZIP Varchar( 15 ),
@shipStreet Varchar( 50 ),
@shipCity Varchar( 50 ),
@shipState Char( 2 ),
@shipZIP Varchar( 15 )
)
As
Insert Orders
(
UserID,
firstname,
lastname,
billStreet,
billCity,
billState,
billZIP,
shipStreet,
shipCity,
shipState,
shipZIP
) Values (
@UserID,
@firstname,
@lastname,
@billStreet,
@billCity,
@billState,
@billZIP,
@shipStreet,
@shipCity,
@shipState,
@shipZIP
)
Insert OrderDetails
(
UserID,
ProductID,
ProductName,
UnitPrice,
Quantity
)
SELECT
UserID,
ProductID,
ProductName,
UnitPrice,
Quantity
From ShoppingCarts
Where ShoppingCarts.UserID = @UserID
Delete ShoppingCarts
Where UserID = @UserID
GO
Print '============================='
Print 'All Done!'
Jeg ved ikke om det er beregnet til access, men hvis jeg kører det klager den over at koden ikke virker. Der skriver den noget om UPDATE DELETE osv...