IF EXISTS (
  SELECT *
  FROM  dbo.sysobjects 
  WHERE ID = OBJECT_ID(N'[dbo].[GetRandomPhotoLite]') AND
        OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetRandomPhotoLite]

GO

CREATE PROCEDURE [dbo].[GetRandomPhotoLite]
	@AlbumID int,
	@IsPublic bit
AS
	SELECT TOP 1
    [Photos].[PhotoID],
    [Photos].[AlbumID],
    [Photos].[Caption]
	FROM [Photos] LEFT JOIN [Albums]
		ON [Albums].[AlbumID] = [Photos].[AlbumID] 
	WHERE [Photos].[AlbumID] = @AlbumID
		AND ([Albums].[IsPublic] = @IsPublic OR [Albums].[IsPublic] = 1)
  ORDER BY NEWID()

GO

------
IF EXISTS (
  SELECT *
  FROM  dbo.sysobjects 
  WHERE ID = OBJECT_ID(N'[dbo].[GetPhotosLite]') AND
        OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetPhotosLite]

GO

CREATE PROCEDURE [dbo].[GetPhotosLite]
	@AlbumID int,
	@IsPublic bit
AS
	SELECT 
    [Photos].[PhotoID],
    [Photos].[AlbumID],
    [Photos].[Caption]
	FROM [Photos] LEFT JOIN [Albums]
		ON [Albums].[AlbumID] = [Photos].[AlbumID] 
	WHERE [Photos].[AlbumID] = @AlbumID
		AND ([Albums].[IsPublic] = @IsPublic OR [Albums].[IsPublic] = 1)
RETURN

GO

------
IF EXISTS (
  SELECT *
  FROM  dbo.sysobjects 
  WHERE ID = OBJECT_ID(N'[dbo].[GetRandomNonEmptyAlbum]') AND
        OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[GetRandomNonEmptyAlbum]

GO

CREATE PROCEDURE [dbo].[GetRandomNonEmptyAlbum]
AS
	SELECT TOP 1
		[Albums].[AlbumID]
	FROM [Albums] LEFT JOIN [Photos] 
		ON [Albums].[AlbumID] = [Photos].[AlbumID] 
	WHERE
		[Albums].[IsPublic] = 1
	GROUP BY 
		[Albums].[AlbumID], 
		[Albums].[Caption], 
		[Albums].[IsPublic]
	HAVING
		Count([Photos].[PhotoID]) > 0
  ORDER BY NEWID()
RETURN