我正在努力做出这样的声明:如果汽车年份比2012年更新,它将打印出来。然后,如果它比2012年更早,它将打印出来。唯一的问题是它给了我无效的对象名称.Attached是我的汽车表和我试过的代码。
CREATE TABLE CAR
(
CarID CHAR(8) PRIMARY KEY, -- Primary key
StoreID CHAR(4),
CarMake VARCHAR(30),
CarModel VARCHAR(30),
CarYear CHAR(4),
NeworUsed VARCHAR(6),
CarPrice DECIMAL (6,2),
);
ALTER PROC CarYear
@CarYear CHAR(4)
AS
BEGIN
IF(
SELECT CarYear
FROM CAR
WHERE CarYear = @CarYear
) >= 2012
BEGIN
PRINT 'This car is newer than 2012!'
END
ELSE
PRINT 'Car is older than 2012'
END
答案 0 :(得分:0)
CREATE TABLE CAR
(
CarID CHAR (8) PRIMARY KEY, -- Primary key
StoreID CHAR(4),
CarMake VARCHAR(30),
CarModel VARCHAR(30),
CarYear CHAR(4), -- better if you change to int type
NeworUsed VARCHAR(6),
CarPrice DECIMAL (6,2),
);
go
--change alter to create
create proc CarYear
@CarYear char (4)
AS
BEGIN
-- add TOP 1, otherwise if you will have more than 1 record you'll get error
if( select TOP 1 CONVERT(INT,CarYear)
from CAR
where CONVERT(int,CarYear)= CONVERT(int,@CarYear)
-- CarYear is char type and you compare to int type
) >= 2012
begin
print 'This car is newer than 2012!'
end
else
print 'Car is older than 2012'
ENDCO