我的存储过程执行了几次?

时间:2015-08-27 12:36:56

标签: sql-server sql-server-2008 sql-server-2014

我编写了一个过滤表格的存储过程。它工作正常,并选择正确的记录,但在结果中,例如,如果查询必须返回一条记录。

而不是一条记录返回太多记录,我的存储过程可能会执行多次?

我使用此代码:

ALTER PROCEDURE [dbo].[propertyFilterByCity] 
    @city NVARCHAR(150) = NULL,
    @DefaultPageIndex smallint = NULL,
    @DefaultPageSize smallint = NULL,
    @RecordCount bit = NULL,
    @MinPrice decimal(18, 0) = NULL,
    @MaxPrice decimal(18, 0) = NULL,
    @bedRoom smallint = NULL,
    @PropertyType NVARCHAR(20) = NULL,
    @requestType NVARCHAR(20) = NULL,
    @Parking bit = NULL,
    @RemoteParking bit = NULL,
    @Lobby bit = NULL,
    @AssemblyHall bit = NULL,
    @Gym bit = NULL,
    @Surveillance bit = NULL,
    @FireAlarm bit = NULL,
    @FireFighting bit = NULL,
    @Pool bit = NULL,
    @Sauna bit = NULL,
    @Jacuzzi bit = NULL,
    @Carwash bit = NULL,
    @Laundry bit = NULL,
    @Custodian bit = NULL,
    @Shooting bit = NULL,
    @TheftAlarm bit = NULL,
    @PanelFit bit = NULL,
    @Sentry bit = NULL,
    @CentralSatellite bit = NULL,
    @CentralAntenna bit = NULL,
    @Fireplaces bit = NULL,
    @MasterRoom bit = NULL,
    @Patio bit = NULL,
    @Barbecue bit = NULL,
    @UPS bit = NULL,
    @RoofGarden bit = NULL
AS
BEGIN
    BEGIN TRY
        SET NOCOUNT ON ;
        BEGIN TRANSACTION;
        DECLARE @CityId int;
        IF EXISTS(SELECT Estate_CityId FROM [^Estate_City] WHERE Estate_CityName = @city)
            BEGIN
                SET @CityId= (SELECT Estate_CityId FROM [^Estate_City] WHERE Estate_CityName = @city);
            END
        IF(@MinPrice IS NOT NULL AND @MaxPrice IS NOT NULL)
        BEGIN       
            IF (@MaxPrice>0 AND @MaxPrice>@MinPrice)
            BEGIN           
                DECLARE @results TABLE (RowNum INT,Estate_Code BIGINT,Estate_propertyType NVARCHAR(50),Estate_buildingArea NVARCHAR(20),Estate_floor NVARCHAR(10),Estate_neighborhoodProperty NVARCHAR(100),Estate_street1 NVARCHAR(100),
                Estate_street2 NVARCHAR(100),Estate_visits BIGINT,houseImage NVARCHAR(MAX),houseImage1 NVARCHAR(MAX),houseImage2 NVARCHAR(MAX),houseImage3 NVARCHAR(MAX),houseImage4 NVARCHAR(MAX),Owner_requestType NVARCHAR(MAX),
                Facility_Description NVARCHAR(MAX),Estate_BedRoomCount NVARCHAR(2))
                INSERT INTO @results(RowNum,Estate_Code,Estate_propertyType,Estate_buildingArea,Estate_floor,Estate_neighborhoodProperty,Estate_street1,Estate_street2,Estate_visits,houseImage,houseImage1,
                houseImage2,houseImage3,houseImage4,Owner_requestType,Facility_Description,Estate_BedRoomCount)
                SELECT ROW_NUMBER() OVER(ORDER BY [^Estate].Estate_Code) AS [Row],[^Estate].Estate_Code,[^Estate].Estate_propertyType, [^Estate].Estate_buildingArea,[^Estate].Estate_floor,[^Estate].Estate_neighborhoodProperty,[^Estate].Estate_street1,[^Estate].Estate_street2,[^Estate].Estate_visits,[^Estate_Images].houseImage,[^Estate_Images].houseImage1,[^Estate_Images].houseImage2,[^Estate_Images].houseImage3,[^Estate_Images].houseImage4,[^Estate_OwnerInfo].RequestType_Type,[^Estate_Facility].Facility_descText,[^Estate].Estate_bedRoomCount
                FROM [^Estate]
                INNER JOIN [^Estate_Images] ON [^Estate].Estate_Code=[^Estate_Images].Estate_Code
                INNER JOIN [^Estate_OwnerInfo] ON [^Estate].Owner_code=[^Estate_OwnerInfo].Owner_code
                INNER JOIN [^Estate_Facility] ON [^Estate].Estate_Code=[^Estate_Facility].Estate_Code
                INNER JOIN [^Estate_City] ON [^Estate].Estate_CityId =[^Estate].Estate_CityId
                WHERE 
                    (@CityId IS NULL OR [^Estate].Estate_CityId =@CityId ) AND
                    (@requestType IS NULL OR [^Estate_OwnerInfo].RequestType_Type = @requestType) AND
                    ([^Estate].Estate_totalPrice BETWEEN @MinPrice AND @MaxPrice) AND
                    (@bedRoom IS NULL OR [^Estate].Estate_bedRoomCount>@bedRoom) AND
                    (@PropertyType IS NULL OR [^Estate].Estate_propertyType = @PropertyType) AND
                    (@requestType IS NULL OR [^Estate_OwnerInfo].RequestType_Type = @requestType) AND
                    (@bedRoom IS NULL OR [^Estate].Estate_bedRoomCount = @bedRoom) AND
                    (@Parking IS NULL OR [^Estate_Facility].Facility_Parking = @Parking) AND
                    (@RemoteParking IS NULL OR [^Estate_Facility].Facility_RemoteParking = @RemoteParking) AND
                    (@Lobby IS NULL OR [^Estate_Facility].Facility_Lobby = @Lobby)AND
                    (@Gym IS NULL OR [^Estate_Facility].Facility_Gym = @Gym) AND
                    (@Surveillance IS NULL OR [^Estate_Facility].Facility_Surveillance = @Surveillance) AND
                    (@FireAlarm IS NULL OR [^Estate_Facility].Facility_FireAlarm = @FireAlarm) AND
                    (@FireFighting IS NULL OR [^Estate_Facility].Facility_FireFighting = @FireFighting) AND
                    (@Pool IS NULL OR [^Estate_Facility].Facility_Pool = @Pool) AND
                    (@Sauna IS NULL OR [^Estate_Facility].Facility_Sauna = @Sauna) AND
                    (@Jacuzzi IS NULL OR [^Estate_Facility].Facility_Jacuzzi = @Jacuzzi) AND
                    (@Carwash IS NULL OR [^Estate_Facility].Facility_Carwash = @Carwash) AND
                    (@Laundry IS NULL OR [^Estate_Facility].Facility_Laundry = @Laundry) AND
                    (@Custodian IS NULL OR [^Estate_Facility].Facility_Custodian = @Custodian) AND
                    (@Shooting IS NULL OR [^Estate_Facility].Facility_Shooting = @Shooting) AND
                    (@TheftAlarm IS NULL OR [^Estate_Facility].Facility_TheftAlarm = @TheftAlarm) AND
                    (@PanelFit IS NULL OR [^Estate_Facility].Facility_PanelFit = @PanelFit) AND
                    (@Sentry IS NULL OR [^Estate_Facility].Facility_Sentry = @Sentry) AND
                    (@CentralSatellite IS NULL OR [^Estate_Facility].Facility_CentralSatellite = @CentralSatellite) AND
                    (@CentralAntenna IS NULL OR [^Estate_Facility].Facility_CentralAntenna = @CentralAntenna) AND
                    (@Fireplaces IS NULL OR [^Estate_Facility].Facility_Fireplaces = @Fireplaces) AND
                    (@MasterRoom IS NULL OR [^Estate_Facility].Facility_MasterRoom = @MasterRoom) AND
                    (@Patio IS NULL OR [^Estate_Facility].Facility_Patio = @Patio) AND
                    (@Barbecue IS NULL OR [^Estate_Facility].Facility_Barbecue = @Barbecue) AND
                    (@UPS IS NULL OR [^Estate_Facility].Facility_UPS= @UPS) AND
                    (@RoofGarden IS NULL OR [^Estate_Facility].Facility_RoofGarden = @RoofGarden)
            END
        END
        select * from @results order by Estate_Code;
        DECLARE @ErrorCode INT;
            DECLARE @Result INT;
            SET @ErrorCode = @@ERROR;
            IF (@ErrorCode <> 0)
            BEGIN
                ROLLBACK TRANSACTION;
                SET @Result=0;
                EXEC Debug_InsertSQLErrorDetails
            END
            ELSE
            BEGIN
                COMMIT TRANSACTION;
                SET @Result=SCOPE_IDENTITY();
            END
    END TRY
    BEGIN CATCH
        SET NOCOUNT OFF
        IF @@TRANCOUNT > 0 
        BEGIN
            ROLLBACK TRANSACTION;   
        END
        EXEC Debug_InsertSQLErrorDetails
        SET @Result=0;
    END CATCH
END

请帮我解决这个问题谢谢!

1 个答案:

答案 0 :(得分:0)

首先,存储过程不会多次运行。

原因可能是您从Estate表中加入多个表。如果这些连接不是1到1,而是1到n,那么每个可能的连接都会得到一个结果行。

例如,如果一个地产有多个图像,你的结果中的每个地产图像都会有一行。