Want to know which appropriate fields to use in review table.
requirement for review table:
> review will be given by customer
* review in points
* review in description
* review in videos (instead of text can upload review video in which it present review description)
* comments on reviews.
* restriction to display review in public or not
> review for menuItem
> review for restaurants
> review for packs / coupns available
can any one help me who has worked on review system.
review table contains
review_id (pk,autp_increment)
user_id(fk references usier_id in user table)
menu_items_reviews table contains
id (pk,auto_increment),
review_id (fk references review_id in review table),
review_desc (text or varchar(xxx))
this is similar for all other types reviews like restaurants and packs / coupns available
It really depends on your requirements, business rules and application high level design. However you can start with a sample design I am putting up for you.
You must have following tables to enforce these requirements at bare minimum level.
User table ( containing following fields UserId, Name, Phone, EMail)
Menu table ( containing following fields MenuId, Description)
MenuItem table ( containing following fields MenuItemId, MenuId, ItemId,CategoryId)
Item table ( containing following fields ItemId, ItemName, Description, Price, IsActive, IsSpecial)
MenuCategory table ( containing following fields CategoryId, CategoryName, Description, IsActive)
Review comment table ( containing following fields ReviewId, ReviewComment, ReviewRating, UserId, MenuItemId, ReviewDate)
Restaurant table ( containing following fields RestaurantId, Name, AddressLine1, AddressLine2)
RestaurantMenu table (containing following fields RestaurantMenuId, MenuId, RestaurantId)
Coupon table (containing following fields CouponId, Description, StartDate, EndDate, isActive, RestaurantId, [Discount%])
USE [YourDBName]
GO
/****** Object: Table [dbo].[Coupon] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Coupon](
[CouponId] [int] NOT NULL,
[Description] [nvarchar](200) NULL,
[StartDate] [datetime] NULL,
[EndDate] [datetime] NULL,
[isActive] [bit] NOT NULL,
[RestaurantId] [int] NULL,
[Discount%] [real] NULL,
CONSTRAINT [PK_Coupon] PRIMARY KEY CLUSTERED
(
[CouponId] 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
/****** Object: Table [dbo].[Item] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Item](
[ItemId] [int] NOT NULL,
[ItemName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](255) NULL,
[Price] [money] NOT NULL,
[IsActive] [bit] NOT NULL,
[IsSpecial] [bit] NOT NULL,
CONSTRAINT [PK_Item] PRIMARY KEY CLUSTERED
(
[ItemId] 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
/****** Object: Table [dbo].[Menu] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Menu](
[MenuId] [int] NOT NULL,
[Description] [nvarchar](255) NULL,
CONSTRAINT [PK_Menu] PRIMARY KEY CLUSTERED
(
[MenuId] 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
/****** Object: Table [dbo].[MenuCategory] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MenuCategory](
[CategoryId] [int] NOT NULL,
[CategoryName] [nvarchar](50) NOT NULL,
[Description] [nvarchar](255) NULL,
[IsActive] [bit] NOT NULL,
CONSTRAINT [PK_MenuCategory] PRIMARY KEY CLUSTERED
(
[CategoryId] 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
/****** Object: Table [dbo].[MenuItem] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MenuItem](
[MenuItemId] [int] NOT NULL,
[MenuId] [int] NOT NULL,
[itemid] [int] NOT NULL,
[CategoryId] [int] NOT NULL,
CONSTRAINT [PK_MenuItem] PRIMARY KEY CLUSTERED
(
[MenuItemId] 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
/****** Object: Table [dbo].[Restaurant] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Restaurant](
[RestaurantId] [int] NOT NULL,
[Name] [nvarchar](100) NOT NULL,
[Addressline1] [nvarchar](50) NOT NULL,
[Addressline2] [nvarchar](50) NULL,
CONSTRAINT [PK_Restaurant] PRIMARY KEY CLUSTERED
(
[RestaurantId] 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
/****** Object: Table [dbo].[RestaurantMenu] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[RestaurantMenu](
[RestaurantMenuId] [int] NOT NULL,
[Menuid] [int] NOT NULL,
[Restaurantid] [int] NOT NULL,
CONSTRAINT [PK_RestaurantMenu] PRIMARY KEY CLUSTERED
(
[RestaurantMenuId] 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
/****** Object: Table [dbo].[Review] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Review](
[ReviewId] [int] NOT NULL,
[ReviewComment] [nvarchar](500) NULL,
[ReviewRating] [real] NOT NULL,
[UserId] [int] NOT NULL,
[MenuItemId] [int] NOT NULL,
[ReviewDate] [datetime] NOT NULL,
CONSTRAINT [PK_Review] PRIMARY KEY CLUSTERED
(
[ReviewId] 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
/****** Object: Table [dbo].[User] Script Date: 1/8/2014 12:44:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[UserId] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Phone] [nvarchar](10) NOT NULL,
[Email] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserId] 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].[Coupon] WITH CHECK ADD CONSTRAINT [FK_Coupon_Restaurant] FOREIGN KEY([RestaurantId])
REFERENCES [dbo].[Restaurant] ([RestaurantId])
GO
ALTER TABLE [dbo].[Coupon] CHECK CONSTRAINT [FK_Coupon_Restaurant]
GO
ALTER TABLE [dbo].[MenuItem] WITH CHECK ADD CONSTRAINT [FK_MenuItem_Item] FOREIGN KEY([itemid])
REFERENCES [dbo].[Item] ([ItemId])
GO
ALTER TABLE [dbo].[MenuItem] CHECK CONSTRAINT [FK_MenuItem_Item]
GO
ALTER TABLE [dbo].[MenuItem] WITH CHECK ADD CONSTRAINT [FK_MenuItem_Menu] FOREIGN KEY([MenuId])
REFERENCES [dbo].[Menu] ([MenuId])
GO
ALTER TABLE [dbo].[MenuItem] CHECK CONSTRAINT [FK_MenuItem_Menu]
GO
ALTER TABLE [dbo].[MenuItem] WITH CHECK ADD CONSTRAINT [FK_MenuItem_MenuCategory] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[MenuCategory] ([CategoryId])
GO
ALTER TABLE [dbo].[MenuItem] CHECK CONSTRAINT [FK_MenuItem_MenuCategory]
GO
ALTER TABLE [dbo].[RestaurantMenu] WITH CHECK ADD CONSTRAINT [FK_RestaurantMenu_Menu] FOREIGN KEY([Menuid])
REFERENCES [dbo].[Menu] ([MenuId])
GO
ALTER TABLE [dbo].[RestaurantMenu] CHECK CONSTRAINT [FK_RestaurantMenu_Menu]
GO
ALTER TABLE [dbo].[RestaurantMenu] WITH CHECK ADD CONSTRAINT [FK_RestaurantMenu_Restaurant] FOREIGN KEY([Restaurantid])
REFERENCES [dbo].[Restaurant] ([RestaurantId])
GO
ALTER TABLE [dbo].[RestaurantMenu] CHECK CONSTRAINT [FK_RestaurantMenu_Restaurant]
GO
ALTER TABLE [dbo].[Review] WITH CHECK ADD CONSTRAINT [FK_Review_MenuItem] FOREIGN KEY([MenuItemId])
REFERENCES [dbo].[MenuItem] ([MenuItemId])
GO
ALTER TABLE [dbo].[Review] CHECK CONSTRAINT [FK_Review_MenuItem]
GO
ALTER TABLE [dbo].[Review] WITH CHECK ADD CONSTRAINT [FK_Review_User] FOREIGN KEY([UserId])
REFERENCES [dbo].[User] ([UserId])
GO
ALTER TABLE [dbo].[Review] CHECK CONSTRAINT [FK_Review_User]
GO
Review Table Contains
1. reviewId (INT, Primary key, Auto Increment)
2. restaurantId (INT, ForeignKey of restaurant table)
3. userId (INT, ForeignKey of user_master table)
4. menuItemId (INT, ForeignKey of menuItem table)
5. couponId (INT, ForeignKey of packs / coupons table)
6. title (VARCHAR)
7. comment (VARCHAR or TEXT)
8. reviewePoints (TINYINT)
9. reviewedOn (DATETIME)
10. status (TINYINT)
as per my senior suggestion its need to be like this:
user_reviews
- id
- restaurant_menu_item_id
- restaurant_id
- customer_id
- review_title : title like wow / superb / or ...
- review_points : from 1 - 10
- description : limit it to ...
- review_video_id
- created_bye
- created_date
- update_date
- status
I think this works fine for now as well as future (i mean it allow me to change in future if required)
Related
I have 2 tables User and UserLogin. UserLogin have a foreign key relationship with User table. What I want to do here is whenever I insert data into the User table through my API their autogenerated(user_id) auto-inserted into UserLogin table.
User table:
user_id | user_name | user_email
UserLogin table:
user_id | user_password | user_number
So when I run my query to add name and email in User table then autoincremented user_id is automatically inserted in UserLogin table with the provided password and number. How can I achieve this and is that thread safe?
yes it is possible and usually can be optained by ##identity try something like
set nocount off;
insert into User Values("Name","Email")
declare #lastID = ##identity
insert into UserLogin values(#lastID,"Password","number")
This code helps you
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[User](
[user_id] [int] IDENTITY(1,1) NOT NULL,
[user_name] [varchar](100) NULL,
[user_email] [varchar](100) NULL,
[salt] [uniqueidentifier] NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[user_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
SET ANSI_PADDING OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserLogin](
[UserLoginId] [int] IDENTITY(1,1) NOT NULL,
[user_id] [int] NULL,
[user_password] [binary](1) NULL,
[user_number] [int] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[User] WITH CHECK ADD CONSTRAINT [FK_UserLogin_User] FOREIGN KEY([user_id])
REFERENCES [dbo].[User] ([user_id])
GO
ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_UserLogin_User]
GO
CREATE PROC [dbo].[Usp_UserLogin]
(
#user_name VARCHAR(100)
,#user_email VARCHAR(100)
,#user_password VARCHAR(200)
,#user_number INT
)
AS
Begin
SET NOCOUNT ON
DECLARE #Salt UNIQUEIDENTIFIER =NEWID()
,#IdentityNUmber INT
,#responseMessage nvarchar(1000)
BEGIN TRY
INSERT INTO Dbo.[User]([user_name],[user_email],[salt])
SELECT #user_name
,#user_email
,#salt
SET #IdentityNUmber=SCOPE_IDENTITY()
INSERT INTO Dbo.[UserLogin]([user_id],[user_password],user_number)
SELECT
#IdentityNUmber
,#user_number
,HASHBYTES('SHA2_512', #user_password + CAST(#salt AS NVARCHAR(36)))
END TRY
BEGIN CATCH
SET #responseMessage=ERROR_MESSAGE()
END CATCH
END
GO
Execute the Procedure
EXEC [Usp_UserLogin] #user_name='Test1',#user_email='Test1#gmail',#user_password='Test1#123',#user_number=2
I'm new to SQL Server triggers.
I have 2 tables (one master and one slave) with a primary on multiple columns.
I need to create a trigger on first table in order to synchronize the second table. In which way could I manage an update on multiple rows on a column on primary key?
My tables are:
CREATE TABLE [dbo].[Seasons]
(
[Entity] [nvarchar](4) NOT NULL,
[Code] [nvarchar](5) NOT NULL,
[Description] [nvarchar](50) NULL,
CONSTRAINT [PK_Seasons]
PRIMARY KEY CLUSTERED ([Entity] ASC, [Code] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 10) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Seasons2]
(
[Entity] [nvarchar](4) NOT NULL,
[Code] [nvarchar](5) NOT NULL,
[Description] [nvarchar](50) NULL,
CONSTRAINT [PK_Seasons2]
PRIMARY KEY CLUSTERED ([Entity] ASC, [Code] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 10) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO [Seasons] VALUES ('XX','11','Test')
INSERT INTO [Seasons] VALUES ('XX','22','Test')
INSERT INTO [Seasons] VALUES ('XX','33','Test')
UPDATE [Seasons]
SET Entity = 'YY'
In the worst case, could I iterate over inserted and deleted tables? These tables are in the same order?
Thank you very much!
I have simple code in Microsoft SQL Server Managment Studio query window.
insert into [clients] ("Addres","companyID") values (NULL,NULL);
SCOPE_IDENTITY();
select * from clients;
When I run it I have error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'SCOPE_IDENTITY'.
What is wrong? Without line SCOPE_IDENTITY(); everything goes fine.
Table clients:
CREATE TABLE [dbo].[clients](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Addres] [nvarchar](200) NULL,
[companyID] [int] NULL,
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]
) ON [PRIMARY]
Use with select.
insert into [clients] ("Addres","companyID") values (NULL,NULL);
select SCOPE_IDENTITY();
I am trying to build a database (access)for students marks. Every student has many courses and different marks for each course.
Example
John:
Math Q1 = 80, Q2= 90, Q3=77
Art
Q1=75, Q2=85, Q3=80
and so on
I build 3 tables with relations as the following
students(id-name-grade)
courses(courseid-coursename)
marks(id-courseid-Q1-Q2-Q3)
Is my work correct? because I will upgrade it to SQL, so I need to know if the tables and relations are identical for reports and forms?
Ok, but you should rename some fields: courses(id, name), marks(stu_id, course_id, Q1, Q2, Q3)
SQL (I use PostgreSQL, but I think other DBMS similar)
CREATE TABLE students
(
id integer NOT NULL,
fname varchar(20),
lname varchar(20),
grade integer,
PRIMARY KEY (id)
);
CREATE TABLE courses
(
id integer NOT NULL,
name varchar(20),
PRIMARY KEY (id)
);
CREATE TABLE marks
(
stu_id integer REFERENCES students(id),
course_id integer REFERENCES courses(id),
Q1 double,
Q2 double,
Q3 double,
PRIMARY KEY (stu_id, course_id)
);
or
CREATE TABLE marks
(
id integer NOT NULL,
stu_id integer REFERENCES students(id),
course_id integer REFERENCES courses(id),
Q1 double,
Q2 double,
Q3 double,
PRIMARY KEY (id)
);
Hope your help
You should use composite key in marks table. That will not limited you to only 3 questions.
Ms Sql Query for your table creation will look like-
/****** Object: Table [dbo].[students] Script Date: 11/11/2014 16:20:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[students](
[id] [int] NOT NULL,
[fname] [varchar](20) NULL,
[lname] [varchar](20) NULL,
[grade] [int] NULL,
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]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[courses] Script Date: 11/11/2014 16:19:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[courses](
[id] [int] NOT NULL,
[name] [varchar](20) NULL,
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]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[marks] Script Date: 11/11/2014 16:19:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[marks](
[stu_id] [int] NOT NULL,
[course_id] [int] NOT NULL,
[q_id] [int] NOT NULL,
[mark] [numeric](18, 0) NOT NULL,
CONSTRAINT [PK_marks] PRIMARY KEY CLUSTERED
(
[stu_id] ASC,
[course_id] ASC,
[q_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].[marks] WITH CHECK ADD CONSTRAINT [FK_marks_courses] FOREIGN KEY([course_id])
REFERENCES [dbo].[courses] ([id])
GO
ALTER TABLE [dbo].[marks] CHECK CONSTRAINT [FK_marks_courses]
GO
ALTER TABLE [dbo].[marks] WITH CHECK ADD CONSTRAINT [FK_marks_students] FOREIGN KEY([stu_id])
REFERENCES [dbo].[students] ([id])
GO
ALTER TABLE [dbo].[marks] CHECK CONSTRAINT [FK_marks_students]
GO
I am unable to select more than 1 row from a sql table.
I can only do
Select top 1 * from [table_name]
or
Select [pk_table] from [table_name].
Any other command just keeps processing and the table only has about 300 records with 10 columns.
Table Schema:
CREATE TABLE [dbo].[tb1](
[myid] [int] IDENTITY(1,1) NOT NULL,
[cl1] [int] NULL,
[cl2] [int] NULL,
[cl3] [varchar](5) NOT NULL,
[cl4] [varchar](5) NULL,
[cl5] [tinyint] NULL,
CONSTRAINT [PK_tb1_Tbl] PRIMARY KEY CLUSTERED
(
[myid] ASC
)
WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
I am not sure what is causing the problem here since same table schema is working in other database.
You can use following query to find out running queries in your DB:
select
p.spid
, right(convert(varchar,
dateadd(ms, datediff(ms, P.last_batch, getdate()), '1900-01-01'),
121), 12) as 'batch_duration'
, P.program_name
, P.hostname
, P.loginame
from master.dbo.sysprocesses P
where P.spid > 50
and P.status not in ('background', 'sleeping')
and P.cmd not in ('AWAITING COMMAND'
,'MIRROR HANDLER'
,'LAZY WRITER'
,'CHECKPOINT SLEEP'
,'RA MANAGER')
order by batch_duration desc