【发布时间】:2015-01-13 18:44:47
【问题描述】:
我的数据库有问题,我尝试简化我的数据库.. 经过多次尝试该函数使用'X'表,然后在'X'表上使用该函数。你不能使用函数使用同一张表...
--建表后:
create table Items(
ID int,
Price money
);
--插入一些值
insert into Items Values(1,4);
insert into Items Values(2,5);
--创建上表使用的函数
CREATE FUNCTION GetMaxPrice
(
-- Add the parameters for the function here
)
RETURNS Money
AS
BEGIN
-- Declare the return variable here
DECLARE @result money = 5;
select @result = max(Price)
from Items;
-- Return the result of the function
RETURN @result;
END
--然后alter table 添加约束 --接受任何低于或等于表中价格的价格
alter table Items add check(Price <= dbo.GetMaxPrice())
--之后,我尝试插入项目
insert into Items Values(3,4);
insert into Items Values(4,15); -- <-- here, I have problem. it inserted in Database..
--why inserted when max value is 5 ??
我使用 sql server 2012 Express,Win7
【问题讨论】:
标签: sql sql-server database function