You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Complete Sample Database Metadata for a Microsoft SQL Server 2022 Linux container. Note: Eliminating the create database as part of the script could be used in either windows or linux by creating the database shell and load the rest of the metadata which is OS agnostic at this point.
USE [master]
GO
/****** This is a create database for a linux docker container /
CREATE DATABASE [Northwinds2024Student]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Northwinds2024Student', FILENAME = N'/var/opt/mssql/data/Northwinds2024Student.mdf' , SIZE = 73728KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'Northwinds2024Student_log', FILENAME = N'/var/opt/mssql/log/Northwinds2024Student_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT, LEDGER = OFF
GO
ALTER DATABASE [Northwinds2024Student] SET COMPATIBILITY_LEVEL = 160
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Northwinds2024Student].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ARITHABORT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Northwinds2024Student] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [Northwinds2024Student] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Northwinds2024Student] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Northwinds2024Student] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Northwinds2024Student] SET DISABLE_BROKER
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Northwinds2024Student] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Northwinds2024Student] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [Northwinds2024Student] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Northwinds2024Student] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [Northwinds2024Student] SET RECOVERY FULL
GO
ALTER DATABASE [Northwinds2024Student] SET MULTI_USER
GO
ALTER DATABASE [Northwinds2024Student] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Northwinds2024Student] SET DB_CHAINING OFF
GO
ALTER DATABASE [Northwinds2024Student] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [Northwinds2024Student] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [Northwinds2024Student] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [Northwinds2024Student] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'Northwinds2024Student', N'ON'
GO
ALTER DATABASE [Northwinds2024Student] SET QUERY_STORE = OFF
GO
USE [Northwinds2024Student]
GO
/ Object: Schema [Audit] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Audit]
GO
/ Object: Schema [Example] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Example]
GO
/ Object: Schema [HumanResources] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [HumanResources]
GO
/ Object: Schema [JsonOutput] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [JsonOutput]
GO
/ Object: Schema [MySchemaName] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [MySchemaName]
GO
/ Object: Schema [Production] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Production]
GO
/ Object: Schema [RelationalCalculii] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [RelationalCalculii]
GO
/ Object: Schema [RelationalCrossJoin] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [RelationalCrossJoin]
GO
/ Object: Schema [Sales] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Sales]
GO
/ Object: Schema [SequenceIdInsert] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [SequenceIdInsert]
GO
/ Object: Schema [SystemVersioned] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [SystemVersioned]
GO
/ Object: Schema [Temporal] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Temporal]
GO
/ Object: Schema [Triggered] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Triggered]
GO
/ Object: Schema [Udt] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Udt]
GO
/ Object: Schema [Utils] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Utils]
GO
/ Object: UserDefinedDataType [Udt].[Address] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Address] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[BlobDefinition] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[BlobDefinition] FROM varbinary NULL
GO
/ Object: UserDefinedDataType [Udt].[CategoryName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[CategoryName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[City] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[City] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[CompanyName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[CompanyName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[ContactName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ContactName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[ContactTitle] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ContactTitle] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Country] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Country] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Currency] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Currency] FROM [money] NULL
GO
/ Object: UserDefinedDataType [Udt].[DateYYYYMMDD] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[DateYYYYMMDD] FROM [date] NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[Description] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Description] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[FirstName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[FirstName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[FlagBit] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[FlagBit] FROM [bit] NULL
GO
/ Object: UserDefinedDataType [Udt].[Grade] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Grade] FROM [tinyint] NULL
GO
/ Object: UserDefinedDataType [Udt].[LastName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[LastName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Name] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Name] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Note] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Note] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Percentage] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Percentage] FROM [numeric](4, 3) NULL
GO
/ Object: UserDefinedDataType [Udt].[PostalCode] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[PostalCode] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[ProductName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ProductName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[QuantitySmall] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[QuantitySmall] FROM [smallint] NULL
GO
/ Object: UserDefinedDataType [Udt].[Region] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Region] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Score] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Score] FROM [tinyint] NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[SequenceNo] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[SequenceNo] FROM [int] NULL
GO
/ Object: UserDefinedDataType [Udt].[ShortDescription] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ShortDescription] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[StateName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[StateName] FROM nvarchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[StudentIdentification] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[StudentIdentification] FROM varchar NULL
GO
/ Object: UserDefinedDataType [Udt].[SurrogateKeyInt] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[SurrogateKeyInt] FROM [int] NULL
GO
/ Object: UserDefinedDataType [Udt].[TelephoneNumber] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[TelephoneNumber] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[TextKeyVarchar10] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[TextKeyVarchar10] FROM varchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[Title] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Title] FROM nvarchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[TitleOfCourtesy] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[TitleOfCourtesy] FROM nvarchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[Version] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Version] FROM [int] NULL
GO
/ Object: UserDefinedDataType [Udt].[YYYY] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[YYYY] FROM nchar NOT NULL
GO
/ Object: UserDefinedFunction [Example].[udf_AddTwoNumbers] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[udf_AddTwoNumbers]
(
-- Add the parameters for the function here @p1 INT, @p2 int
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
--DECLARE @Result int
-- Add the T-SQL statements to compute the return value here
--SELECT @Result = @p1
-- Return the result of the function
RETURN @p1 +@p2
END
GO
/****** Object: UserDefinedFunction [Example].[udf_Caluclator] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[udf_Caluclator]
(
-- Add the parameters for the function here @calculator CHAR(1), @p1 INT, @p2 int
)
RETURNS int
AS
BEGIN
RETURN
CASE
WHEN UPPER(@Calculator) ='A' THEN @p1 + @p2
WHEN UPPER(@Calculator) ='S' THEN @p1 - @p2
WHEN UPPER(@Calculator) ='M' THEN @p1 * @p2
WHEN UPPER(@Calculator) ='D' AND @p2 <> 0 THEN @p1 / @p2
WHEN UPPER(@Calculator) ='D' AND @p2 = 0 THEN -888888
ELSE -777777
end
END
GO
/****** Object: UserDefinedFunction [MySchemaName].[LineCost] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [MySchemaName].[LineCost]
( @UnitPrice MONEY, @quantity INT,
@DiscountPercentage DECIMAL(5,2)
)
RETURNS MONEY
AS
BEGIN
END
GO
/****** Object: UserDefinedFunction [Sales].[TypesOfOrders] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Me
-- Create date:
-- Description:
-- =============================================
CREATE function [Sales].[TypesOfOrders]
(
-- Add the parameters for the function here @orderID int
)
returns char(8)
as
begin
return case
when @OrderId % 2 = 1 then
concat('SO-', @OrderId)
when @OrderId % 10 = 8 then
concat('OM-', @OrderId)
when @OrderId % 10 in ( 0, 2, 4, 6 ) then
concat('PO-', @OrderId)
else 'Unknown'
end;
end;
GO
/****** Object: UserDefinedFunction [Sales].[udf_GetTotalPrice] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Added Money datatype for @UnitPrice and returns money since it is cost
create function [Sales].[udf_GetTotalPrice]
( @quantity int
, @UnitPrice money
, @DiscountPercentage numeric(4, 3)
)
returns money
as
begin
-- Return the result of the function
return (@quantity * @UnitPrice) * (1. - @DiscountPercentage);
end;
GO
/****** Object: UserDefinedFunction [Utils].[CreateSha256KeyFromJsonInputCustomer] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================================================================================
-- Author: Your Name
-- Create date: Today
-- Modification date: Today
-- FUNCTION: Hashing.CreateSha256KeyFromJsonInputAnalyticsCustomerExpenditureClassification (@pky int)
-- FUNCTION type: scalar
create FUNCTION [Utils].[CreateSha256KeyFromJsonInputCustomer] (@CustomerId int)
returns varbinary(32)
as
begin
-- Sales.[Order] selected @ColumnOuputInJSONIncludingNullValues are
-- all of the columns designated to create the JSON output
-- to create the unique 'SHA2_256' hash key
declare @ColumnOuputInJSONIncludingNullValues nvarchar(max) =
(select C.*
from Sales.Customer as c
where c.CustomerId =@CustomerId
for json auto,include_null_values);
--
-- Sales.[Order]
-- selected @ColumnOuputInJSONIncludingNullValues is the JSON output
-- used for input into HASHBYTES('SHA2_256', @ColumnOuputInJSONIncludingNullValues)
-- to create the varbinary(32) output
end
GO
/****** Object: UserDefinedFunction [Utils].[CreateSha256KeyFromJsonInputSalesOrder] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [Utils].[CreateSha256KeyFromJsonInputSalesOrder] (@orderID int)
returns varbinary(32)
as
begin
-- Sales.[Order] selected @ColumnOuputInJSONIncludingNullValues are
-- all of the columns designated to create the JSON output
-- to create the unique 'SHA2_256' hash key
declare @ColumnOuputInJSONIncludingNullValues nvarchar(max) =
(select o.OrderId,
o.CustomerId,
o.EmployeeId,
o.ShipperId,
o.OrderDate,
o.RequiredDate,
o.ShipToDate,
o.Freight,
o.ShipToName,
o.ShipToAddress,
o.ShipToCity,
o.ShipToRegion,
o.ShipToPostalCode,
o.ShipToCountry
from Sales.[Order] as o
where o.OrderId =@OrderId
for json auto,include_null_values);
--
-- Sales.[Order]
-- selected @ColumnOuputInJSONIncludingNullValues is the JSON output
-- used for input into HASHBYTES('SHA2_256', @ColumnOuputInJSONIncludingNullValues)
-- to create the varbinary(32) output
end
GO
/****** Object: UserDefinedFunction [Utils].[DetermineFiscalYearQuarters] Script Date: 7/5/2024 6:08:18 AM *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [Utils].[DetermineFiscalYearQuarters]
(
@OrderDate date
)
returns nvarchar(20)
as
begin
-- Declare the return variable here
declare @Result varchar(20), @yyyy int = year(@OrderDate), @mm int = month(@OrderDate),
@FiscalYear char(2) = 'FY',
@AnchorMonth int = 7;
----
/
If the anchor month is 1 (January), this is a fiscal year based upon the Calendar Year /
if (@mm = 1) set @FiscalYear = 'CY'
else
/
If the anchor month is NOT January, this is a fiscal year that spans
two years. The months from the Anchor month through December
requires adding one year to the year(@OrderDate) + 1
*/
if (@mm between @AnchorMonth and 12) set @yyyy += 1;
-- Below is for the Fiscal Year July:
select @Result = case
when @MM in ( @AnchorMonth, 8, 9 ) then
concat(@FiscalYear, @YYYY, '-QTR I')
when @MM in ( 10, 11, 12 ) then
concat(@FiscalYear, @YYYY, '-QTR II')
when @MM in ( 1, 2, 3 ) then
concat(@FiscalYear, @YYYY, '-QTR III')
when @MM in ( 4, 5, 6 ) then
concat(@FiscalYear, @YYYY, '-QTR IV')
else
'Problem'
end;
-- Return the result of the function
return @Result
end;
GO
/****** Object: Table [Sales].[OrderDetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[OrderDetail](
[OrderId] [Udt].[SurrogateKeyInt] NOT NULL,
[ProductId] [Udt].[SurrogateKeyInt] NOT NULL,
[UnitPrice] [Udt].[Currency] NOT NULL,
[Quantity] [Udt].[QuantitySmall] NOT NULL,
[DiscountPercentage] [Udt].[Percentage] NOT NULL,
[DiscountedLineAmount] AS (([UnitPrice][Quantity])((1.)-[DiscountPercentage])),
[LineAmount] AS ([UnitPrice][Quantity]),
CONSTRAINT [PK_OrderDetails] PRIMARY KEY CLUSTERED
(
[OrderId] ASC,
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/*** Object: Table [Sales].[Order] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[Order](
[OrderId] [Udt].[SurrogateKeyInt] NOT NULL,
[CustomerId] [Udt].[SurrogateKeyInt] NULL,
[EmployeeId] [Udt].[SurrogateKeyInt] NOT NULL,
[ShipperId] [Udt].[SurrogateKeyInt] NOT NULL,
[OrderDate] [Udt].[DateYYYYMMDD] NOT NULL,
[RequiredDate] [Udt].[DateYYYYMMDD] NOT NULL,
[ShipToDate] [Udt].[DateYYYYMMDD] NULL,
[Freight] [Udt].[Currency] NOT NULL,
[ShipToName] [Udt].[ContactName] NOT NULL,
[ShipToAddress] [Udt].[Address] NOT NULL,
[ShipToCity] [Udt].[City] NOT NULL,
[ShipToRegion] [Udt].[Region] NULL,
[ShipToPostalCode] [Udt].[PostalCode] NULL,
[ShipToCountry] [Udt].[Country] NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[OrderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: View [Sales].[uvw_OrderValues] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Create Views and Functions
CREATE view [Sales].[uvw_OrderValues]
--with schemabinding
as
select O.OrderId
, O.CustomerId
, O.EmployeeId
, O.ShipperId
, O.OrderDate
, O.RequiredDate
, O.ShipToDate
, sum(OD.Quantity) as TotalQuantity
, cast(sum(OD.Quantity * OD.UnitPrice * (1 - OD.DiscountPercentage)) as numeric(12, 2)) as DiscountedTotalAmount
from Sales.[Order] as O
join Sales.OrderDetail as OD
on O.OrderId = OD.OrderId
group by O.OrderId
, O.CustomerId
, O.EmployeeId
, O.ShipperId
, O.OrderDate
, O.RequiredDate
, O.ShipToDate;
GO
/****** Object: View [Sales].[uvw_TotalOrderCost] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_TotalOrderCost]
with schemabinding
as
select o.CustomerId
, o.OrderId
, sum(od.Quantity) as TotalQuantity
, sum((od.Quantity * od.UnitPrice) * (1. - od.DiscountPercentage)) as TotalDiscountedAmount
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.CustomerId
, o.OrderId;
GO
/ Object: View [Utils].[InvoiceMonthAndMonthName] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Utils].[InvoiceMonthAndMonthName] as
select distinct
month(o.OrderDate) as InvoiceMonthInt,
format(month(o.OrderDate), '0#') as InvoiceMonth,
datename(month, o.OrderDate) as InvoiceMonthName
from Sales.[Order] as o
--order by InvoiceMonth;
GO
/ Object: Table [Sales].[Customer] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[Customer](
[CustomerId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[CustomerCompanyName] [Udt].[CompanyName] NOT NULL,
[CustomerContactName] [Udt].[ContactName] NOT NULL,
[CustomerContactTitle] [Udt].[Title] NOT NULL,
[CustomerAddress] [Udt].[Address] NOT NULL,
[CustomerCity] [Udt].[City] NOT NULL,
[CustomerRegion] [Udt].[Region] NULL,
[CustomerPostalCode] [Udt].[PostalCode] NULL,
[CustomerCountry] [Udt].[Country] NOT NULL,
[CustomerPhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
[CustomerFaxNumber] [Udt].[TelephoneNumber] NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [XAK1CustomerCompamyName] UNIQUE NONCLUSTERED
(
[CustomerCompanyName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: UserDefinedFunction [Example].[itvfCustomerCountry] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
create function [Example].[itvfCustomerCountry]
(
-- Add the parameters for the function here
@CustomerCountry nvarchar(15)
)
returns table
as
return
(
select c.CustomerId,
c.CustomerCompanyName,
c.CustomerCountry
from Sales.Customer as c
where c.CustomerCountry = @CustomerCountry
);
GO
/ Object: UserDefinedFunction [Example].[itvfEmployeeParameter] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[itvfEmployeeParameter]
(
-- Add the parameters for the function here
@EmployeeId as int
)
RETURNS TABLE
AS
RETURN
(
select year(o.OrderDate) as OrderYear,
o.CustomerId,
o.EmployeeId,
@EmployeeId as parameterEmployeeId
from Sales.[Order] as o
where o.EmployeeId = @EmployeeId
)
GO
/ Object: UserDefinedFunction [Example].[itvfSpecificEmployee] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[itvfSpecificEmployee]
(
@EmployeId int
)
RETURNS TABLE
AS
RETURN
(
select year(o.OrderDate) as OrderYear,
o.CustomerId
from Sales.[Order] as o
where o.EmployeeId = @EmployeId
)
GO
/ Object: Table [HumanResources].[Employee] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [HumanResources].[Employee](
[EmployeeId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[EmployeeLastName] [Udt].[LastName] NOT NULL,
[EmployeeFirstName] [Udt].[FirstName] NOT NULL,
[EmployeeTitle] [Udt].[Title] NOT NULL,
[EmployeeTitleOfCourtesy] [Udt].[TitleOfCourtesy] NOT NULL,
[BirthDate] [Udt].[DateYYYYMMDD] NOT NULL,
[HireDate] [Udt].[DateYYYYMMDD] NOT NULL,
[EmployeeAddress] [Udt].[Address] NOT NULL,
[EmployeeCity] [Udt].[City] NULL,
[EmployeeRegion] [Udt].[Region] NULL,
[EmployeePostalCode] [Udt].[PostalCode] NULL,
[EmployeeCountry] [Udt].[Country] NOT NULL,
[EmployeePhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
[EmployeeManagerId] [Udt].[SurrogateKeyInt] NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: Table [Sales].[Shipper] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[Shipper](
[ShipperId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[ShipperCompanyName] [Udt].[CompanyName] NOT NULL,
[PhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
CONSTRAINT [PK_Shippers] PRIMARY KEY CLUSTERED
(
[ShipperId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: UserDefinedFunction [JsonOutput].[MongoOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
create function [JsonOutput].[MongoOrder]
()
returns table
as
return
(
select o.OrderId as OrderObjectId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.Freight
, o.ShipToName
, o.ShipToAddress
, o.ShipToCity
, o.ShipToRegion
, o.ShipToPostalCode
, o.ShipToCountry
, (
select SalesCustomer.CustomerId as 'CustomerObjectId'
, SalesCustomer.CustomerCompanyName as 'CustomerCompanyName'
, SalesCustomer.CustomerContactName as 'CustomerContactName'
, SalesCustomer.CustomerPhoneNumber as 'CustomerPhoneNumber'
, SalesCustomer.CustomerFaxNumber as 'CustomerFaxNumber'
from Sales.[Order] as o2
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesCustomer
, (
select HumanResourcesEmployee.EmployeeId as 'EmployeeObjectId'
, HumanResourcesEmployee.EmployeeLastName as 'EmployeeLastName'
, HumanResourcesEmployee.EmployeeFirstName as 'EmployeeFirstName'
, HumanResourcesEmployee.EmployeePhoneNumber as 'EmployeePhoneNumber'
, HumanResourcesEmployee.EmployeeManagerId as 'EmployeeManagerId'
from Sales.[Order] as o2
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o2.EmployeeId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as HumanResourcesEmployee
, (
select SalesShipper.ShipperId as 'ShipperObjectId'
, SalesShipper.ShipperCompanyName as 'ShipperCompanyName'
, SalesShipper.PhoneNumber as 'PhoneNumber'
from Sales.[Order] as o2
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o2.ShipperId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesShipper
from Sales.[Order] as o
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o.EmployeeId
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o.ShipperId
);
GO
/ Object: Table [Production].[Category] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Production].[Category](
[CategoryId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[CategoryName] [Udt].[CategoryName] NOT NULL,
[Description] [Udt].[Description] NOT NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[CategoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: Table [Production].[Product] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Production].[Product](
[ProductId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[ProductName] [Udt].[ProductName] NOT NULL,
[SupplierId] [Udt].[SurrogateKeyInt] NOT NULL,
[CategoryId] [Udt].[SurrogateKeyInt] NOT NULL,
[UnitPrice] [Udt].[Currency] NOT NULL,
[Discontinued] [Udt].[FlagBit] NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [XAK1ProductName] UNIQUE NONCLUSTERED
(
[ProductName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: Table [Production].[Supplier] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Production].[Supplier](
[SupplierId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[SupplierCompanyName] [Udt].[CompanyName] NOT NULL,
[SupplierContactName] [Udt].[ContactName] NOT NULL,
[SupplierContactTitle] [Udt].[ContactTitle] NOT NULL,
[SupplierAddress] [Udt].[Address] NOT NULL,
[SupplierCity] [Udt].[City] NOT NULL,
[SupplierRegion] [Udt].[Region] NULL,
[SupplierPostalCode] [Udt].[PostalCode] NULL,
[SupplierCountry] [Udt].[Country] NOT NULL,
[SupplierPhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
[SupplierFaxNumber] [Udt].[TelephoneNumber] NULL,
CONSTRAINT [PK_Suppliers] PRIMARY KEY CLUSTERED
(
[SupplierId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [XAK1SupplierCompanyName] UNIQUE NONCLUSTERED
(
[SupplierCompanyName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: UserDefinedFunction [JsonOutput].[MongoOrderDetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE function [JsonOutput].[MongoOrderDetail]
( @orderID int
)
returns table
as
return
(
select OrderDetail.OrderId as OrderObjectId
, row_number() over (partition by OrderDetail.OrderId order by OrderDetail.OrderId) as LineItemNumer
, OrderDetail.UnitPrice
, OrderDetail.Quantity
, OrderDetail.DiscountPercentage
, OrderDetail.UnitPrice * OrderDetail.Quantity as TotalCost
, (OrderDetail.UnitPrice * OrderDetail.Quantity) * (1. - OrderDetail.DiscountPercentage) as TotalDiscountedCost
, OrderDetail.OrderId
, (
select distinct
ProductionProduct.ProductId as 'ProductionProduct.ProductObjectId'
, ProductionProduct.ProductName as 'ProductionProduct.ProductName'
, ProductionProduct.SupplierId as 'ProductionProduct.SupplierId'
, ProductionProduct.CategoryId as 'ProductionProduct.CategoryId'
, ProductionProduct.UnitPrice as 'ProductionProduct.UnitPrice'
, ProductionProduct.Discontinued as 'ProductionProduct.Discontinued'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionProduct
, (
select distinct
ProductionCategory.CategoryId as 'ProductionCategory.CategoryObjectId'
, ProductionCategory.CategoryName as 'ProductionCategory.CategoryName'
, ProductionCategory.Description as 'ProductionCategory.Description'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionCategory
, (
select distinct
ProductionSupplier.SupplierId as 'ProductionSupplier.SupplierObjectId'
, ProductionSupplier.SupplierCompanyName as 'ProductionSupplier.SupplierCompanyName'
, ProductionSupplier.SupplierContactName as 'ProductionSupplier.SupplierContactName'
, ProductionSupplier.SupplierPhoneNumber as 'ProductionSupplier.SupplierPhoneNumber'
, ProductionSupplier.SupplierFaxNumber as 'ProductionSupplier.SupplierFaxNumber'
from Production.Product as ProductionProduct
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionSupplier
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as OrderDetail
on OrderDetail.ProductId = ProductionProduct.ProductId
where OrderDetail.OrderId = @orderID
);
GO
/ Object: UserDefinedFunction [Sales].[utvf_FindAllCustomerOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [Sales].[utvf_FindAllCustomerOrder]
(
@CustomerId as int
)
returns table
as
return
(
select o.OrderId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.EmployeeId
, o.ShipperId
, sh.ShipperCompanyName
, p.ProductName
, su.SupplierCompanyName
, su.SupplierCountry
, o.Freight
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmount
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmountDiscounted
, od.Quantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId
inner join Production.Supplier as su
on su.SupplierId = p.SupplierId
inner join Sales.Shipper as sh
on sh.ShipperId = o.ShipperId
where o.CustomerId = @CustomerId
);
GO
/ Object: UserDefinedFunction [Sales].[utvf_FindAllCustomerOrdersByCustomerId] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [Sales].[utvf_FindAllCustomerOrdersByCustomerId]
(
@CustomerId as int
)
returns table
as
return
(
select p.ProductName
, o.OrderId
, o.OrderDate
, od.UnitPrice
, od.Quantity
, od.DiscountPercentage
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId
where o.CustomerId = @CustomerId
);
GO
/ Object: UserDefinedFunction [Sales].[utvf_GetAllCustomerOrdersByCustId] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [Sales].[utvf_GetAllCustomerOrdersByCustId]
(
@CustomerId as int
)
returns table
as
return
(
select @CustomerId as CustIdParameter
,c.CustomerCompanyName
, o.orderId
, o.OrderDate
from Sales.[Order] as o
inner join Sales.Customer as c
on c.CustomerId = o.CustomerId
where c.customerid = @CustomerId
);
GO
/****** Object: UserDefinedFunction [Production].[LowestPriceProducts] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [Production].[LowestPriceProducts]
(@SupplierId AS INT, @NumberOfRows AS INT)
RETURNS TABLE
AS
RETURN
SELECT TOP (@NumberOfRows)
p.ProductId, p.ProductName,p.UnitPrice
FROM Production.Product as p
WHERE supplierid = @SupplierId
ORDER BY unitprice;
GO
/****** Object: View [Utils].[uvw_FindColumnDefinitionPlusDefaultAndCheckConstraint] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [Utils].[uvw_FindColumnDefinitionPlusDefaultAndCheckConstraint]
as
select concat(tbl.TABLE_SCHEMA, '.', tbl.TABLE_NAME) as FullyQualifiedTableName
, tbl.TABLE_SCHEMA as SchemaName
, tbl.TABLE_NAME as TableName
, col.COLUMN_NAME as ColumnName
, col.ORDINAL_POSITION as OrdinalPosition
, concat(col.DOMAIN_SCHEMA, '.', col.DOMAIN_NAME) as FullyQualifiedDomainName
, col.DOMAIN_NAME as DomainName
, case
when col.DATA_TYPE = 'varchar' then
concat('varchar(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'char' then
concat('char(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'nvarchar' then
concat('nvarchar(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'nchar' then
concat('nchar(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'numeric' then
concat('numeric(', NUMERIC_PRECISION_RADIX, ', ', NUMERIC_SCALE, ')')
when col.DATA_TYPE = 'decimal' then
concat('decimal(', NUMERIC_PRECISION_RADIX, ', ', NUMERIC_SCALE, ')')
else
col.DATA_TYPE
end as DataType
, col.IS_NULLABLE as IsNullable
, dcn.DefaultName
, col.COLUMN_DEFAULT as DefaultNameDefinition
, cc.CONSTRAINT_NAME as CheckConstraintRuleName
, cc.CHECK_CLAUSE as CheckConstraintRuleNameDefinition
from
(
select TABLE_CATALOG
, TABLE_SCHEMA
, TABLE_NAME
, TABLE_TYPE
from INFORMATION_SCHEMA.TABLES
where (TABLE_TYPE = 'BASE TABLE')
) as tbl
inner join
(
select TABLE_CATALOG
, TABLE_SCHEMA
, TABLE_NAME
, COLUMN_NAME
, ORDINAL_POSITION
, COLUMN_DEFAULT
, IS_NULLABLE
, DATA_TYPE
, CHARACTER_MAXIMUM_LENGTH
, CHARACTER_OCTET_LENGTH
, NUMERIC_PRECISION
, NUMERIC_PRECISION_RADIX
, NUMERIC_SCALE
, DATETIME_PRECISION
, CHARACTER_SET_CATALOG
, CHARACTER_SET_SCHEMA
, CHARACTER_SET_NAME
, COLLATION_CATALOG
, COLLATION_SCHEMA
, COLLATION_NAME
, DOMAIN_CATALOG
, DOMAIN_SCHEMA
, DOMAIN_NAME
from INFORMATION_SCHEMA.COLUMNS
) as col
on col.TABLE_CATALOG = tbl.TABLE_CATALOG
and col.TABLE_SCHEMA = tbl.TABLE_SCHEMA
and col.TABLE_NAME = tbl.TABLE_NAME
left outer join
(
select t.name as TableName
, schema_name(s.schema_id) as SchemaName
, ac.name as ColumnName
, d.name as DefaultName
from sys.all_columns as ac
inner join sys.tables as t
on ac.object_id = t.object_id
inner join sys.schemas as s
on t.schema_id = s.schema_id
inner join sys.default_constraints as d
on ac.default_object_id = d.object_id
) as dcn
on dcn.SchemaName = tbl.TABLE_SCHEMA
and dcn.TableName = tbl.TABLE_NAME
and dcn.ColumnName = col.COLUMN_NAME
left outer join
(
select cu.TABLE_CATALOG
, cu.TABLE_SCHEMA
, cu.TABLE_NAME
, cu.COLUMN_NAME
, c.CONSTRAINT_CATALOG
, c.CONSTRAINT_SCHEMA
, c.CONSTRAINT_NAME
, c.CHECK_CLAUSE
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as cu
inner join INFORMATION_SCHEMA.CHECK_CONSTRAINTS as c
on c.CONSTRAINT_NAME = cu.CONSTRAINT_NAME
) as cc
on cc.TABLE_SCHEMA = tbl.TABLE_SCHEMA
and cc.TABLE_NAME = tbl.TABLE_NAME
and cc.COLUMN_NAME = col.COLUMN_NAME;
GO
/****** Object: View [Utils].[uw_FindAllDatabaseColumnsOnTheServer] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Utils].[uw_FindAllDatabaseColumnsOnTheServer] as
select vw.FullyQualifiedTableName
,vw.SchemaName
, vw.ColumnName
, vw.OrdinalPosition
, vw.FullyQualifiedDomainName
, vw.DataType
, vw.IsNullable
, vw.DefaultName
, vw.DefaultNameDefinition
, vw.CheckConstraintRuleName
, vw.CheckConstraintRuleNameDefinition
, @@ServerName as 'Server Name' -- The database server's machine name
, @@Version as [Target Database Version]
, @@servicename as 'Instance Name' -- e.g.: MSSQLSERVER
, db_name() as 'Database Name'
, host_name() as 'Host Name' -- The database client's machine name
from Utils.uvw_FindColumnDefinitionPlusDefaultAndCheckConstraint as vw
where vw.SchemaName in ('Sales','Production', 'HumanResources')
GO
/****** Object: View [Example].[uvw_OrderTotalCost] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Example].[uvw_OrderTotalCost]
as
select header.OrderId
, header.EmployeeName
, header.CustomerName
, cast(header.Freight + Line.OrderDetailCost as money) as TotalOrderCost
, header.OrderDate
, header.OrderYear
, header.OrderMonth
, Line.NumberOfProductsPurchased
from
(
select o.OrderId
, concat(e.EmployeeFirstName, ' ', e.EmployeeLastName) as EmployeeName
, case
when o.CustomerId = 71 then
'Customer I'
when o.CustomerId = 20 then
'Customer II'
when o.CustomerId = 63 then
'Customer III'
when o.CustomerId = 24 then
'Customer IV'
else
'Error'
end as CustomerName
, o.Freight
, o.OrderDate
, year(o.OrderDate) as OrderYear
, month(o.OrderDate) as OrderMonth
from Sales.[Order] as o
inner join HumanResources.Employee as e
on e.EmployeeId = o.EmployeeId
where o.CustomerId in ( 71, 20, 63, 24 )
) as header
inner join
(
select o.OrderId
, count(od.ProductId) as NumberOfProductsPurchased
, sum(od.UnitPrice * od.Quantity * (1.0 - od.DiscountPercentage)) as OrderDetailCost
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.OrderId
) as Line
on Line.OrderId = header.OrderId;
GO
/ Object: View [Example].[uvwCountryCustomers] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Example].[uvwCountryCustomers]
as
select c.CustomerId
, c.CustomerCompanyName
, c.CustomerCountry
, c.CustomerContactTitle
from Sales.Customer as c
GO
/ Object: View [JsonOutput].[uvw_HumanResourcesEmployee] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_HumanResourcesEmployee] as
--for json auto, include_null_values;
SELECT E.EmployeeId as [EmployeeObjectId]
, E.EmployeeLastName
, E.EmployeeFirstName
, E.EmployeeTitle
, E.EmployeeTitleOfCourtesy
, E.BirthDate
, E.HireDate
, E.EmployeeAddress
, E.EmployeeCity
, E.EmployeeRegion
, E.EmployeePostalCode
, E.EmployeeCountry
, E.EmployeePhoneNumber
, E.EmployeeManagerId FROM HumanResources.Employee as E
GO
/ Object: View [JsonOutput].[uvw_MongoDBOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_MongoDBOrder]
as
select o.OrderId as OrderObjectId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.Freight
, o.ShipToName
, o.ShipToAddress
, o.ShipToCity
, o.ShipToRegion
, o.ShipToPostalCode
, o.ShipToCountry
, (
select SalesCustomer.CustomerId as 'CustomerObjectId'
, SalesCustomer.CustomerCompanyName as 'CustomerCompanyName'
, SalesCustomer.CustomerContactName as 'CustomerContactName'
, SalesCustomer.CustomerPhoneNumber as 'CustomerPhoneNumber'
, SalesCustomer.CustomerFaxNumber as 'CustomerFaxNumber'
from Sales.[Order] as o2
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesCustomer
, (
select HumanResourcesEmployee.EmployeeId as 'EmployeeObjectId'
, HumanResourcesEmployee.EmployeeLastName as 'EmployeeLastName'
, HumanResourcesEmployee.EmployeeFirstName as 'EmployeeFirstName'
, HumanResourcesEmployee.EmployeePhoneNumber as 'EmployeePhoneNumber'
, HumanResourcesEmployee.EmployeeManagerId as 'EmployeeManagerId'
from Sales.[Order] as o2
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o2.EmployeeId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as HumanResourcesEmployee
, (
select SalesShipper.ShipperId as 'ShipperObjectId'
, SalesShipper.ShipperCompanyName as 'ShipperCompanyName'
, SalesShipper.PhoneNumber as 'PhoneNumber'
from Sales.[Order] as o2
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o2.ShipperId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesShipper
from Sales.[Order] as o
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o.EmployeeId
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o.ShipperId;
GO
/ Object: View [JsonOutput].[uvw_MongoDBOrderdetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_MongoDBOrderdetail] as
select OrderDetail.OrderId as OrderObjectId
, row_number() over (partition by OrderDetail.OrderId order by OrderDetail.OrderId) as LineItemNumer
, OrderDetail.UnitPrice
, OrderDetail.Quantity
, OrderDetail.DiscountPercentage
, OrderDetail.UnitPrice * OrderDetail.Quantity as TotalCost
, (OrderDetail.UnitPrice * OrderDetail.Quantity) * (1. - OrderDetail.DiscountPercentage) as TotalDiscountedCost
, OrderDetail.OrderId
, (
select distinct
ProductionProduct.ProductId as 'ProductionProduct.ProductObjectId'
, ProductionProduct.ProductName as 'ProductionProduct.ProductName'
, ProductionProduct.SupplierId as 'ProductionProduct.SupplierId'
, ProductionProduct.CategoryId as 'ProductionProduct.CategoryId'
, ProductionProduct.UnitPrice as 'ProductionProduct.UnitPrice'
, ProductionProduct.Discontinued as 'ProductionProduct.Discontinued'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionProduct
, (
select distinct
ProductionCategory.CategoryId as 'ProductionCategory.CategoryObjectId'
, ProductionCategory.CategoryName as 'ProductionCategory.CategoryName'
, ProductionCategory.Description as 'ProductionCategory.Description'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionCategory
, (
select distinct
ProductionSupplier.SupplierId as 'ProductionSupplier.SupplierObjectId'
, ProductionSupplier.SupplierCompanyName as 'ProductionSupplier.SupplierCompanyName'
, ProductionSupplier.SupplierContactName as 'ProductionSupplier.SupplierContactName'
, ProductionSupplier.SupplierPhoneNumber as 'ProductionSupplier.SupplierPhoneNumber'
, ProductionSupplier.SupplierFaxNumber as 'ProductionSupplier.SupplierFaxNumber'
from Production.Product as ProductionProduct
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionSupplier
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as OrderDetail
on OrderDetail.ProductId = ProductionProduct.ProductId
GO
/ Object: View [JsonOutput].[uvw_Order] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_Order] as
SELECT O.OrderId
, O.CustomerId
, O.EmployeeId
, O.ShipperId
, O.OrderDate
, O.RequiredDate
, O.ShipToDate
, O.Freight
, O.ShipToName
, O.ShipToAddress
, O.ShipToCity
, O.ShipToRegion
, O.ShipToPostalCode
, O.ShipToCountry
FROM Sales.[Order] as O
GO
/ Object: View [JsonOutput].[uvw_OrderDetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_OrderDetail] as
SELECT OD.OrderId
, OD.ProductId
, OD.UnitPrice
, OD.Quantity
, OD.DiscountPercentage
FROM Sales.OrderDetail as OD
GO
/ Object: View [JsonOutput].[uvw_ProductionCategory] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [JsonOutput].[uvw_ProductionCategory] as
--for json auto, include_null_values;
select CategoryId as [CategoryObjectId]
,[CategoryName]
,[Description]
FROM [Production].[Category]
GO
/ Object: View [JsonOutput].[uvw_ProductionProduct] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_ProductionProduct] as
--for json auto, include_null_values;
select
P.ProductId as ProductObjectId
, P.ProductName
, P.SupplierId
, P.CategoryId
, P.UnitPrice
, P.Discontinued
, C.CategoryId as 'Production.CategoryObjectId'
, C.CategoryName as 'Production.CategoryName'
, C.Description as 'Production.Description'
, S.SupplierId as 'Production.SupplierId'
, S.SupplierCompanyName as 'Production.SupplierCompanyName'
, S.SupplierContactName as 'Production.SupplierContactName'
, S.SupplierPhoneNumber as 'Production.SupplierPhoneNumber'
, S.SupplierFaxNumber as 'Production.SupplierFaxNumber'
from Production.Product as P
inner join Production.Category as C
on C.CategoryId = P.CategoryId
inner join Production.Supplier as S
on S.SupplierId = P.SupplierId
GO
/ Object: View [JsonOutput].[uvw_ProductionSupplier] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_ProductionSupplier] as
--for json auto, include_null_values;
select S.SupplierId as SupplierObjectId
, S.SupplierCompanyName
, S.SupplierContactName
, S.SupplierContactTitle
, S.SupplierAddress
, S.SupplierCity
, S.SupplierRegion
, S.SupplierPostalCode
, S.SupplierCountry
, S.SupplierPhoneNumber
, S.SupplierFaxNumber
from Production.Supplier as S
GO
/ Object: View [JsonOutput].[uvw_SalesCustomer] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_SalesCustomer] as
--for json auto, include_null_values;
select C.CustomerId as CustomerObjectId
, C.CustomerCompanyName
, C.CustomerContactName
, C.CustomerContactTitle
, C.CustomerAddress
, C.CustomerCity
, C.CustomerRegion
, C.CustomerPostalCode
, C.CustomerCountry
, C.CustomerPhoneNumber
, C.CustomerFaxNumber
from Sales.Customer as C
GO
/ Object: View [JsonOutput].[uvw_SalesShipper] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_SalesShipper] as
--for json auto, include_null_values;
SELECT S.ShipperId as ShipperObjectId
, S.ShipperCompanyName
, S.PhoneNumber FROM Sales.Shipper as S
GO
/ Object: View [Sales].[uvw_CustomerOrder] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_CustomerOrder]
with schemabinding
as
select
o.CustomerId,
dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date)) as OrderMonth,
sum(od.Quantity) as TotalQuantity
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by CustomerId, dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date));
GO
/****** Object: View [Sales].[uvw_CustomerOrderAndOrderDetail] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [Sales].[uvw_CustomerOrderAndOrderDetail]
as
select c.CustomerCompanyName
, c.CustomerCountry
, c.CustomerRegion
, c.CustomerCity
, c.CustomerPostalCode
,c.CustomerId
, o.OrderId
, o.EmployeeId
, o.ShipperId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.Freight
, od.ProductId
, od.Quantity as TotalQuantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.Customer as c
inner join Sales.[Order] as o
on o.CustomerId = c.CustomerId
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
;
GO
/****** Object: View [Sales].[uvw_CustomerProductOrderAndOrderDetail] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_CustomerProductOrderAndOrderDetail]
as
select c.CustomerCompanyName
, o.OrderId
, o.OrderDate
, o.EmployeeId
, o.ShipperId
, p.ProductName
, o.Freight
,TotalDiscountedLineAmount =
(od.Quantity* od.UnitPrice) * (1.0-od.DiscountPercentage)
, od.Quantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.Customer as c
inner join Sales.[Order] as o
on o.CustomerId = c.CustomerId
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId;
GO
/****** Object: View [Sales].[uvw_EmployeeOrder] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_EmployeeOrder]
with schemabinding
as
select
o.EmployeeId,
dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date)) as OrderMonth,
sum(od.Quantity) as TotalQuantity,
cast(sum(od.Quantity * od.UnitPrice * (1 - od.DiscountPercentage))
as numeric(12, 2)) as TotalDiscountedAmount,
count() as TotalNumberOfOrders
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.EmployeeId, dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date));
GO
/***** Object: View [Sales].[uvw_FindAllCustomerOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [Sales].[uvw_FindAllCustomerOrder]
as
select o.CustomerId
, o.OrderId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.EmployeeId
, o.ShipperId
, sh.ShipperCompanyName
, p.ProductName
, su.SupplierCompanyName
, su.SupplierCountry
, o.Freight
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmount
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmountDiscounted
, od.Quantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId
inner join Production.Supplier as su
on su.SupplierId = p.SupplierId
inner join Sales.Shipper as sh
on sh.ShipperId = o.ShipperId;
GO
/ Object: View [Sales].[uvw_OrderTotalQuantityAndTotalDiscountedAmount] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Create Views and Functions
create view [Sales].[uvw_OrderTotalQuantityAndTotalDiscountedAmount]
with schemabinding
as
select o.OrderId
, o.CustomerId
, o.EmployeeId
, o.ShipperId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, sum(od.Quantity) as TotalQuantity
, cast(sum(od.Quantity * od.UnitPrice * (1 - od.DiscountPercentage)) as numeric(12, 2)) as [TotalDiscountedAmount]
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.OrderId
, o.CustomerId
, o.EmployeeId
, o.ShipperId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate;
GO
/****** Object: View [Sales].[uvw_OrderTotalQuantityByYear] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_OrderTotalQuantityByYear]
with schemabinding
as
select year(o.OrderDate) as OrderYear
, sum(od.Quantity) as TotalQuantity
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by year(o.OrderDate);
GO
/****** Object: Table [dbo].[Digits] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Digits](
[digit] [int] NOT NULL
) ON [PRIMARY]
GO
/ Object: Table [RelationalCalculii].[USSupremeCourtJustices] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [RelationalCalculii].[USSupremeCourtJustices](
[JusticeId] [int] NOT NULL,
[LastName] nvarchar NOT NULL,
[FirstName] nvarchar NOT NULL,
[StateOfBirth] nvarchar NOT NULL,
[YearOfBirth] nchar NOT NULL
) ON [PRIMARY]
GO
/ Object: Table [RelationalCrossJoin].[S1] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [RelationalCrossJoin].[S1](
[Letter] char NULL
) ON [PRIMARY]
GO
/ Object: Table [RelationalCrossJoin].[S2] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [RelationalCrossJoin].[S2](
[Number] char NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/ Object: Index [NCI_UniqueCategoryName_Idx] Script Date: 7/5/2024 6:08:18 AM /
CREATE UNIQUE NONCLUSTERED INDEX [NCI_UniqueCategoryName_Idx] ON [Production].[Category]
(
[CategoryName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/ Object: Index [XIE1SupplierContactName] Script Date: 7/5/2024 6:08:18 AM /
CREATE NONCLUSTERED INDEX [XIE1SupplierContactName] ON [Production].[Supplier]
(
[SupplierContactName] ASC
)
INCLUDE([SupplierContactTitle],[SupplierAddress],[SupplierCity],[SupplierCountry],[SupplierPhoneNumber]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/ Object: Index [XIE1CustomerContactName] Script Date: 7/5/2024 6:08:18 AM /
CREATE NONCLUSTERED INDEX [XIE1CustomerContactName] ON [Sales].[Customer]
(
[CustomerContactName] ASC
)
INCLUDE([CustomerCompanyName],[CustomerContactTitle],[CustomerAddress],[CustomerCity],[CustomerCountry],[CustomerPhoneNumber],[CustomerPostalCode]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
/ Object: Index [XAK1OrderDate] Script Date: 7/5/2024 6:08:18 AM /
CREATE NONCLUSTERED INDEX [XAK1OrderDate] ON [Sales].[Order]
(
[OrderDate] ASC
)
INCLUDE([OrderId],[CustomerId],[EmployeeId],[ShipperId],[RequiredDate],[ShipToDate],[Freight]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
ALTER TABLE [Production].[Product] ADD CONSTRAINT [DFT_Products_unitprice] DEFAULT ((0)) FOR [UnitPrice]
GO
ALTER TABLE [Production].[Product] ADD CONSTRAINT [DFT_Products_discontinued] DEFAULT ((0)) FOR [Discontinued]
GO
ALTER TABLE [Sales].[Order] ADD CONSTRAINT [DFT_Orders_freight] DEFAULT ((0)) FOR [Freight]
GO
ALTER TABLE [Sales].[OrderDetail] ADD CONSTRAINT [DFT_OrderDetails_UnitPrice] DEFAULT ((0)) FOR [UnitPrice]
GO
ALTER TABLE [Sales].[OrderDetail] ADD CONSTRAINT [DFT_OrderDetails_Quantity] DEFAULT ((1)) FOR [Quantity]
GO
ALTER TABLE [Sales].[OrderDetail] ADD CONSTRAINT [DFT_OrderDetails_DiscountPercentage] DEFAULT ((0)) FOR [DiscountPercentage]
GO
ALTER TABLE [HumanResources].[Employee] WITH CHECK ADD CONSTRAINT [FK_Employees_Employees] FOREIGN KEY([EmployeeManagerId])
REFERENCES [HumanResources].[Employee] ([EmployeeId])
GO
ALTER TABLE [HumanResources].[Employee] CHECK CONSTRAINT [FK_Employees_Employees]
GO
ALTER TABLE [Production].[Product] WITH CHECK ADD CONSTRAINT [FK_Product_Category] FOREIGN KEY([CategoryId])
REFERENCES [Production].[Category] ([CategoryId])
GO
ALTER TABLE [Production].[Product] CHECK CONSTRAINT [FK_Product_Category]
GO
ALTER TABLE [Production].[Product] WITH CHECK ADD CONSTRAINT [FK_product_supplier] FOREIGN KEY([SupplierId])
REFERENCES [Production].[Supplier] ([SupplierId])
GO
ALTER TABLE [Production].[Product] CHECK CONSTRAINT [FK_product_supplier]
GO
ALTER TABLE [Sales].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Customer1] FOREIGN KEY([CustomerId])
REFERENCES [Sales].[Customer] ([CustomerId])
GO
ALTER TABLE [Sales].[Order] CHECK CONSTRAINT [FK_Order_Customer1]
GO
ALTER TABLE [Sales].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Employee] FOREIGN KEY([EmployeeId])
REFERENCES [HumanResources].[Employee] ([EmployeeId])
GO
ALTER TABLE [Sales].[Order] CHECK CONSTRAINT [FK_Order_Employee]
GO
ALTER TABLE [Sales].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Shipper] FOREIGN KEY([ShipperId])
REFERENCES [Sales].[Shipper] ([ShipperId])
GO
ALTER TABLE [Sales].[Order] CHECK CONSTRAINT [FK_Order_Shipper]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [FK_OrderDetail_Order] FOREIGN KEY([OrderId])
REFERENCES [Sales].[Order] ([OrderId])
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [FK_OrderDetail_Order]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [FK_OrderDetail_Product] FOREIGN KEY([ProductId])
REFERENCES [Production].[Product] ([ProductId])
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [FK_OrderDetail_Product]
GO
ALTER TABLE [HumanResources].[Employee] WITH CHECK ADD CONSTRAINT [CHK_birthdate] CHECK (([BirthDate]<=CONVERT([date],sysdatetime())))
GO
ALTER TABLE [HumanResources].[Employee] CHECK CONSTRAINT [CHK_birthdate]
GO
ALTER TABLE [Production].[Product] WITH CHECK ADD CONSTRAINT [CHK_Products_unitprice] CHECK (([UnitPrice]>=(0)))
GO
ALTER TABLE [Production].[Product] CHECK CONSTRAINT [CHK_Products_unitprice]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [CHK_DiscountPercentage] CHECK (([DiscountPercentage]>=(0) AND [DiscountPercentage]<=(1)))
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [CHK_DiscountPercentage]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [CHK_Quantity] CHECK (([Quantity]>(0)))
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [CHK_Quantity]
GO
/ Object: StoredProcedure [JsonOutput].[uspHumanResourcesEmployee] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
create procedure [JsonOutput].[uspHumanResourcesEmployee]
AS
BEGIN
select e.EmployeeId as [EmployeeObjectId]
, e.EmployeeLastName
, e.EmployeeFirstName
, e.EmployeeTitle
, e.EmployeeTitleOfCourtesy
, e.BirthDate
, e.HireDate
, e.EmployeeAddress
, e.EmployeeCity
, e.EmployeeRegion
, e.EmployeePostalCode
, e.EmployeeCountry
, e.EmployeePhoneNumber
, E.EmployeeManagerId from HumanResources.Employee as E
for json auto, include_null_values;
END
GO
/****** Object: StoredProcedure [Sales].[usp_getorders] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [Sales].[usp_getorders] @Country as nvarchar(40)
as
select orderid
, orderdate
, empid
, custid
from Sales.Orders
where shipcountry = @Country;
GO
USE [master]
GO
ALTER DATABASE [Northwinds2024Student] SET READ_WRITE
GO
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Vendor database
Complete Sample Database Metadata for a Microsoft SQL Server 2022 Linux container. Note: Eliminating the create database as part of the script could be used in either windows or linux by creating the database shell and load the rest of the metadata which is OS agnostic at this point.
USE [master]
GO
/****** This is a create database for a linux docker container /
CREATE DATABASE [Northwinds2024Student]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Northwinds2024Student', FILENAME = N'/var/opt/mssql/data/Northwinds2024Student.mdf' , SIZE = 73728KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'Northwinds2024Student_log', FILENAME = N'/var/opt/mssql/log/Northwinds2024Student_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
WITH CATALOG_COLLATION = DATABASE_DEFAULT, LEDGER = OFF
GO
ALTER DATABASE [Northwinds2024Student] SET COMPATIBILITY_LEVEL = 160
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Northwinds2024Student].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ARITHABORT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Northwinds2024Student] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [Northwinds2024Student] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Northwinds2024Student] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Northwinds2024Student] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Northwinds2024Student] SET DISABLE_BROKER
GO
ALTER DATABASE [Northwinds2024Student] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Northwinds2024Student] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Northwinds2024Student] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [Northwinds2024Student] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [Northwinds2024Student] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Northwinds2024Student] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Northwinds2024Student] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [Northwinds2024Student] SET RECOVERY FULL
GO
ALTER DATABASE [Northwinds2024Student] SET MULTI_USER
GO
ALTER DATABASE [Northwinds2024Student] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Northwinds2024Student] SET DB_CHAINING OFF
GO
ALTER DATABASE [Northwinds2024Student] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [Northwinds2024Student] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [Northwinds2024Student] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [Northwinds2024Student] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'Northwinds2024Student', N'ON'
GO
ALTER DATABASE [Northwinds2024Student] SET QUERY_STORE = OFF
GO
USE [Northwinds2024Student]
GO
/ Object: Schema [Audit] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Audit]
GO
/ Object: Schema [Example] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Example]
GO
/ Object: Schema [HumanResources] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [HumanResources]
GO
/ Object: Schema [JsonOutput] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [JsonOutput]
GO
/ Object: Schema [MySchemaName] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [MySchemaName]
GO
/ Object: Schema [Production] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Production]
GO
/ Object: Schema [RelationalCalculii] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [RelationalCalculii]
GO
/ Object: Schema [RelationalCrossJoin] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [RelationalCrossJoin]
GO
/ Object: Schema [Sales] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Sales]
GO
/ Object: Schema [SequenceIdInsert] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [SequenceIdInsert]
GO
/ Object: Schema [SystemVersioned] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [SystemVersioned]
GO
/ Object: Schema [Temporal] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Temporal]
GO
/ Object: Schema [Triggered] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Triggered]
GO
/ Object: Schema [Udt] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Udt]
GO
/ Object: Schema [Utils] Script Date: 7/5/2024 6:08:18 AM /
CREATE SCHEMA [Utils]
GO
/ Object: UserDefinedDataType [Udt].[Address] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Address] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[BlobDefinition] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[BlobDefinition] FROM varbinary NULL
GO
/ Object: UserDefinedDataType [Udt].[CategoryName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[CategoryName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[City] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[City] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[CompanyName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[CompanyName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[ContactName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ContactName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[ContactTitle] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ContactTitle] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Country] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Country] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Currency] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Currency] FROM [money] NULL
GO
/ Object: UserDefinedDataType [Udt].[DateYYYYMMDD] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[DateYYYYMMDD] FROM [date] NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[Description] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Description] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[FirstName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[FirstName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[FlagBit] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[FlagBit] FROM [bit] NULL
GO
/ Object: UserDefinedDataType [Udt].[Grade] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Grade] FROM [tinyint] NULL
GO
/ Object: UserDefinedDataType [Udt].[LastName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[LastName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Name] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Name] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Note] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Note] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Percentage] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Percentage] FROM [numeric](4, 3) NULL
GO
/ Object: UserDefinedDataType [Udt].[PostalCode] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[PostalCode] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[ProductName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ProductName] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[QuantitySmall] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[QuantitySmall] FROM [smallint] NULL
GO
/ Object: UserDefinedDataType [Udt].[Region] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Region] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[Score] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Score] FROM [tinyint] NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[SequenceNo] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[SequenceNo] FROM [int] NULL
GO
/ Object: UserDefinedDataType [Udt].[ShortDescription] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[ShortDescription] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[StateName] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[StateName] FROM nvarchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[StudentIdentification] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[StudentIdentification] FROM varchar NULL
GO
/ Object: UserDefinedDataType [Udt].[SurrogateKeyInt] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[SurrogateKeyInt] FROM [int] NULL
GO
/ Object: UserDefinedDataType [Udt].[TelephoneNumber] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[TelephoneNumber] FROM nvarchar NULL
GO
/ Object: UserDefinedDataType [Udt].[TextKeyVarchar10] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[TextKeyVarchar10] FROM varchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[Title] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Title] FROM nvarchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[TitleOfCourtesy] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[TitleOfCourtesy] FROM nvarchar NOT NULL
GO
/ Object: UserDefinedDataType [Udt].[Version] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[Version] FROM [int] NULL
GO
/ Object: UserDefinedDataType [Udt].[YYYY] Script Date: 7/5/2024 6:08:18 AM /
CREATE TYPE [Udt].[YYYY] FROM nchar NOT NULL
GO
/ Object: UserDefinedFunction [Example].[udf_AddTwoNumbers] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[udf_AddTwoNumbers]
(
-- Add the parameters for the function here
@p1 INT,
@p2 int
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
--DECLARE @Result int
END
GO
/****** Object: UserDefinedFunction [Example].[udf_Caluclator] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[udf_Caluclator]
(
-- Add the parameters for the function here
@calculator CHAR(1),
@p1 INT,
@p2 int
)
RETURNS int
AS
BEGIN
END
GO
/****** Object: UserDefinedFunction [MySchemaName].[LineCost] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [MySchemaName].[LineCost]
( @UnitPrice MONEY,
@quantity INT,
@DiscountPercentage DECIMAL(5,2)
)
RETURNS MONEY
AS
BEGIN
END
GO
/****** Object: UserDefinedFunction [Sales].[TypesOfOrders] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Me
-- Create date:
-- Description:
-- =============================================
CREATE function [Sales].[TypesOfOrders]
(
-- Add the parameters for the function here
@orderID int
)
returns char(8)
as
begin
end;
GO
/****** Object: UserDefinedFunction [Sales].[udf_GetTotalPrice] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Added Money datatype for @UnitPrice and returns money since it is cost
create function [Sales].[udf_GetTotalPrice]
(
@quantity int
, @UnitPrice money
, @DiscountPercentage numeric(4, 3)
)
returns money
as
begin
-- Return the result of the function
return (@quantity * @UnitPrice) * (1. - @DiscountPercentage);
end;
GO
/****** Object: UserDefinedFunction [Utils].[CreateSha256KeyFromJsonInputCustomer] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================================================================================
-- Author: Your Name
-- Create date: Today
-- Modification date: Today
-- FUNCTION: Hashing.CreateSha256KeyFromJsonInputAnalyticsCustomerExpenditureClassification (@pky int)
-- FUNCTION type: scalar
-- Description: TSQL-AuditInsertTrigger Templated Code
-- =========================================================================================
create FUNCTION [Utils].[CreateSha256KeyFromJsonInputCustomer] (@CustomerId int)
returns varbinary(32)
as
begin
-- Sales.[Order] selected @ColumnOuputInJSONIncludingNullValues are
-- all of the columns designated to create the JSON output
-- to create the unique 'SHA2_256' hash key
--
-- Sales.[Order]
-- selected @ColumnOuputInJSONIncludingNullValues is the JSON output
-- used for input into HASHBYTES('SHA2_256', @ColumnOuputInJSONIncludingNullValues)
-- to create the varbinary(32) output
end
GO
/****** Object: UserDefinedFunction [Utils].[CreateSha256KeyFromJsonInputSalesOrder] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [Utils].[CreateSha256KeyFromJsonInputSalesOrder] (@orderID int)
returns varbinary(32)
as
begin
-- Sales.[Order] selected @ColumnOuputInJSONIncludingNullValues are
-- all of the columns designated to create the JSON output
-- to create the unique 'SHA2_256' hash key
--
-- Sales.[Order]
-- selected @ColumnOuputInJSONIncludingNullValues is the JSON output
-- used for input into HASHBYTES('SHA2_256', @ColumnOuputInJSONIncludingNullValues)
-- to create the varbinary(32) output
end
GO
/****** Object: UserDefinedFunction [Utils].[DetermineFiscalYearQuarters] Script Date: 7/5/2024 6:08:18 AM *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [Utils].[DetermineFiscalYearQuarters]
(
@OrderDate date
)
returns nvarchar(20)
as
begin
-- Declare the return variable here
declare @Result varchar(20),
@yyyy int = year(@OrderDate),
@mm int = month(@OrderDate),
@FiscalYear char(2) = 'FY',
@AnchorMonth int = 7;
----
/
If the anchor month is 1 (January), this is a fiscal year based upon the Calendar Year
/
if (@mm = 1) set @FiscalYear = 'CY'
else
/
If the anchor month is NOT January, this is a fiscal year that spans
two years. The months from the Anchor month through December
requires adding one year to the year(@OrderDate) + 1
*/
if (@mm between @AnchorMonth and 12) set @yyyy += 1;
GO
/****** Object: Table [Sales].[OrderDetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[OrderDetail](
[OrderId] [Udt].[SurrogateKeyInt] NOT NULL,
[ProductId] [Udt].[SurrogateKeyInt] NOT NULL,
[UnitPrice] [Udt].[Currency] NOT NULL,
[Quantity] [Udt].[QuantitySmall] NOT NULL,
[DiscountPercentage] [Udt].[Percentage] NOT NULL,
[DiscountedLineAmount] AS (([UnitPrice][Quantity])((1.)-[DiscountPercentage])),
[LineAmount] AS ([UnitPrice][Quantity]),
CONSTRAINT [PK_OrderDetails] PRIMARY KEY CLUSTERED
(
[OrderId] ASC,
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/*** Object: Table [Sales].[Order] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[Order](
[OrderId] [Udt].[SurrogateKeyInt] NOT NULL,
[CustomerId] [Udt].[SurrogateKeyInt] NULL,
[EmployeeId] [Udt].[SurrogateKeyInt] NOT NULL,
[ShipperId] [Udt].[SurrogateKeyInt] NOT NULL,
[OrderDate] [Udt].[DateYYYYMMDD] NOT NULL,
[RequiredDate] [Udt].[DateYYYYMMDD] NOT NULL,
[ShipToDate] [Udt].[DateYYYYMMDD] NULL,
[Freight] [Udt].[Currency] NOT NULL,
[ShipToName] [Udt].[ContactName] NOT NULL,
[ShipToAddress] [Udt].[Address] NOT NULL,
[ShipToCity] [Udt].[City] NOT NULL,
[ShipToRegion] [Udt].[Region] NULL,
[ShipToPostalCode] [Udt].[PostalCode] NULL,
[ShipToCountry] [Udt].[Country] NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[OrderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: View [Sales].[uvw_OrderValues] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Create Views and Functions
CREATE view [Sales].[uvw_OrderValues]
--with schemabinding
as
select O.OrderId
, O.CustomerId
, O.EmployeeId
, O.ShipperId
, O.OrderDate
, O.RequiredDate
, O.ShipToDate
, sum(OD.Quantity) as TotalQuantity
, cast(sum(OD.Quantity * OD.UnitPrice * (1 - OD.DiscountPercentage)) as numeric(12, 2)) as DiscountedTotalAmount
from Sales.[Order] as O
join Sales.OrderDetail as OD
on O.OrderId = OD.OrderId
group by O.OrderId
, O.CustomerId
, O.EmployeeId
, O.ShipperId
, O.OrderDate
, O.RequiredDate
, O.ShipToDate;
GO
/****** Object: View [Sales].[uvw_TotalOrderCost] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_TotalOrderCost]
with schemabinding
as
select o.CustomerId
, o.OrderId
, sum(od.Quantity) as TotalQuantity
, sum((od.Quantity * od.UnitPrice) * (1. - od.DiscountPercentage)) as TotalDiscountedAmount
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.CustomerId
, o.OrderId;
GO
/ Object: View [Utils].[InvoiceMonthAndMonthName] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Utils].[InvoiceMonthAndMonthName] as
select distinct
month(o.OrderDate) as InvoiceMonthInt,
format(month(o.OrderDate), '0#') as InvoiceMonth,
datename(month, o.OrderDate) as InvoiceMonthName
from Sales.[Order] as o
--order by InvoiceMonth;
GO
/ Object: Table [Sales].[Customer] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[Customer](
[CustomerId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[CustomerCompanyName] [Udt].[CompanyName] NOT NULL,
[CustomerContactName] [Udt].[ContactName] NOT NULL,
[CustomerContactTitle] [Udt].[Title] NOT NULL,
[CustomerAddress] [Udt].[Address] NOT NULL,
[CustomerCity] [Udt].[City] NOT NULL,
[CustomerRegion] [Udt].[Region] NULL,
[CustomerPostalCode] [Udt].[PostalCode] NULL,
[CustomerCountry] [Udt].[Country] NOT NULL,
[CustomerPhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
[CustomerFaxNumber] [Udt].[TelephoneNumber] NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [XAK1CustomerCompamyName] UNIQUE NONCLUSTERED
(
[CustomerCompanyName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: UserDefinedFunction [Example].[itvfCustomerCountry] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
create function [Example].[itvfCustomerCountry]
(
-- Add the parameters for the function here
@CustomerCountry nvarchar(15)
)
returns table
as
return
(
select c.CustomerId,
c.CustomerCompanyName,
c.CustomerCountry
from Sales.Customer as c
where c.CustomerCountry = @CustomerCountry
);
GO
/ Object: UserDefinedFunction [Example].[itvfEmployeeParameter] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[itvfEmployeeParameter]
(
-- Add the parameters for the function here
@EmployeeId as int
)
RETURNS TABLE
AS
RETURN
(
select year(o.OrderDate) as OrderYear,
o.CustomerId,
o.EmployeeId,
@EmployeeId as parameterEmployeeId
from Sales.[Order] as o
where o.EmployeeId = @EmployeeId
)
GO
/ Object: UserDefinedFunction [Example].[itvfSpecificEmployee] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE FUNCTION [Example].[itvfSpecificEmployee]
(
@EmployeId int
)
RETURNS TABLE
AS
RETURN
(
select year(o.OrderDate) as OrderYear,
o.CustomerId
from Sales.[Order] as o
where o.EmployeeId = @EmployeId
)
GO
/ Object: Table [HumanResources].[Employee] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [HumanResources].[Employee](
[EmployeeId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[EmployeeLastName] [Udt].[LastName] NOT NULL,
[EmployeeFirstName] [Udt].[FirstName] NOT NULL,
[EmployeeTitle] [Udt].[Title] NOT NULL,
[EmployeeTitleOfCourtesy] [Udt].[TitleOfCourtesy] NOT NULL,
[BirthDate] [Udt].[DateYYYYMMDD] NOT NULL,
[HireDate] [Udt].[DateYYYYMMDD] NOT NULL,
[EmployeeAddress] [Udt].[Address] NOT NULL,
[EmployeeCity] [Udt].[City] NULL,
[EmployeeRegion] [Udt].[Region] NULL,
[EmployeePostalCode] [Udt].[PostalCode] NULL,
[EmployeeCountry] [Udt].[Country] NOT NULL,
[EmployeePhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
[EmployeeManagerId] [Udt].[SurrogateKeyInt] NULL,
CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: Table [Sales].[Shipper] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Sales].[Shipper](
[ShipperId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[ShipperCompanyName] [Udt].[CompanyName] NOT NULL,
[PhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
CONSTRAINT [PK_Shippers] PRIMARY KEY CLUSTERED
(
[ShipperId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: UserDefinedFunction [JsonOutput].[MongoOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
create function [JsonOutput].[MongoOrder]
()
returns table
as
return
(
select o.OrderId as OrderObjectId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.Freight
, o.ShipToName
, o.ShipToAddress
, o.ShipToCity
, o.ShipToRegion
, o.ShipToPostalCode
, o.ShipToCountry
, (
select SalesCustomer.CustomerId as 'CustomerObjectId'
, SalesCustomer.CustomerCompanyName as 'CustomerCompanyName'
, SalesCustomer.CustomerContactName as 'CustomerContactName'
, SalesCustomer.CustomerPhoneNumber as 'CustomerPhoneNumber'
, SalesCustomer.CustomerFaxNumber as 'CustomerFaxNumber'
from Sales.[Order] as o2
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesCustomer
, (
select HumanResourcesEmployee.EmployeeId as 'EmployeeObjectId'
, HumanResourcesEmployee.EmployeeLastName as 'EmployeeLastName'
, HumanResourcesEmployee.EmployeeFirstName as 'EmployeeFirstName'
, HumanResourcesEmployee.EmployeePhoneNumber as 'EmployeePhoneNumber'
, HumanResourcesEmployee.EmployeeManagerId as 'EmployeeManagerId'
from Sales.[Order] as o2
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o2.EmployeeId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as HumanResourcesEmployee
, (
select SalesShipper.ShipperId as 'ShipperObjectId'
, SalesShipper.ShipperCompanyName as 'ShipperCompanyName'
, SalesShipper.PhoneNumber as 'PhoneNumber'
from Sales.[Order] as o2
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o2.ShipperId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesShipper
from Sales.[Order] as o
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o.EmployeeId
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o.ShipperId
);
GO
/ Object: Table [Production].[Category] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Production].[Category](
[CategoryId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[CategoryName] [Udt].[CategoryName] NOT NULL,
[Description] [Udt].[Description] NOT NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
(
[CategoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: Table [Production].[Product] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Production].[Product](
[ProductId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[ProductName] [Udt].[ProductName] NOT NULL,
[SupplierId] [Udt].[SurrogateKeyInt] NOT NULL,
[CategoryId] [Udt].[SurrogateKeyInt] NOT NULL,
[UnitPrice] [Udt].[Currency] NOT NULL,
[Discontinued] [Udt].[FlagBit] NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ProductId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [XAK1ProductName] UNIQUE NONCLUSTERED
(
[ProductName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: Table [Production].[Supplier] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Production].[Supplier](
[SupplierId] [Udt].[SurrogateKeyInt] IDENTITY(1,1) NOT NULL,
[SupplierCompanyName] [Udt].[CompanyName] NOT NULL,
[SupplierContactName] [Udt].[ContactName] NOT NULL,
[SupplierContactTitle] [Udt].[ContactTitle] NOT NULL,
[SupplierAddress] [Udt].[Address] NOT NULL,
[SupplierCity] [Udt].[City] NOT NULL,
[SupplierRegion] [Udt].[Region] NULL,
[SupplierPostalCode] [Udt].[PostalCode] NULL,
[SupplierCountry] [Udt].[Country] NOT NULL,
[SupplierPhoneNumber] [Udt].[TelephoneNumber] NOT NULL,
[SupplierFaxNumber] [Udt].[TelephoneNumber] NULL,
CONSTRAINT [PK_Suppliers] PRIMARY KEY CLUSTERED
(
[SupplierId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
CONSTRAINT [XAK1SupplierCompanyName] UNIQUE NONCLUSTERED
(
[SupplierCompanyName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/ Object: UserDefinedFunction [JsonOutput].[MongoOrderDetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
CREATE function [JsonOutput].[MongoOrderDetail]
(
@orderID int
)
returns table
as
return
(
select OrderDetail.OrderId as OrderObjectId
, row_number() over (partition by OrderDetail.OrderId order by OrderDetail.OrderId) as LineItemNumer
, OrderDetail.UnitPrice
, OrderDetail.Quantity
, OrderDetail.DiscountPercentage
, OrderDetail.UnitPrice * OrderDetail.Quantity as TotalCost
, (OrderDetail.UnitPrice * OrderDetail.Quantity) * (1. - OrderDetail.DiscountPercentage) as TotalDiscountedCost
, OrderDetail.OrderId
, (
select distinct
ProductionProduct.ProductId as 'ProductionProduct.ProductObjectId'
, ProductionProduct.ProductName as 'ProductionProduct.ProductName'
, ProductionProduct.SupplierId as 'ProductionProduct.SupplierId'
, ProductionProduct.CategoryId as 'ProductionProduct.CategoryId'
, ProductionProduct.UnitPrice as 'ProductionProduct.UnitPrice'
, ProductionProduct.Discontinued as 'ProductionProduct.Discontinued'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionProduct
, (
select distinct
ProductionCategory.CategoryId as 'ProductionCategory.CategoryObjectId'
, ProductionCategory.CategoryName as 'ProductionCategory.CategoryName'
, ProductionCategory.Description as 'ProductionCategory.Description'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionCategory
, (
select distinct
ProductionSupplier.SupplierId as 'ProductionSupplier.SupplierObjectId'
, ProductionSupplier.SupplierCompanyName as 'ProductionSupplier.SupplierCompanyName'
, ProductionSupplier.SupplierContactName as 'ProductionSupplier.SupplierContactName'
, ProductionSupplier.SupplierPhoneNumber as 'ProductionSupplier.SupplierPhoneNumber'
, ProductionSupplier.SupplierFaxNumber as 'ProductionSupplier.SupplierFaxNumber'
from Production.Product as ProductionProduct
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionSupplier
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as OrderDetail
on OrderDetail.ProductId = ProductionProduct.ProductId
where OrderDetail.OrderId = @orderID
);
GO
/ Object: UserDefinedFunction [Sales].[utvf_FindAllCustomerOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [Sales].[utvf_FindAllCustomerOrder]
(
@CustomerId as int
)
returns table
as
return
(
select o.OrderId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.EmployeeId
, o.ShipperId
, sh.ShipperCompanyName
, p.ProductName
, su.SupplierCompanyName
, su.SupplierCountry
, o.Freight
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmount
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmountDiscounted
, od.Quantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId
inner join Production.Supplier as su
on su.SupplierId = p.SupplierId
inner join Sales.Shipper as sh
on sh.ShipperId = o.ShipperId
where o.CustomerId = @CustomerId
);
GO
/ Object: UserDefinedFunction [Sales].[utvf_FindAllCustomerOrdersByCustomerId] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [Sales].[utvf_FindAllCustomerOrdersByCustomerId]
(
@CustomerId as int
)
returns table
as
return
(
select p.ProductName
, o.OrderId
, o.OrderDate
, od.UnitPrice
, od.Quantity
, od.DiscountPercentage
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId
where o.CustomerId = @CustomerId
);
GO
/ Object: UserDefinedFunction [Sales].[utvf_GetAllCustomerOrdersByCustId] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [Sales].[utvf_GetAllCustomerOrdersByCustId]
(
@CustomerId as int
)
returns table
as
return
(
select @CustomerId as CustIdParameter
,c.CustomerCompanyName
, o.orderId
, o.OrderDate
from Sales.[Order] as o
inner join Sales.Customer as c
on c.CustomerId = o.CustomerId
where c.customerid = @CustomerId
);
GO
/****** Object: UserDefinedFunction [Production].[LowestPriceProducts] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [Production].[LowestPriceProducts]
(@SupplierId AS INT, @NumberOfRows AS INT)
RETURNS TABLE
AS
RETURN
SELECT TOP (@NumberOfRows)
p.ProductId, p.ProductName,p.UnitPrice
FROM Production.Product as p
WHERE supplierid = @SupplierId
ORDER BY unitprice;
GO
/****** Object: View [Utils].[uvw_FindColumnDefinitionPlusDefaultAndCheckConstraint] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [Utils].[uvw_FindColumnDefinitionPlusDefaultAndCheckConstraint]
as
select concat(tbl.TABLE_SCHEMA, '.', tbl.TABLE_NAME) as FullyQualifiedTableName
, tbl.TABLE_SCHEMA as SchemaName
, tbl.TABLE_NAME as TableName
, col.COLUMN_NAME as ColumnName
, col.ORDINAL_POSITION as OrdinalPosition
, concat(col.DOMAIN_SCHEMA, '.', col.DOMAIN_NAME) as FullyQualifiedDomainName
, col.DOMAIN_NAME as DomainName
, case
when col.DATA_TYPE = 'varchar' then
concat('varchar(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'char' then
concat('char(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'nvarchar' then
concat('nvarchar(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'nchar' then
concat('nchar(', CHARACTER_MAXIMUM_LENGTH, ')')
when col.DATA_TYPE = 'numeric' then
concat('numeric(', NUMERIC_PRECISION_RADIX, ', ', NUMERIC_SCALE, ')')
when col.DATA_TYPE = 'decimal' then
concat('decimal(', NUMERIC_PRECISION_RADIX, ', ', NUMERIC_SCALE, ')')
else
col.DATA_TYPE
end as DataType
, col.IS_NULLABLE as IsNullable
, dcn.DefaultName
, col.COLUMN_DEFAULT as DefaultNameDefinition
, cc.CONSTRAINT_NAME as CheckConstraintRuleName
, cc.CHECK_CLAUSE as CheckConstraintRuleNameDefinition
from
(
select TABLE_CATALOG
, TABLE_SCHEMA
, TABLE_NAME
, TABLE_TYPE
from INFORMATION_SCHEMA.TABLES
where (TABLE_TYPE = 'BASE TABLE')
) as tbl
inner join
(
select TABLE_CATALOG
, TABLE_SCHEMA
, TABLE_NAME
, COLUMN_NAME
, ORDINAL_POSITION
, COLUMN_DEFAULT
, IS_NULLABLE
, DATA_TYPE
, CHARACTER_MAXIMUM_LENGTH
, CHARACTER_OCTET_LENGTH
, NUMERIC_PRECISION
, NUMERIC_PRECISION_RADIX
, NUMERIC_SCALE
, DATETIME_PRECISION
, CHARACTER_SET_CATALOG
, CHARACTER_SET_SCHEMA
, CHARACTER_SET_NAME
, COLLATION_CATALOG
, COLLATION_SCHEMA
, COLLATION_NAME
, DOMAIN_CATALOG
, DOMAIN_SCHEMA
, DOMAIN_NAME
from INFORMATION_SCHEMA.COLUMNS
) as col
on col.TABLE_CATALOG = tbl.TABLE_CATALOG
and col.TABLE_SCHEMA = tbl.TABLE_SCHEMA
and col.TABLE_NAME = tbl.TABLE_NAME
left outer join
(
select t.name as TableName
, schema_name(s.schema_id) as SchemaName
, ac.name as ColumnName
, d.name as DefaultName
from sys.all_columns as ac
inner join sys.tables as t
on ac.object_id = t.object_id
inner join sys.schemas as s
on t.schema_id = s.schema_id
inner join sys.default_constraints as d
on ac.default_object_id = d.object_id
) as dcn
on dcn.SchemaName = tbl.TABLE_SCHEMA
and dcn.TableName = tbl.TABLE_NAME
and dcn.ColumnName = col.COLUMN_NAME
left outer join
(
select cu.TABLE_CATALOG
, cu.TABLE_SCHEMA
, cu.TABLE_NAME
, cu.COLUMN_NAME
, c.CONSTRAINT_CATALOG
, c.CONSTRAINT_SCHEMA
, c.CONSTRAINT_NAME
, c.CHECK_CLAUSE
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as cu
inner join INFORMATION_SCHEMA.CHECK_CONSTRAINTS as c
on c.CONSTRAINT_NAME = cu.CONSTRAINT_NAME
) as cc
on cc.TABLE_SCHEMA = tbl.TABLE_SCHEMA
and cc.TABLE_NAME = tbl.TABLE_NAME
and cc.COLUMN_NAME = col.COLUMN_NAME;
GO
/****** Object: View [Utils].[uw_FindAllDatabaseColumnsOnTheServer] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Utils].[uw_FindAllDatabaseColumnsOnTheServer] as
select vw.FullyQualifiedTableName
,vw.SchemaName
, vw.ColumnName
, vw.OrdinalPosition
, vw.FullyQualifiedDomainName
, vw.DataType
, vw.IsNullable
, vw.DefaultName
, vw.DefaultNameDefinition
, vw.CheckConstraintRuleName
, vw.CheckConstraintRuleNameDefinition
, @@ServerName as 'Server Name' -- The database server's machine name
, @@Version as [Target Database Version]
, @@servicename as 'Instance Name' -- e.g.: MSSQLSERVER
, db_name() as 'Database Name'
, host_name() as 'Host Name' -- The database client's machine name
from Utils.uvw_FindColumnDefinitionPlusDefaultAndCheckConstraint as vw
where vw.SchemaName in ('Sales','Production', 'HumanResources')
GO
/****** Object: View [Example].[uvw_OrderTotalCost] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Example].[uvw_OrderTotalCost]
as
select header.OrderId
, header.EmployeeName
, header.CustomerName
, cast(header.Freight + Line.OrderDetailCost as money) as TotalOrderCost
, header.OrderDate
, header.OrderYear
, header.OrderMonth
, Line.NumberOfProductsPurchased
from
(
select o.OrderId
, concat(e.EmployeeFirstName, ' ', e.EmployeeLastName) as EmployeeName
, case
when o.CustomerId = 71 then
'Customer I'
when o.CustomerId = 20 then
'Customer II'
when o.CustomerId = 63 then
'Customer III'
when o.CustomerId = 24 then
'Customer IV'
else
'Error'
end as CustomerName
, o.Freight
, o.OrderDate
, year(o.OrderDate) as OrderYear
, month(o.OrderDate) as OrderMonth
from Sales.[Order] as o
inner join HumanResources.Employee as e
on e.EmployeeId = o.EmployeeId
where o.CustomerId in ( 71, 20, 63, 24 )
) as header
inner join
(
select o.OrderId
, count(od.ProductId) as NumberOfProductsPurchased
, sum(od.UnitPrice * od.Quantity * (1.0 - od.DiscountPercentage)) as OrderDetailCost
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.OrderId
) as Line
on Line.OrderId = header.OrderId;
GO
/ Object: View [Example].[uvwCountryCustomers] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Example].[uvwCountryCustomers]
as
select c.CustomerId
, c.CustomerCompanyName
, c.CustomerCountry
, c.CustomerContactTitle
from Sales.Customer as c
GO
/ Object: View [JsonOutput].[uvw_HumanResourcesEmployee] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_HumanResourcesEmployee] as
--for json auto, include_null_values;
SELECT E.EmployeeId as [EmployeeObjectId]
, E.EmployeeLastName
, E.EmployeeFirstName
, E.EmployeeTitle
, E.EmployeeTitleOfCourtesy
, E.BirthDate
, E.HireDate
, E.EmployeeAddress
, E.EmployeeCity
, E.EmployeeRegion
, E.EmployeePostalCode
, E.EmployeeCountry
, E.EmployeePhoneNumber
, E.EmployeeManagerId FROM HumanResources.Employee as E
GO
/ Object: View [JsonOutput].[uvw_MongoDBOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_MongoDBOrder]
as
select o.OrderId as OrderObjectId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.Freight
, o.ShipToName
, o.ShipToAddress
, o.ShipToCity
, o.ShipToRegion
, o.ShipToPostalCode
, o.ShipToCountry
, (
select SalesCustomer.CustomerId as 'CustomerObjectId'
, SalesCustomer.CustomerCompanyName as 'CustomerCompanyName'
, SalesCustomer.CustomerContactName as 'CustomerContactName'
, SalesCustomer.CustomerPhoneNumber as 'CustomerPhoneNumber'
, SalesCustomer.CustomerFaxNumber as 'CustomerFaxNumber'
from Sales.[Order] as o2
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesCustomer
, (
select HumanResourcesEmployee.EmployeeId as 'EmployeeObjectId'
, HumanResourcesEmployee.EmployeeLastName as 'EmployeeLastName'
, HumanResourcesEmployee.EmployeeFirstName as 'EmployeeFirstName'
, HumanResourcesEmployee.EmployeePhoneNumber as 'EmployeePhoneNumber'
, HumanResourcesEmployee.EmployeeManagerId as 'EmployeeManagerId'
from Sales.[Order] as o2
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o2.EmployeeId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as HumanResourcesEmployee
, (
select SalesShipper.ShipperId as 'ShipperObjectId'
, SalesShipper.ShipperCompanyName as 'ShipperCompanyName'
, SalesShipper.PhoneNumber as 'PhoneNumber'
from Sales.[Order] as o2
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o2.ShipperId
where o.OrderId = o2.OrderId
for json path, include_null_values
) as SalesShipper
from Sales.[Order] as o
inner join Sales.Customer as SalesCustomer
on SalesCustomer.CustomerId = o.CustomerId
inner join HumanResources.Employee as HumanResourcesEmployee
on HumanResourcesEmployee.EmployeeId = o.EmployeeId
inner join Sales.Shipper as SalesShipper
on SalesShipper.ShipperId = o.ShipperId;
GO
/ Object: View [JsonOutput].[uvw_MongoDBOrderdetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_MongoDBOrderdetail] as
select OrderDetail.OrderId as OrderObjectId
, row_number() over (partition by OrderDetail.OrderId order by OrderDetail.OrderId) as LineItemNumer
, OrderDetail.UnitPrice
, OrderDetail.Quantity
, OrderDetail.DiscountPercentage
, OrderDetail.UnitPrice * OrderDetail.Quantity as TotalCost
, (OrderDetail.UnitPrice * OrderDetail.Quantity) * (1. - OrderDetail.DiscountPercentage) as TotalDiscountedCost
, OrderDetail.OrderId
, (
select distinct
ProductionProduct.ProductId as 'ProductionProduct.ProductObjectId'
, ProductionProduct.ProductName as 'ProductionProduct.ProductName'
, ProductionProduct.SupplierId as 'ProductionProduct.SupplierId'
, ProductionProduct.CategoryId as 'ProductionProduct.CategoryId'
, ProductionProduct.UnitPrice as 'ProductionProduct.UnitPrice'
, ProductionProduct.Discontinued as 'ProductionProduct.Discontinued'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionProduct
, (
select distinct
ProductionCategory.CategoryId as 'ProductionCategory.CategoryObjectId'
, ProductionCategory.CategoryName as 'ProductionCategory.CategoryName'
, ProductionCategory.Description as 'ProductionCategory.Description'
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionCategory
, (
select distinct
ProductionSupplier.SupplierId as 'ProductionSupplier.SupplierObjectId'
, ProductionSupplier.SupplierCompanyName as 'ProductionSupplier.SupplierCompanyName'
, ProductionSupplier.SupplierContactName as 'ProductionSupplier.SupplierContactName'
, ProductionSupplier.SupplierPhoneNumber as 'ProductionSupplier.SupplierPhoneNumber'
, ProductionSupplier.SupplierFaxNumber as 'ProductionSupplier.SupplierFaxNumber'
from Production.Product as ProductionProduct
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as od
on OrderDetail.ProductId = ProductionProduct.ProductId
where od.OrderId = OrderDetail.OrderId
for json path, include_null_values
) as ProductionSupplier
from Production.Product as ProductionProduct
inner join Production.Category as ProductionCategory
on ProductionCategory.CategoryId = ProductionProduct.CategoryId
inner join Production.Supplier as ProductionSupplier
on ProductionSupplier.SupplierId = ProductionProduct.SupplierId
inner join Sales.OrderDetail as OrderDetail
on OrderDetail.ProductId = ProductionProduct.ProductId
GO
/ Object: View [JsonOutput].[uvw_Order] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_Order] as
SELECT O.OrderId
, O.CustomerId
, O.EmployeeId
, O.ShipperId
, O.OrderDate
, O.RequiredDate
, O.ShipToDate
, O.Freight
, O.ShipToName
, O.ShipToAddress
, O.ShipToCity
, O.ShipToRegion
, O.ShipToPostalCode
, O.ShipToCountry
FROM Sales.[Order] as O
GO
/ Object: View [JsonOutput].[uvw_OrderDetail] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_OrderDetail] as
SELECT OD.OrderId
, OD.ProductId
, OD.UnitPrice
, OD.Quantity
, OD.DiscountPercentage
FROM Sales.OrderDetail as OD
GO
/ Object: View [JsonOutput].[uvw_ProductionCategory] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [JsonOutput].[uvw_ProductionCategory] as
--for json auto, include_null_values;
select CategoryId as [CategoryObjectId]
,[CategoryName]
,[Description]
FROM [Production].[Category]
GO
/ Object: View [JsonOutput].[uvw_ProductionProduct] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_ProductionProduct] as
--for json auto, include_null_values;
select
P.ProductId as ProductObjectId
, P.ProductName
, P.SupplierId
, P.CategoryId
, P.UnitPrice
, P.Discontinued
, C.CategoryId as 'Production.CategoryObjectId'
, C.CategoryName as 'Production.CategoryName'
, C.Description as 'Production.Description'
, S.SupplierId as 'Production.SupplierId'
, S.SupplierCompanyName as 'Production.SupplierCompanyName'
, S.SupplierContactName as 'Production.SupplierContactName'
, S.SupplierPhoneNumber as 'Production.SupplierPhoneNumber'
, S.SupplierFaxNumber as 'Production.SupplierFaxNumber'
from Production.Product as P
inner join Production.Category as C
on C.CategoryId = P.CategoryId
inner join Production.Supplier as S
on S.SupplierId = P.SupplierId
GO
/ Object: View [JsonOutput].[uvw_ProductionSupplier] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_ProductionSupplier] as
--for json auto, include_null_values;
select S.SupplierId as SupplierObjectId
, S.SupplierCompanyName
, S.SupplierContactName
, S.SupplierContactTitle
, S.SupplierAddress
, S.SupplierCity
, S.SupplierRegion
, S.SupplierPostalCode
, S.SupplierCountry
, S.SupplierPhoneNumber
, S.SupplierFaxNumber
from Production.Supplier as S
GO
/ Object: View [JsonOutput].[uvw_SalesCustomer] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_SalesCustomer] as
--for json auto, include_null_values;
select C.CustomerId as CustomerObjectId
, C.CustomerCompanyName
, C.CustomerContactName
, C.CustomerContactTitle
, C.CustomerAddress
, C.CustomerCity
, C.CustomerRegion
, C.CustomerPostalCode
, C.CustomerCountry
, C.CustomerPhoneNumber
, C.CustomerFaxNumber
from Sales.Customer as C
GO
/ Object: View [JsonOutput].[uvw_SalesShipper] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [JsonOutput].[uvw_SalesShipper] as
--for json auto, include_null_values;
SELECT S.ShipperId as ShipperObjectId
, S.ShipperCompanyName
, S.PhoneNumber FROM Sales.Shipper as S
GO
/ Object: View [Sales].[uvw_CustomerOrder] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_CustomerOrder]
with schemabinding
as
select
o.CustomerId,
dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date)) as OrderMonth,
sum(od.Quantity) as TotalQuantity
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by CustomerId, dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date));
GO
/****** Object: View [Sales].[uvw_CustomerOrderAndOrderDetail] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [Sales].[uvw_CustomerOrderAndOrderDetail]
as
select c.CustomerCompanyName
, c.CustomerCountry
, c.CustomerRegion
, c.CustomerCity
, c.CustomerPostalCode
,c.CustomerId
, o.OrderId
, o.EmployeeId
, o.ShipperId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.Freight
, od.ProductId
, od.Quantity as TotalQuantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.Customer as c
inner join Sales.[Order] as o
on o.CustomerId = c.CustomerId
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
;
GO
/****** Object: View [Sales].[uvw_CustomerProductOrderAndOrderDetail] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_CustomerProductOrderAndOrderDetail]
as
select c.CustomerCompanyName
, o.OrderId
, o.OrderDate
, o.EmployeeId
, o.ShipperId
, p.ProductName
, o.Freight
,TotalDiscountedLineAmount =
(od.Quantity* od.UnitPrice) * (1.0-od.DiscountPercentage)
, od.Quantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.Customer as c
inner join Sales.[Order] as o
on o.CustomerId = c.CustomerId
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId;
GO
/****** Object: View [Sales].[uvw_EmployeeOrder] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_EmployeeOrder]
with schemabinding
as
select
o.EmployeeId,
dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date)) as OrderMonth,
sum(od.Quantity) as TotalQuantity,
cast(sum(od.Quantity * od.UnitPrice * (1 - od.DiscountPercentage))
as numeric(12, 2)) as TotalDiscountedAmount,
count() as TotalNumberOfOrders
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.EmployeeId, dateadd(month, datediff(month, cast('19000101' as date), o.OrderDate), cast('19000101' as date));
GO
/***** Object: View [Sales].[uvw_FindAllCustomerOrder] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [Sales].[uvw_FindAllCustomerOrder]
as
select o.CustomerId
, o.OrderId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, o.EmployeeId
, o.ShipperId
, sh.ShipperCompanyName
, p.ProductName
, su.SupplierCompanyName
, su.SupplierCountry
, o.Freight
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmount
, (od.Quantity * od.UnitPrice) * (1.0 - od.DiscountPercentage) as LineAmountDiscounted
, od.Quantity
, od.UnitPrice
, od.DiscountPercentage
from Sales.[Order] as o
inner join Sales.OrderDetail as od
on od.OrderId = o.OrderId
inner join Production.Product as p
on p.ProductId = od.ProductId
inner join Production.Supplier as su
on su.SupplierId = p.SupplierId
inner join Sales.Shipper as sh
on sh.ShipperId = o.ShipperId;
GO
/ Object: View [Sales].[uvw_OrderTotalQuantityAndTotalDiscountedAmount] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Create Views and Functions
create view [Sales].[uvw_OrderTotalQuantityAndTotalDiscountedAmount]
with schemabinding
as
select o.OrderId
, o.CustomerId
, o.EmployeeId
, o.ShipperId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate
, sum(od.Quantity) as TotalQuantity
, cast(sum(od.Quantity * od.UnitPrice * (1 - od.DiscountPercentage)) as numeric(12, 2)) as [TotalDiscountedAmount]
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by o.OrderId
, o.CustomerId
, o.EmployeeId
, o.ShipperId
, o.OrderDate
, o.RequiredDate
, o.ShipToDate;
GO
/****** Object: View [Sales].[uvw_OrderTotalQuantityByYear] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [Sales].[uvw_OrderTotalQuantityByYear]
with schemabinding
as
select year(o.OrderDate) as OrderYear
, sum(od.Quantity) as TotalQuantity
from Sales.[Order] as o
join Sales.OrderDetail as od
on od.OrderId = o.OrderId
group by year(o.OrderDate);
GO
/****** Object: Table [dbo].[Digits] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Digits](
[digit] [int] NOT NULL
) ON [PRIMARY]
GO
/ Object: Table [RelationalCalculii].[USSupremeCourtJustices] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [RelationalCalculii].[USSupremeCourtJustices](
[JusticeId] [int] NOT NULL,
[LastName] nvarchar NOT NULL,
[FirstName] nvarchar NOT NULL,
[StateOfBirth] nvarchar NOT NULL,
[YearOfBirth] nchar NOT NULL
) ON [PRIMARY]
GO
/ Object: Table [RelationalCrossJoin].[S1] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [RelationalCrossJoin].[S1](
[Letter] char NULL
) ON [PRIMARY]
GO
/ Object: Table [RelationalCrossJoin].[S2] Script Date: 7/5/2024 6:08:18 AM /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [RelationalCrossJoin].[S2](
[Number] char NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/ Object: Index [NCI_UniqueCategoryName_Idx] Script Date: 7/5/2024 6:08:18 AM /
CREATE UNIQUE NONCLUSTERED INDEX [NCI_UniqueCategoryName_Idx] ON [Production].[Category]
(
[CategoryName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/ Object: Index [XIE1SupplierContactName] Script Date: 7/5/2024 6:08:18 AM /
CREATE NONCLUSTERED INDEX [XIE1SupplierContactName] ON [Production].[Supplier]
(
[SupplierContactName] ASC
)
INCLUDE([SupplierContactTitle],[SupplierAddress],[SupplierCity],[SupplierCountry],[SupplierPhoneNumber]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/ Object: Index [XIE1CustomerContactName] Script Date: 7/5/2024 6:08:18 AM /
CREATE NONCLUSTERED INDEX [XIE1CustomerContactName] ON [Sales].[Customer]
(
[CustomerContactName] ASC
)
INCLUDE([CustomerCompanyName],[CustomerContactTitle],[CustomerAddress],[CustomerCity],[CustomerCountry],[CustomerPhoneNumber],[CustomerPostalCode]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
/ Object: Index [XAK1OrderDate] Script Date: 7/5/2024 6:08:18 AM /
CREATE NONCLUSTERED INDEX [XAK1OrderDate] ON [Sales].[Order]
(
[OrderDate] ASC
)
INCLUDE([OrderId],[CustomerId],[EmployeeId],[ShipperId],[RequiredDate],[ShipToDate],[Freight]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
ALTER TABLE [Production].[Product] ADD CONSTRAINT [DFT_Products_unitprice] DEFAULT ((0)) FOR [UnitPrice]
GO
ALTER TABLE [Production].[Product] ADD CONSTRAINT [DFT_Products_discontinued] DEFAULT ((0)) FOR [Discontinued]
GO
ALTER TABLE [Sales].[Order] ADD CONSTRAINT [DFT_Orders_freight] DEFAULT ((0)) FOR [Freight]
GO
ALTER TABLE [Sales].[OrderDetail] ADD CONSTRAINT [DFT_OrderDetails_UnitPrice] DEFAULT ((0)) FOR [UnitPrice]
GO
ALTER TABLE [Sales].[OrderDetail] ADD CONSTRAINT [DFT_OrderDetails_Quantity] DEFAULT ((1)) FOR [Quantity]
GO
ALTER TABLE [Sales].[OrderDetail] ADD CONSTRAINT [DFT_OrderDetails_DiscountPercentage] DEFAULT ((0)) FOR [DiscountPercentage]
GO
ALTER TABLE [HumanResources].[Employee] WITH CHECK ADD CONSTRAINT [FK_Employees_Employees] FOREIGN KEY([EmployeeManagerId])
REFERENCES [HumanResources].[Employee] ([EmployeeId])
GO
ALTER TABLE [HumanResources].[Employee] CHECK CONSTRAINT [FK_Employees_Employees]
GO
ALTER TABLE [Production].[Product] WITH CHECK ADD CONSTRAINT [FK_Product_Category] FOREIGN KEY([CategoryId])
REFERENCES [Production].[Category] ([CategoryId])
GO
ALTER TABLE [Production].[Product] CHECK CONSTRAINT [FK_Product_Category]
GO
ALTER TABLE [Production].[Product] WITH CHECK ADD CONSTRAINT [FK_product_supplier] FOREIGN KEY([SupplierId])
REFERENCES [Production].[Supplier] ([SupplierId])
GO
ALTER TABLE [Production].[Product] CHECK CONSTRAINT [FK_product_supplier]
GO
ALTER TABLE [Sales].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Customer1] FOREIGN KEY([CustomerId])
REFERENCES [Sales].[Customer] ([CustomerId])
GO
ALTER TABLE [Sales].[Order] CHECK CONSTRAINT [FK_Order_Customer1]
GO
ALTER TABLE [Sales].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Employee] FOREIGN KEY([EmployeeId])
REFERENCES [HumanResources].[Employee] ([EmployeeId])
GO
ALTER TABLE [Sales].[Order] CHECK CONSTRAINT [FK_Order_Employee]
GO
ALTER TABLE [Sales].[Order] WITH CHECK ADD CONSTRAINT [FK_Order_Shipper] FOREIGN KEY([ShipperId])
REFERENCES [Sales].[Shipper] ([ShipperId])
GO
ALTER TABLE [Sales].[Order] CHECK CONSTRAINT [FK_Order_Shipper]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [FK_OrderDetail_Order] FOREIGN KEY([OrderId])
REFERENCES [Sales].[Order] ([OrderId])
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [FK_OrderDetail_Order]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [FK_OrderDetail_Product] FOREIGN KEY([ProductId])
REFERENCES [Production].[Product] ([ProductId])
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [FK_OrderDetail_Product]
GO
ALTER TABLE [HumanResources].[Employee] WITH CHECK ADD CONSTRAINT [CHK_birthdate] CHECK (([BirthDate]<=CONVERT([date],sysdatetime())))
GO
ALTER TABLE [HumanResources].[Employee] CHECK CONSTRAINT [CHK_birthdate]
GO
ALTER TABLE [Production].[Product] WITH CHECK ADD CONSTRAINT [CHK_Products_unitprice] CHECK (([UnitPrice]>=(0)))
GO
ALTER TABLE [Production].[Product] CHECK CONSTRAINT [CHK_Products_unitprice]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [CHK_DiscountPercentage] CHECK (([DiscountPercentage]>=(0) AND [DiscountPercentage]<=(1)))
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [CHK_DiscountPercentage]
GO
ALTER TABLE [Sales].[OrderDetail] WITH CHECK ADD CONSTRAINT [CHK_Quantity] CHECK (([Quantity]>(0)))
GO
ALTER TABLE [Sales].[OrderDetail] CHECK CONSTRAINT [CHK_Quantity]
GO
/ Object: StoredProcedure [JsonOutput].[uspHumanResourcesEmployee] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date:
-- Description:
-- =============================================
create procedure [JsonOutput].[uspHumanResourcesEmployee]
AS
BEGIN
select e.EmployeeId as [EmployeeObjectId]
, e.EmployeeLastName
, e.EmployeeFirstName
, e.EmployeeTitle
, e.EmployeeTitleOfCourtesy
, e.BirthDate
, e.HireDate
, e.EmployeeAddress
, e.EmployeeCity
, e.EmployeeRegion
, e.EmployeePostalCode
, e.EmployeeCountry
, e.EmployeePhoneNumber
, E.EmployeeManagerId from HumanResources.Employee as E
for json auto, include_null_values;
END
GO
/****** Object: StoredProcedure [Sales].[usp_getorders] Script Date: 7/5/2024 6:08:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [Sales].[usp_getorders]
@Country as nvarchar(40)
as
select orderid
, orderdate
, empid
, custid
from Sales.Orders
where shipcountry = @Country;
GO
USE [master]
GO
ALTER DATABASE [Northwinds2024Student] SET READ_WRITE
GO
Beta Was this translation helpful? Give feedback.
All reactions