无法使用参数创建存储过程

时间:2013-05-30 01:35:53

标签: mysql date stored-procedures join

我正在尝试使用in参数第一个日期,最后日期和国家

创建存储过程

这是正常的查询巫婆工作正常。


SELECT CAR_TYPE.NAME, CAR_TYPE.TYPE, CAR.DAY_PRICE, CAR.REGISTRATION_NUMBER, AGENCY.NAME, BRANCH.NAME
FROM CAR
JOIN CAR_TYPE ON CAR_TYPE.ID = CAR.CAR_TYPE_ID
JOIN BRANCH ON BRANCH.ID = CAR.BRANCH_ID    
JOIN BRANCH_ADDRESS ON BRANCH_ADDRESS.BRANCH_ID = BRANCH.ID
JOIN AGENCY ON AGENCY.ID = BRANCH.ID`  
WHERE CAR.ID NOT IN
(SELECT BOOKING.CAR_ID FROM BOOKING WHERE BOOKING.PICKUP_DATE < '2013-01-01' AND BOOKING.RETURN_DATE > '2013-12-12')
AND BRANCH_ADDRESS.CITY_NAME = 'Stockholm'
end

1 个答案:

答案 0 :(得分:-1)

我无法弄清楚你在问题中做了什么,因为格式不正确。

我重新格式化了它,看起来像是一个正确的查询,并进行了一些更改以提高可读性。

SELECT t.NAME, t.TYPE, c.DAY_PRICE, 
      c.REGISTRATION_NUMBER, a.NAME, b.NAME`

FROM CAR c
    JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
    JOIN BRANCH b ON b.ID = c.BRANCH_ID
    JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
    JOIN AGENCY a ON a.ID = b.ID

WHERE ba.CITY_NAME = 'Stockholm'
  And c.ID NOT IN
      (SELECT CAR_ID 
       FROM BOOKING 
       WHERE PICKUP_DATE < '2013-01-01' 
       AND RETURN_DATE > '2013-12-12')

如果这是SQL服务器,那么用于终止基本(没有错误处理)存储过程的语句将是

Create Procedure dbo.FetchCarData -- or whatever you want to name it
@firstDate DateTime,
@lastDate DateTime,
@country varChar(50)    
As
  SELECT t.NAME, t.TYPE, c.DAY_PRICE, 
      c.REGISTRATION_NUMBER, a.NAME, b.NAME`

  FROM CAR c
    JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
    JOIN BRANCH b ON b.ID = c.BRANCH_ID
    JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
    JOIN AGENCY a ON a.ID = b.ID`

  WHERE ba.CITY_NAME = @country 
    And c.ID NOT IN
      (SELECT CAR_ID 
       FROM BOOKING 
       WHERE PICKUP_DATE < @firstDate 
       AND RETURN_DATE > @lastDate )
MySQL中的

我觉得它看起来像这样......

Create Procedure FetchCarData( -- or whatever you want to name it
  @firstDate DateTime,
  @lastDate DateTime,
  @country varChar(50))   
Begin
   SELECT t.NAME, t.TYPE, c.DAY_PRICE, 
      c.REGISTRATION_NUMBER, a.NAME, b.NAME`

   FROM CAR c
    JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
    JOIN BRANCH b ON b.ID = c.BRANCH_ID
    JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
    JOIN AGENCY a ON a.ID = b.ID`

   WHERE ba.CITY_NAME = @country 
     And c.ID NOT IN
       (SELECT CAR_ID 
        FROM BOOKING 
        WHERE PICKUP_DATE < @firstDate 
        AND RETURN_DATE > @lastDate )   
End