SELECT DoctorID, LastName, FirstName FROM ( UPDATE Doctors SET NumImpressionsInSearches = NumImpressionsInSearches + 1 OUTPUT INSERTED.* FROM Doctors WHERE [some criteria]) a
SELECT DoctorID, LastName, FirstName FROM ( UPDATE Doctors SET NumImpressionsInSearches = NumImpressionsInSearches + 1 OUTPUT INSERTED.* FROM Doctors WHERE DoctorID IN ( SELECT DoctorID FROM ( SELECT TotalRows = COUNT(*) OVER(), RowNum = CASE WHEN @sortColumn = 'OccupationName_ASC' THEN ROW_NUMBER()OVER (ORDER BY O.OccupationName) WHEN @sortColumn = 'City_ASC' THEN ROW_NUMBER()OVER (ORDER BY D.City) WHEN @sortColumn = 'OccupationName_DESC' THEN ROW_NUMBER()OVER (ORDER BY O.OccupationName DESC) WHEN @sortColumn = 'City_DESC' THEN ROW_NUMBER()OVER (ORDER BY D.City DESC) ELSE ROW_NUMBER()OVER (ORDER BY NewID()) -- note this returns random order, and each page's results may not be unique. END, D.DoctorID, D.OccupationID, D.LastName, D.FirstName, D.Designations, D.NumRecommendations, D.City, D.State, D.Zip, D.Phone, D.NumProfileViews, D.ApprovedStatus, O.OccupationName, D.MembershipStatusID FROM dbo.Doctors P WITH (NOLOCK) INNER JOIN dbo.Occupations O ON O.OccupationID = D.OccupationID INNER JOIN dbo.DoctorsPracticeAreas DPA ON DPA.DoctorID = D.DoctorID WHERE DPA.PracticeAreaID = @practiceAreaID AND (@occupationID IS NULL OR D.OccupationID = @occupationID) AND (@name IS NULL OR D.LastName LIKE @name + '%' OR D.PracticeName LIKE '%' + @name + '%') ) AS XYZ -- you need this AS XYZ WHERE RowNum BETWEEN @startRow AND (@startRow + @rowsPerPage) - 1 )) a
CREATE TABLE #Doctors (ID INT IDENTITY, Name VARCHAR(100), Practice VARCHAR(100), [Views] INT)CREATE TABLE #DoctorInfo (Name VARCHAR(100), OfficeDays VARCHAR(10))INSERT INTO #Doctors SELECT 'Dr. Dwain', 'Gynecology', 0 UNION ALL SELECT 'Dr. Jeff', 'General Practitioner', 0INSERT INTO #DoctorInfoSELECT 'Dr. Dwain', 'Monday' UNION ALL SELECT 'Dr. Jeff', 'Tuesday'UNION ALL SELECT 'Dr. Dwain', 'Tuesday'UNION ALL SELECT 'Dr. Jeff', 'Wednesday'GOUPDATE dSET [Views] = [Views] + 1OUTPUT INSERTED.*, a.OfficeDaysFROM #Doctors dINNER JOIN #DoctorInfo a ON a.Name = d.NameWHERE ID = 1+ ABS(CHECKSUM(NEWID())) % 2GO 5SELECT * FROM #DoctorsDROP TABLE #Doctors, #DoctorInfo
UPDATE doctors_updateSET NumImpressionsInSearches = NumImpressionsInSearches + 1OUTPUT derived.*FROM dbo.Doctors doctors_updateINNER JOIN ( SELECT TOP 100 PERCENT * FROM ( SELECT TotalRows = COUNT(*) OVER(), RowNum = CASE WHEN @sortColumn = 'OccupationName_ASC' THEN ROW_NUMBER()OVER (ORDER BY O.OccupationName) WHEN @sortColumn = 'City_ASC' THEN ROW_NUMBER()OVER (ORDER BY D.City) WHEN @sortColumn = 'OccupationName_DESC' THEN ROW_NUMBER()OVER (ORDER BY O.OccupationName DESC) WHEN @sortColumn = 'City_DESC' THEN ROW_NUMBER()OVER (ORDER BY D.City DESC) ELSE ROW_NUMBER()OVER (ORDER BY NewID()) -- note this returns random order, and each page's results may not be unique. END, D.DoctorID, D.OccupationID, D.LastName, D.FirstName, D.Designations, D.NumRecommendations, D.City, D.State, D.Zip, D.Phone, D.NumProfileViews, D.ApprovedStatus, O.OccupationName, D.MembershipStatusID FROM dbo.Doctors P WITH (NOLOCK) INNER JOIN dbo.Occupations O ON O.OccupationID = D.OccupationID INNER JOIN dbo.DoctorsPracticeAreas DPA ON DPA.DoctorID = D.DoctorID WHERE DPA.PracticeAreaID = @practiceAreaID AND (@occupationID IS NULL OR D.OccupationID = @occupationID) AND (@name IS NULL OR D.LastName LIKE @name + '%' OR D.PracticeName LIKE '%' + @name + '%') ) AS XYZ -- you need this AS XYZ WHERE RowNum BETWEEN @startRow AND (@startRow + @rowsPerPage) - 1 ORDER BY RowNum) AS derived ON derived.DoctorID = doctors_update.DoctorID