September 12, 2007 at 12:14 am
Now how do I get this query to loop through all the records in the Commercial table? The following only does the first property
declare @pic1 as varchar(255)
declare @pic2 as varchar(255)
declare @pic3 as varchar(255)
declare @pic4 as varchar(255)
declare @newID as integer
declare @PropertyNumber as varchar(255)
declare @PropertyAddress as varchar(255)
select [ID], @PropertyNumber=PrprtyNo, @PropertyAddress =PrprtyAddr , @pic1=isNull(ImageLink1, 'none.gif'), @Pic2=isNull(ImageLink2, 'none.gif'), @Pic3=isNull(ImageLink3, 'none.gif'), @pic4=isNull(ImageLink4, 'none.gif') from Commercial
insert into Property (PropertyNumber, PropertyAddress) values (@PropertyNumber, @PropertyAddress)
SET @NewID = SCOPE_IDENTITY()
if @Pic1 <> 'none.gif'
insert into PropertyImages (PropertyID, dbPhoto) values (@NewID, @Pic1)
if @Pic2 <> 'none.gif'
insert into PropertyImages (PropertyID, dbPhoto) values (@NewID, @Pic2)
if @Pic3 <> 'none.gif'
insert into PropertyImages (PropertyID, dbPhoto) values (@NewID, @Pic3)
if @Pic4 <> 'none.gif'
insert into PropertyImages (PropertyID, dbPhoto) values (@NewID, @Pic4)
thanks
September 12, 2007 at 1:11 am
-- Initialize staging
BEGIN TRAN Yak
DECLARE @LastID INT
SELECT @LastID = MAX({Property table identity column name here})
FROM Property
-- Insert "master" records
INSERT Property
(
PropertyNumber,
PropertyAddress
)
SELECT PrprtyNo,
PrprtyAddr
FROM Commercial
-- Insert "child" records
INSERT PropertyImages
(
PropertyID,
dbPhoto
)
SELECT id.CurrID,
photo.Link
FROM (
SELECT {Property table identity column name here} AS CurrID
PropertyNumber,
PropertyAddress
FROM Commercial
WHERE {Property table identity column name here} > @LastID
) AS id
INNER JOIN (
SELECT PrprtyNo,
PrprtyAddr,
ImageLink1 AS Link
FROM Commercial
UNION ALL
SELECT PrprtyNo,
PrprtyAddr,
ImageLink2
FROM Commercial
UNION ALL
SELECT PrprtyNo,
PrprtyAddr,
ImageLink3
FROM Commercial
UNION ALL
SELECT PrprtyNo,
PrprtyAddr,
ImageLink4
FROM Commercial
) AS photo ON photo.PrprtyNo = id.PropertyNumber AND photo.PrprtyAddr = id.PropertyAddress
WHERE photo.Link IS NOT NULL
-- Do some cleanup
IF @@ERROR = 0
COMMIT TRAN Yak
ELSE
ROLLBACK TRAN Yak
N 56°04'39.16"
E 12°55'05.25"
September 12, 2007 at 1:14 am
September 12, 2007 at 1:23 am
September 12, 2007 at 1:33 am
September 12, 2007 at 4:29 pm
I am having a problem with the insertion of the images.
The PropertyAddress field is a text field while the PrprtyAddr field is an varchar(255) when I do a cast it is seems to matching in the "ON photo.PrprtyNo = id.PropertyNumber AND photo.PrprtyAddr = id.PropertyAddress" as no photos are being added
Any ideas???
- Initialize staging
BEGIN TRAN Yak
DECLARE @LastID INT
SELECT @LastID = MAX([ID])
FROM Property
-- Insert "master" records
INSERT Property
(
PropertyNumber,
PropertyAddress
)
SELECT PrprtyNo,
PrprtyAddr
FROM Commercial
-- Insert "child" records
INSERT PropertyImages
(
PropertyID,
dbPhoto
)
SELECT id.CurrID,
photo.Link
FROM (
SELECT [ID] AS CurrID
PropertyNumber,
PropertyAddress
FROM Property
WHERE ID > @LastID
) AS id
INNER JOIN (
SELECT PrprtyNo,
PrprtyAddr,
ImageLink1 AS Link
FROM Commercial
UNION ALL
SELECT PrprtyNo,
PrprtyAddr,
ImageLink2
FROM Commercial
UNION ALL
SELECT PrprtyNo,
PrprtyAddr,
ImageLink3
FROM Commercial
UNION ALL
SELECT PrprtyNo,
PrprtyAddr,
ImageLink4
FROM Commercial
) AS photo ON photo.PrprtyNo = id.PropertyNumber AND photo.PrprtyAddr = id.PropertyAddress
WHERE photo.Link IS NOT NULL
-- Do some cleanup
IF @@ERROR = 0
COMMIT TRAN Yak
ELSE
ROLLBACK TRAN Yak
September 12, 2007 at 8:25 pm
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply