选择进入sql - 用户功能

时间:2012-05-21 10:27:34

标签: sql sql-function

我想从不同的表中获取相同的列,因此我使用User Function。 但我想在Return之前对结果表进行排序。 在这段代码中,我尝试使用临时表来执行此操作。 sql返回: '@Result'附近的语法错误。' 我怎么能这样做?

由于

这里是代码:

CREATE FUNCTION sp_GetAllNotConfirmedOrders
()
RETURNS @Result TABLE 
(
    OrderId int,
    CreationDate datetime,
    BusinessName nvarchar(150),
    BusinessPhone nvarchar(20),
    UserType int
)
AS
BEGIN
    DECLARE @TempData TABLE
    (
        OrderId int,
        CreationDate datetime,
        BusinessName nvarchar(150),
        BusinessPhone nvarchar(20),
        UserType int
    )

    INSERT INTO @TempData
        SELECT     Orders.OrderId, ZimmerOrders.CreationDate,Zimmers.OwnerName, Zimmers.PhoneNUmber1, 2
        FROM         Orders INNER JOIN
                  ZimmerOrders ON Orders.OrderId = ZimmerOrders.OrederId INNER JOIN
                  ZimmerRooms ON ZimmerOrders.RoomId = ZimmerRooms.RoomId INNER JOIN
                  Zimmers ON ZimmerRooms.ZimmerId = Zimmers.UserId
        WHERE     (Orders.IsConfirm = 0)

    INSERT INTO @TempData
        SELECT     Orders.OrderId, HotelOrders.CreationDate, Hotels.PhoneNumber1, Hotels.HotelName, 4
        FROM         HotelOrders INNER JOIN
                      HotelRooms ON HotelOrders.RoomId = HotelRooms.RoomId INNER JOIN
                      Hotels ON HotelRooms.HotelId = Hotels.UserId INNER JOIN
                      Orders ON HotelOrders.OrderId = Orders.OrderId
        WHERE     (Orders.IsConfirm = 0)

    SELECT * INTO @Result FROM @TempData ORDER BY CreationDate
    RETURN 
END

1 个答案:

答案 0 :(得分:1)

你可以这样做:

INSERT INTO @Result
SELECT OrderId, CreationDate, BusinessName, BusinessPhone, UserType
FROM @TempData
ORDER BY CreationDate