3

I have a table with the following rows.

tblPictures

  • picID
  • picName
  • galleryID
  • isActive

I want to run a query to select for example all the pictures with galleryID=3 and isActive=1

If there are no pictures in this select with the isActive row being 1 (in other words, query returns no results) I want to make the first picture's isActive=1 with the galleryID=3

Can anyone guide me on this?

9
  • 2
    You need to tell us how to define "first." SQL Server considers a table an unordered set of rows; you need to give it criteria (e.g. earliest value in a given datetime column) so that it can reliably and repeatedly use the same method to update the row you call "first." Commented Sep 13, 2012 at 22:19
  • what do you mean by the first row? sorted by primary index or what? Commented Sep 13, 2012 at 22:20
  • Ok I apologise for not being clear.. I am updating my question now.. Commented Sep 13, 2012 at 22:21
  • 1
    Your edit still gives no indication of what you consider the "first row". As @AaronBertrand explained, there is no "first row" unless you have a criteria that makes it so (like a date or time stamp, or an id that changes for each row, etc.). IOW, if you have five pictures where galleryID = 3, which one of those five rows is "first"?
    – Ken White
    Commented Sep 13, 2012 at 22:28
  • 1
    And what do you want if there are rows with isActive = 1? No word problems, please; post sample data and desired results in each case. Commented Sep 13, 2012 at 22:34

2 Answers 2

6

Here you go. Basically, it checks to see if there are any Active records in that set, and if not, updates the first one (determined by the picID). You can stick it in a stored procedure or function if you like, or just run the script and change the gallery ID manually.

DECLARE @Count INT,
@GalleryID INT

SET @GalleryID = 3

SELECT @Count = COUNT(*)
FROM tblPictures
WHERE galleryID=@GalleryID and isActive=1

IF @Count = 0
BEGIN

UPDATE tblPictures
SET isActive = 1
WHERE picID = (
    SELECT TOP 1 picID 
    FROM tblPictures 
    WHERE galleryID = @GalleryID 
    ORDER BY picID
    )

END

MOAR CODE

This should update the first picture (by picID) in any gallery without an active picture:

UPDATE tblPictures
SET isActive = 1
WHERE picID IN (
    SELECT picID 
    FROM (
        SELECT galleryID, MIN(picID) AS 'picID'
        FROM tblPictures AS p
        JOIN (
            SELECT galleryID 
            FROM tblPictures 
            WHERE isActive = 1
            ) AS s ON p.galleryID <> s.galleryID
        GROUP BY galleryID
        )
    )

The innermost subquery finds the galleries that DO have active pictures. The next step up finds the galleryIDs excluding the ones that have pictures. Then we find the lowest picID for each galleryID and use those to update the table.

2
  • Emin, I added another query that should do the whole table in one shot if you want to take a look.
    – Jim
    Commented Sep 13, 2012 at 23:16
  • It's like you are being poetic tonight :)) thank you very much! Commented Sep 13, 2012 at 23:39
-1

Try this:

declare @tav table

(

gallaryid int,

isactive int

)

insert into @tav 

select

3,0 union all

select

4,0 union all

select

5,1

select * from @tav

update @tav set isactive=1

where gallaryid=3 and isactive=(select top(1)isactive from @tav  order by isactive)

select * from @tav

where gallaryid=3 and isactive=1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.