I have the below table definition and I want to add system versioning to it but I can't seem to get it into the WITH clause without SQL Server complaining.
CREATE TABLE [dbo].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[SysStartTime] [datetime2](7) NOT NULL,
[SysEndTime] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Employee_1] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Here's the system versioning WITH clause.
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE=[dbo].[EmployeeHistory], DATA_CONSISTENCY_CHECK=ON));
It's probably easiest and most succinct to create your table and then add the required system versioning elements:
create table [dbo].[Employee](
[EmployeeId] [int] identity(1,1) not null,
[FirstName] [varchar](50) null,
[LastName] [varchar](50) null
constraint [PK_Employee_1] primary key clustered
(
[EmployeeId] asc
)with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [PRIMARY]
) on [PRIMARY]
alter table dbo.Employee add
SysStartTime datetime2 generated always as row start constraint DF_EmployeeSysStart default SysUtcDateTime(),
SysEndTime datetime2 generated always as row end constraint DF_EmployeeSysEnd default Convert(datetime2, '9999-12-31 23:59:59.9999999'),
period for system_time (SysStartTime,SysEndTime);
alter table Employee set (system_versioning = on (history_table = dbo.EmployeeHistory, data_consistency_check=on));
Edit
To combine in a single create table
create table [dbo].[Employee](
[EmployeeId] [int] identity(1,1) not null,
[FirstName] [varchar](50) null,
[LastName] [varchar](50) null,
[SysStartTime] datetime2 generated always as row start constraint DF_EmployeeSysStart default SysUtcDateTime(),
[SysEndTime] datetime2 generated always as row end hidden constraint DF_EmployeeSysEnd default Convert(datetime2, '9999-12-31 23:59:59.9999999'),
period for system_time (SysStartTime,SysEndTime),
constraint [PK_Employee_1] primary key clustered ( [EmployeeId] asc) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on)
) with ( system_versioning = on (history_table = dbo.EmployeeHistory, data_consistency_check=on))
Related
I have a temporal table defined as:
CREATE TABLE [dbo].[MyTable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](200) NOT NULL,
[Description] [varchar](100) NULL,
[CreatedOn] [datetime] NULL,
[LastUpdatedOn] [datetime] NULL,
[SysStartTime] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL,
[SysEndTime] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
) ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [dbo].[MyTable_History] )
)
and when I try to do a query like this:
SELECT * FROM MyTable FOR SYSTEM_TIME from '2020-08-18 00:00:00' to '2020-08-18 23:59:59'
I seem to get rows that are not in the range as well. For example,
Name1 Description1 2018-07-05 22:53:36.543 2020-03-17 16:25:44.707 2020-07-22 18:21:03.9662004 9999-12-31 23:59:59.9999999
2020-07-22 is not in the range but why is it showing up?
I have 3 tables . Orders, OrderLines, BillLines. The primary key of Orders is a FK in the 2 other tables.
I am trying to query the client debit and credit amount.
The Action table just describe the type of the transaction
The Subindex table stores the clients names and ids
CREATE TABLE [dbo].[Orders](
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[Ordertype] [nvarchar](max) NULL,
[Orderdate] [datetime2](7) NOT NULL,
[transactionNo] [int] NOT NULL,
[DocumentNo] [int] NOT NULL,
[ordernote] [nvarchar](max) NULL,
[Profittax] [decimal](18, 2) NOT NULL,
[DescDR] [decimal](18, 2) NULL,
[DescCR] [decimal](18, 2) NULL,
[ActionsID] [smallint] NOT NULL,
[SubIndexesID] [smallint] NULL,
CONSTRAINT [PK_dbo.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) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_dbo.Orders_dbo.Actions_ActionsID] FOREIGN KEY([ActionsID])
REFERENCES [dbo].[Actions] ([ActionsID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_dbo.Orders_dbo.Actions_ActionsID]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_dbo.Orders_dbo.SubIndexes_SubIndexesID] FOREIGN KEY([SubIndexesID])
REFERENCES [dbo].[SubIndexes] ([SubIndexesID])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_dbo.Orders_dbo.SubIndexes_SubIndexesID]
GO
CREATE TABLE [dbo].[Orderlines](
[OrderlinesID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NOT NULL,
[Qin] [smallint] NOT NULL,
[Qout] [smallint] NOT NULL,
[Price] [decimal](18, 2) NOT NULL,
[currency] [nvarchar](max) NULL,
[Discount] [decimal](18, 2) NOT NULL,
[VAT] [decimal](18, 2) NOT NULL,
[ItemsID] [smallint] NOT NULL,
[Total] [decimal](18, 2) NOT NULL,
[Vatvalue] [decimal](18, 2) NOT NULL,
[Net] [decimal](18, 2) NOT NULL,
CONSTRAINT [PK_dbo.Orderlines] PRIMARY KEY CLUSTERED
(
[OrderlinesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orderlines] ADD DEFAULT ((0)) FOR [Total]
GO
ALTER TABLE [dbo].[Orderlines] ADD DEFAULT ((0)) FOR [Vatvalue]
GO
ALTER TABLE [dbo].[Orderlines] ADD DEFAULT ((0)) FOR [Net]
GO
ALTER TABLE [dbo].[Orderlines] WITH CHECK ADD CONSTRAINT [FK_dbo.Orderlines_dbo.Items_ItemsID] FOREIGN KEY([ItemsID])
REFERENCES [dbo].[Items] ([ItemsID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Orderlines] CHECK CONSTRAINT [FK_dbo.Orderlines_dbo.Items_ItemsID]
GO
ALTER TABLE [dbo].[Orderlines] WITH CHECK ADD CONSTRAINT [FK_dbo.Orderlines_dbo.Orders_OrderID] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Orderlines] CHECK CONSTRAINT [FK_dbo.Orderlines_dbo.Orders_OrderID]
GO
CREATE TABLE [dbo].[BillLines](
[BillLinesID] [int] IDENTITY(1,1) NOT NULL,
[VALIN] [decimal](18, 2) NULL,
[VALOut] [decimal](18, 2) NULL,
[OrderID] [int] NOT NULL,
[SubIndexesID] [smallint] NULL,
CONSTRAINT [PK_dbo.BillLines] PRIMARY KEY CLUSTERED
(
[BillLinesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BillLines] WITH CHECK ADD CONSTRAINT [FK_dbo.BillLines_dbo.Orders_OrderID] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[BillLines] CHECK CONSTRAINT [FK_dbo.BillLines_dbo.Orders_OrderID]
GO
ALTER TABLE [dbo].[BillLines] WITH CHECK ADD CONSTRAINT [FK_dbo.BillLines_dbo.SubIndexes_SubIndexesID] FOREIGN KEY([SubIndexesID])
REFERENCES [dbo].[SubIndexes] ([SubIndexesID])
GO
ALTER TABLE [dbo].[BillLines] CHECK CONSTRAINT [FK_dbo.BillLines_dbo.SubIndexes_SubIndexesID]
GO
CREATE TABLE [dbo].[Actions](
[ActionsID] [smallint] IDENTITY(1,1) NOT NULL,
[Actionname] [nvarchar](4000) NOT NULL,
CONSTRAINT [PK_dbo.Actions] PRIMARY KEY CLUSTERED
(
[ActionsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[SubIndexes](
[SubIndexesID] [smallint] IDENTITY(1,1) NOT NULL,
[Subname] [nvarchar](max) NULL,
[AccountIndexID] [smallint] NOT NULL,
[Contacttype] [nvarchar](max) NULL,
[phones] [nvarchar](max) NULL,
[fax] [nvarchar](max) NULL,
[email] [nvarchar](max) NULL,
[address] [nvarchar](max) NULL,
[zip] [nvarchar](max) NULL,
[Website] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.SubIndexes] PRIMARY KEY CLUSTERED
(
[SubIndexesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[SubIndexes] WITH CHECK ADD CONSTRAINT [FK_dbo.SubIndexes_dbo.AccountIndexes_AccountIndexID] FOREIGN KEY([AccountIndexID])
REFERENCES [dbo].[AccountIndexes] ([AccountIndexID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[SubIndexes] CHECK CONSTRAINT [FK_dbo.SubIndexes_dbo.AccountIndexes_AccountIndexID]
GO
My Query is
select distinct
o.OrderID ,
s.Subname as subindexname,
o.SubIndexesID as Subindex ,
sum(ol.Qout * (ol.Price - ol.Discount)* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALOut) -iif(o.ActionsID between 3 and 4, o.DescDR+o.DescCR,0) as Debit ,
sum(ol.Qin * (ol.Price - ol.Discount )* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALIN)-iif(o.ActionsID not between 3 and 4,o.DescCR + o.DescDR,0) as Credit
from Orders o
inner join
Orderlines ol on o.OrderID = ol.OrderID
inner join
BillLines bl on o.OrderID = bl.OrderID
inner join
SubIndexes s on o.SubIndexesID= s.SubIndexesID
group by o.OrderID,
o.ActionsID,
o.SubIndexesID,
o.DescCR,
o.DescDR,
ol.Qout,
ol.Qin,
s.Subname,
s.SubIndexesID,
ol.VAT,
ol.Price,
ol.Discount
having o.ActionsID=2
or o.ActionsID=3
or o.ActionsID=4
or o.ActionsID=5
I want to show the debit and the credit in one row even I have multiple orderlines for one order.
It is necessary to include all cols in Group-by in Select as well:
select distinct
o.OrderID ,
s.Subname as subindexname,
o.SubIndexesID as Subindex ,
o.ActionsID,
o.DescCR,
o.DescDR,
ol.Qout,
ol.Qin,
s.Subname,
s.SubIndexesID,
ol.VAT,
ol.Price,
ol.Discount
sum(ol.Qout * (ol.Price - ol.Discount)* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALOut) -iif(o.ActionsID between 3 and 4, o.DescDR+o.DescCR,0) as Debit ,
sum(ol.Qin * (ol.Price - ol.Discount )* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALIN)-iif(o.ActionsID not between 3 and 4,o.DescCR + o.DescDR,0) as Credit
from Orders o
inner join
Orderlines ol on o.OrderID = ol.OrderID
inner join
BillLines bl on o.OrderID = bl.OrderID
inner join
SubIndexes s on o.SubIndexesID= s.SubIndexesID
group by o.OrderID,
o.ActionsID,
o.SubIndexesID,
o.DescCR,
o.DescDR,
ol.Qout,
ol.Qin,
s.Subname,
s.SubIndexesID,
ol.VAT,
ol.Price,
ol.Discount
having o.ActionsID=2
or o.ActionsID=3
or o.ActionsID=4
or o.ActionsID=5
I am a beginner in SQL,
I have a table with GroupRole column and Age column.
CREATE TABLE [Persons](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](70) NULL,
[Age] [int] NULL,
[GroupRole ] [varchar](30) NULL
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[PersonID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
I want to limit the value range that can be placed in an age column lower 30
and GroupRole column equal 'Admin'.
I do not want to do this in the c# code.
How can I do it?
this have easy way:
ALTER TABLE Persons
ADD CONSTRAINT CHK_PersonAge CHECK (Age>=30 AND GroupRole ='Admin');
You need to use Check Constraints. They are part of your table's metadata and define conditions to be checked every time, when the data is modified or inserted in the table. If these conditions are not met, the DML operation will fail with an error.
You need to modify your CREATE TABLE statement as follows:
CREATE TABLE [Persons](
[PersonID] [int] IDENTITY(1,1) NOT NULL,
[FullName] [varchar](70) NULL,
[Age] [int] NULL CONSTRAINT CHK_Age CHECK ([Age] < 30),
[GroupRole] [varchar](30) NULL CONSTRAINT CHK_GroupRole CHECK ([GroupRole] in ('Admin'))
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[PersonID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
I need to create a stored procedure transaction that will pull data from two tables and create a random integer to populate a third table (dbo.patient_address).
I want the following, but I am having a hard time understanding how to script this as a stored procedure.
dbo.address AddressID = dbo.patient_address AddressID,
dbo.patient PatientID = dbo.patient_address PatientID,
RAND is an integer between 1-3 (see dbo.addresstype) = dbo.patient_address AddressTypeID
Below are create statements of all of these tables.
ADDRESS TABLE - 30000 rows in place (complete data set)
CREATE TABLE [dbo].[ADDRESS](
[AddressID] [int] NOT NULL,
[StreetAddress] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [varchar](50) NULL,
[PostalCode] [varchar](50) NULL,
CONSTRAINT [ADDRESS_PK] PRIMARY KEY CLUSTERED
(
[AddressID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
PATIENT TABLE - 30000 rows in place (complete data set)
CREATE TABLE [dbo].[PATIENT](
[PatientID] [int] NOT NULL,
[PatientFName] [varchar](50) NOT NULL,
[PatientLName] [varchar](50) NOT NULL,
[GenderID] [int] NOT NULL,
[DateOfBirth] [date] NOT NULL,
CONSTRAINT [PATIENT_PK] PRIMARY KEY CLUSTERED
(
[PatientID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ADDRESS TYPE - 3 choices (home, billing, work) – integer between 1 - 3
CREATE TABLE [dbo].[ADDRESS_TYPE](
[AddressTypeID] [int] NOT NULL,
[AddressTypeName] [varchar](10) NOT NULL,
[AddressTypeDescr] [varchar](10) NULL,
CONSTRAINT [ADDRESS_TYPE_PK] PRIMARY KEY CLUSTERED
(
[AddressTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO [dbo].[ADDRESS_TYPE]
([AddressTypeID],[AddressTypeName],[AddressTypeDescr])
Values ('1','Home','Home'), ('2','Bill','Billing'), ('3','Work','Work')
This is the final table that I want to populate from all of the above tables using a stored procedure transaction.
CREATE TABLE [dbo].[PATIENT_ADDRESS](
[PatientID] [int] NOT NULL,
[AddressID] [int] NOT NULL,
[AddressTypeID] [int] NULL,
CONSTRAINT [PATIENT_ADDRESS_PK] PRIMARY KEY CLUSTERED
(
[PatientID] ASC,
[AddressID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Try this:
INSERT INTO PATIENT_ADDRESS
SELECT PatientID, AddressID,
CONVERT(INT,(ABS(CHECKSUM(NEWID())/2148000000.))*3)+1 AS AddressTypeID
FROM (
SELECT PatientID, ROW_NUMBER() OVER (ORDER BY PatientID) AS RowNum
FROM dbo.PATIENT
) x
INNER JOIN (
SELECT AddressID, ROW_NUMBER() OVER (ORDER BY AddressID) AS RowNum
FROM dbo.ADDRESS
) y ON x.RowNum = y.RowNum
I have 2 tables. Problem is I always get multiple records no matter what I do.
Pages
SubMenus
Relation is 1 to 1 (each submenu has a page)
SubMenus sample data:
submenu_id parentmenu_id display_name url_name
----------------------------------------------------
1 1 Home home
2 1 Contact contact
Pages table data:
page_id submenu_id page_title url_name
---------------------------------------------------------
1 1 Home Page home
2 1 Contact Page null
I want to retrieve single value from there JOIN where SubMenu.UrlName == Home
(from s in SubMenus
join p in Pages on s.Id equals p.SubMenuId
where s.UrlName == "home"
select new
{
s.Id, s.UrlName,
PageId = p.Id, p.Title, p.Html,
p.MetaAuthor, p.MetaKeywords, p.MetaDescription
}).FirstOrDefault();
But if I check the SQL and run it. I get single record without any problem. Below is what SQL generated
SELECT TOP (1)
[Extent1].[submenu_id] AS [submenu_id],
[Extent1].[url_name] AS [url_name],
[Extent2].[page_id] AS [page_id],
[Extent2].[page_title] AS [page_title],
[Extent2].[page_html] AS [page_html],
[Extent2].[meta_author] AS [meta_author],
[Extent2].[meta_keywords] AS [meta_keywords],
[Extent2].[meta_description] AS [meta_description]
FROM
[dbo].[SubMenu] AS [Extent1]
INNER JOIN
[dbo].[Pages] AS [Extent2] ON [Extent1].[submenu_id] = [Extent2].[submenu_id]
WHERE
N'home' = [Extent1].[url_name]
Table structure:
CREATE TABLE [dbo].[ParentMenu]
(
[parentmenu_id] [int] IDENTITY(1,1) NOT NULL,
[group_name] [nvarchar](50) NULL,
[title] [nvarchar](100) NULL,
[active] [bit] NOT NULL,
[index_order] [int] NULL,
CONSTRAINT [PK_ParentMenu]
PRIMARY KEY CLUSTERED ([parentmenu_id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ParentMenu]
ADD CONSTRAINT [DF_ParentMenu_active] DEFAULT ((1)) FOR [active]
GO
ALTER TABLE [dbo].[ParentMenu]
ADD CONSTRAINT [DF_ParentMenu_index_order] DEFAULT ((0)) FOR [index_order]
GO
CREATE TABLE [dbo].[SubMenu]
(
[submenu_id] [int] IDENTITY(1,1) NOT NULL,
[parentmenu_id] [int] NOT NULL,
[display_name] [nvarchar](100) NULL,
[url_name] [nvarchar](100) NULL,
[index_order] [int] NULL,
[active] [bit] NOT NULL,
CONSTRAINT [PK_SubMenu]
PRIMARY KEY CLUSTERED ([submenu_id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_SubMenu_1]
UNIQUE NONCLUSTERED ([url_name] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SubMenu] WITH CHECK
ADD CONSTRAINT [FK_SubMenu_ParentMenu]
FOREIGN KEY([parentmenu_id]) REFERENCES [dbo].[ParentMenu] ([parentmenu_id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[SubMenu] CHECK CONSTRAINT [FK_SubMenu_ParentMenu]
GO
ALTER TABLE [dbo].[SubMenu]
ADD CONSTRAINT [DF_SubMenu_index_order] DEFAULT ((0)) FOR [index_order]
GO
ALTER TABLE [dbo].[SubMenu]
ADD CONSTRAINT [DF_SubMenu_active] DEFAULT ((1)) FOR [active]
GO
CREATE TABLE [dbo].[Pages]
(
[page_id] [int] IDENTITY(1,1) NOT NULL,
[submenu_id] [int] NOT NULL,
[page_title] [nvarchar](200) NULL,
[url_name] [nvarchar](100) NULL,
[page_html] [nvarchar](max) NULL,
[meta_author] [nvarchar](300) NULL,
[meta_keywords] [nvarchar](max) NULL,
[meta_description] [nvarchar](max) NULL,
[active] [bit] NOT NULL,
[creation_date] [date] NULL,
CONSTRAINT [PK_Pages]
PRIMARY KEY CLUSTERED ([page_id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Pages] WITH CHECK
ADD CONSTRAINT [FK_Pages_SubMenu]
FOREIGN KEY([submenu_id]) REFERENCES [dbo].[SubMenu] ([submenu_id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Pages] CHECK CONSTRAINT [FK_Pages_SubMenu]
GO
ALTER TABLE [dbo].[Pages]
ADD CONSTRAINT [DF_Pages_active] DEFAULT ((1)) FOR [active]
GO
You are saying that Problem is I always get multiple records no matter what I do.
The query that you show has top 1 in the very first line, it will always return just one row.
What's wrong with it?
From what I see your SQL query matches what your LINQ query does.
In this case LINQ query cannot possible return more rows than SQL returns.