Question : Help with top 1 relational query

Hello, I have two tables: Galleries and photos. They have a primary key of GalleryId.

I want to get  a random smImage and its galleryid from the photos table for each galleryId in galleries with a page value such as 'Authors'

What is the best method to do this:

Here is my table structure:

CREATE TABLE [dbo].[Galleries](
      [GalleryId] [uniqueidentifier] NULL,
      [Page] [varchar](50) NULL,
      [Title] [varchar](30) NULL,
      [Visible] [bit] NULL,
      [SortOrder] [numeric](10, 0) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Photos](
      [ImageId] [uniqueidentifier] NULL,
      [GalleryId] [uniqueidentifier] NULL,
      [smImage] [varchar](50) NULL,
      [lgImage] [varchar](50) NULL
) ON [PRIMARY]

Answer : Help with top 1 relational query

I would start by not using Dynamic SQL where possible.  So in other words instead of passing in the table and the column have a Stored Procedure for each table.  Than your UPDATE statement can be reduced to:

UPDATE YourTableName
SET  YourDateTimeColumn = CONVERT(datetime, STUFF(STUFF(STUFF(STUFF(YourDateCharColumn, 5, 0, '-'), 8, 0, '-'), 14, 0, ':'), 17, 0, ':'), 120)
WHERE YourTableID  = @FirstID
Random Solutions  
 
programming4us programming4us