Click here to Skip to main content
15,913,773 members
Home / Discussions / Database
   

Database

 
GeneralShutdown and restart SQL Server programatically Pin
Xiangyang Liu 刘向阳2-Oct-04 16:39
Xiangyang Liu 刘向阳2-Oct-04 16:39 
GeneralRe: Shutdown and restart SQL Server programatically Pin
Kurt _B7-Oct-04 13:09
Kurt _B7-Oct-04 13:09 
GeneralRe: Shutdown and restart SQL Server programatically Pin
Xiangyang Liu 刘向阳14-Oct-04 8:05
Xiangyang Liu 刘向阳14-Oct-04 8:05 
GeneralRe: Shutdown and restart SQL Server programatically Pin
Xiangyang Liu 刘向阳14-Oct-04 15:04
Xiangyang Liu 刘向阳14-Oct-04 15:04 
GeneralNeed help do design the table for this requirement Pin
percyvimal2-Oct-04 6:44
percyvimal2-Oct-04 6:44 
GeneralRe: Need help do design the table for this requirement Pin
Colin Angus Mackay2-Oct-04 7:00
Colin Angus Mackay2-Oct-04 7:00 
GeneralRe: Need help do design the table for this requirement Pin
percyvimal2-Oct-04 7:14
percyvimal2-Oct-04 7:14 
GeneralRe: Need help do design the table for this requirement Pin
Colin Angus Mackay2-Oct-04 7:52
Colin Angus Mackay2-Oct-04 7:52 
The following function will return the ExpiryDate for any article for which the article code is known.
CREATE FUNCTION dbo.ExpiryDate(@ArticleCode int)
RETURNS datetime
AS
BEGIN
	DECLARE @ExpiryDate datetime

	SELECT @ExpiryDate = MAX(ExpiryDate)
	FROM Accounts
	WHERE ArticleCode = @ArticleCode

	RETURN @ExpiryDate
END


When you are creating your article table you can add a calculated column, so that its value is created anytime it is asked for.

The table stubs needed to support this, and the other information you've given me are:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_Accounts_Articles]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[Accounts] DROP CONSTRAINT FK_Accounts_Articles
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_Articles_Users]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE [dbo].[Articles] DROP CONSTRAINT FK_Articles_Users
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ExpiryDate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[ExpiryDate]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Accounts]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Accounts]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Articles]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Articles]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Users]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE FUNCTION dbo.ExpiryDate(@ArticleCode int)
RETURNS datetime
AS
BEGIN
	DECLARE @ExpiryDate datetime

	SELECT @ExpiryDate = MAX(ExpiryDate)
	FROM Accounts
	WHERE ArticleCode = @ArticleCode

	RETURN @ExpiryDate
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE TABLE [dbo].[Accounts] (
	[AccountCode] [int] NOT NULL ,
	[ArticleCode] [int] NOT NULL ,
	[Amount] [money] NOT NULL ,
	[PaymentDate] [datetime] NOT NULL ,
	[ExpiryDate] [datetime] NOT NULL 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Articles] (
	[ArticleCode] [int] NOT NULL ,
	[UserCode] [int] NOT NULL ,
	[IsPaid] AS (case when (getdate() < [dbo].[ExpiryDate]([ArticleCode])) then 1 else 0 end) 
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Users] (
	[UserCode] [int] NOT NULL 
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Accounts] ADD 
	CONSTRAINT [PK_Accounts] PRIMARY KEY  CLUSTERED 
	(
		[AccountCode]
	)  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[Articles] ADD 
	CONSTRAINT [PK_Articles] PRIMARY KEY  CLUSTERED 
	(
		[ArticleCode]
	)  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[Users] ADD 
	CONSTRAINT [PK_Users] PRIMARY KEY  CLUSTERED 
	(
		[UserCode]
	)  ON [PRIMARY] 
GO

ALTER TABLE [dbo].[Accounts] ADD 
	CONSTRAINT [FK_Accounts_Articles] FOREIGN KEY 
	(
		[ArticleCode]
	) REFERENCES [dbo].[Articles] (
		[ArticleCode]
	)
GO

ALTER TABLE [dbo].[Articles] ADD 
	CONSTRAINT [FK_Articles_Users] FOREIGN KEY 
	(
		[UserCode]
	) REFERENCES [dbo].[Users] (
		[UserCode]
	)
GO


You obviously need to fill in the other details that you need for other parts of your application, but the above shows the relationships between Users/Articles/Accounts and how to calculate a payment flag.

Does this help?


Do you want to know more?


Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
GeneralRe: Need help do design the table for this requirement Pin
percyvimal2-Oct-04 8:25
percyvimal2-Oct-04 8:25 
GeneralRe: Need help do design the table for this requirement Pin
Colin Angus Mackay2-Oct-04 8:59
Colin Angus Mackay2-Oct-04 8:59 
QuestionMark DataTable column so changes do not affect RowState? Pin
DrGerry1-Oct-04 12:09
DrGerry1-Oct-04 12:09 
GeneralCheck for syntax Pin
macupryk30-Sep-04 21:16
macupryk30-Sep-04 21:16 
GeneralRe: Check for syntax Pin
Colin Angus Mackay1-Oct-04 1:24
Colin Angus Mackay1-Oct-04 1:24 
GeneralI need to Create a User Login and Registration Webform in Oracle and ODBC Pin
macupryk30-Sep-04 21:15
macupryk30-Sep-04 21:15 
QuestionCan we bind all these three to one connection Pin
macupryk30-Sep-04 21:14
macupryk30-Sep-04 21:14 
GeneralPassing a var to a SP.. Pin
Jacob Hammack30-Sep-04 15:32
Jacob Hammack30-Sep-04 15:32 
GeneralRe: Passing a var to a SP.. Pin
Christian Graus30-Sep-04 16:30
protectorChristian Graus30-Sep-04 16:30 
GeneralRe: Passing a var to a SP.. Pin
Jacob Hammack30-Sep-04 20:22
Jacob Hammack30-Sep-04 20:22 
GeneralRe: Passing a var to a SP.. Pin
Colin Angus Mackay1-Oct-04 0:26
Colin Angus Mackay1-Oct-04 0:26 
GeneralBeginner OLE question Pin
errenden30-Sep-04 10:01
errenden30-Sep-04 10:01 
GeneralRe: Beginner OLE question Pin
ThomasH13-Oct-04 12:58
ThomasH13-Oct-04 12:58 
GeneralRe: Beginner OLE question Pin
errenden4-Oct-04 5:23
errenden4-Oct-04 5:23 
GeneralRe: Beginner OLE question Pin
ThomasH14-Oct-04 13:04
ThomasH14-Oct-04 13:04 
GeneralRe: Beginner OLE question Pin
errenden5-Oct-04 3:59
errenden5-Oct-04 3:59 
GeneralTransaction SQL help (SQL Server 2000) Pin
Matt Newman30-Sep-04 6:31
Matt Newman30-Sep-04 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.