Varchar中简单选择语句问题中的存储过程问题

时间:2014-12-19 06:19:46

标签: mysql sql stored-procedures procedure

这是我的Fiddle

这是我的表:

CREATE TABLE IF NOT EXISTS `vehicle` (
  `AutoID` int(11) NOT NULL AUTO_INCREMENT,
  `VehicleCode` varchar(20) NOT NULL,
  `NumberSeats` int(3) NOT NULL,
  `VehicleNumber` varchar(10) NOT NULL,
  `MaximumAllowed` int(2) NOT NULL,
  `City` varchar(4) NOT NULL,
  `State` varchar(4) NOT NULL,
  `Phone` int(12) DEFAULT NULL,
  `Insurance` varchar(30) NOT NULL,
  `VehicleType` varchar(10) NOT NULL,
  `Address` varchar(255) NOT NULL,
  `Tax` varchar(50) NOT NULL,
  `InsurancePhoto` varchar(255) NOT NULL,
  `RCPhoto` varchar(255) NOT NULL,
  `CreatedAt` datetime NOT NULL,
  `CreatedBy` varchar(30) NOT NULL,
  `UpdatedAt` datetime NOT NULL,
  `UpdatedBy` varchar(30) NOT NULL,
  `ActiveStatus` tinyint(1) NOT NULL,
  `IsDeletable` tinyint(1) NOT NULL,
  PRIMARY KEY (`AutoID`),
  UNIQUE KEY `VehicleCode` (`VehicleCode`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

并插入了两条记录

INSERT INTO `vehicle` (`AutoID`, `VehicleCode`, `NumberSeats`, `VehicleNumber`, `MaximumAllowed`, `City`, `State`, `Phone`, `Insurance`, `VehicleType`, `Address`, `Tax`, `InsurancePhoto`, `RCPhoto`, `CreatedAt`, `CreatedBy`, `UpdatedAt`, `UpdatedBy`, `ActiveStatus`, `IsDeletable`) VALUES
(1, '100', 0, '', 0, '', '', 0, '', '', '', '', '', '', '0000-00-00 00:00:00', '', '0000-00-00 00:00:00', '', 0, 0),
(2, '101', 0, '', 0, '', '', NULL, '', '', '', '', '', '', '0000-00-00 00:00:00', '', '0000-00-00 00:00:00', '', 0, 0);

我可以使用常规选择语句选择记录

SELECT * FROM `vehicle` where VehicleCode = '100'

我创建了选择的程序

我的程序在这里

DELIMITER //
CREATE PROCEDURE GetVehicle(IN VCode VARCHAR(20))
 BEGIN
 SELECT * 
 FROM vehicle
 WHERE VehicleCode = VCode;
 END //
DELIMITER ;

当我尝试访问我的程序时,我没有得到任何结果。

CALL `GetVehicle`(100);

我没有得到任何结果。

我正在做的错误是什么,如何解决?

2 个答案:

答案 0 :(得分:0)

尝试更改

中的VehicleCode
CREATE PROCEDURE GetVehicle(IN Vcode VARCHAR(255))

到Vcode然后在where语句中使用它。

答案 1 :(得分:0)

我尝试了你的代码没问题,但是你应该在你的架构中插入记录并将你存储的参数名称改为与字段名称不同,你可以将其命名为“vcode”,例如

所以你的架构应该是

CREATE TABLE IF NOT EXISTS `vehicle` (
  `AutoID` int(11) NOT NULL AUTO_INCREMENT,
  `VehicleCode` varchar(20) NOT NULL,
  `NumberSeats` int(3) NOT NULL,
  `VehicleNumber` varchar(10) NOT NULL,
  `MaximumAllowed` int(2) NOT NULL,
  `City` varchar(4) NOT NULL,
  `State` varchar(4) NOT NULL,
  `Phone` int(12) DEFAULT NULL,
  `Insurance` varchar(30) NOT NULL,
  `VehicleType` varchar(10) NOT NULL,
  `Address` varchar(255) NOT NULL,
  `Tax` varchar(50) NOT NULL,
  `InsurancePhoto` varchar(255) NOT NULL,
  `RCPhoto` varchar(255) NOT NULL,
  `CreatedAt` datetime NOT NULL,
  `CreatedBy` varchar(30) NOT NULL,
  `UpdatedAt` datetime NOT NULL,
  `UpdatedBy` varchar(30) NOT NULL,
  `ActiveStatus` tinyint(1) NOT NULL,
  `IsDeletable` tinyint(1) NOT NULL,
  PRIMARY KEY (`AutoID`),
  UNIQUE KEY `VehicleCode` (`VehicleCode`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

insert into vehicle(VehicleCode,NumberSeats,VehicleNumber,MaximumAllowed,
                    City,State,Phone,Insurance,VehicleType,Address,Tax,
                   InsurancePhoto,RCPhoto,CreatedAt,CreatedBy,UpdatedAt,
                   UpdatedBy,ActiveStatus,IsDeletable) 
                   values('100',4,'DES547',7,'Los','CA',44554,'45879','sedan',
                          'ssssss','55', 'sdsdsds','asdasdas',Now(),'saasda',
                           Now(),'asdasda',1,1);

然后按以下方式创建存储过程

DELIMITER //
create PROCEDURE GetVehicle(IN vcode VARCHAR(255))
 BEGIN
 SELECT * 
 FROM vehicle
 WHERE VehicleCode = vcode;
 END //
DELIMITER ;

现在尝试调用存储过程,它可以正常工作

call GetVehicle(100);