我有以下 SQL 脚本。如您所见,我手动将@listingid
值设置为 30653。
但是应该对[listings]
表中@listingid
分配了[listings].id
列值的所有记录执行此脚本。
DECLARE @profname nvarchar(150)
DECLARE @furl nvarchar(250)
DECLARE @city nvarchar(250)
DECLARE @listingid int
set @listingid=30653
--select the top 1 professionname
SELECT TOP 1 @profname=REPLACE(LOWER(pn.title),' ','-'),@furl=l.friendlyurl,@city=REPLACE(REPLACE(LOWER(l.city),'''',''),' ','-') FROM healthprof_professionnames hpn
INNER JOIN professionname pn ON pn.id=hpn.professionnameid
INNER JOIN listings l on l.id=hpn.healthprofid
WHERE l.id=@listingid ORDER BY pn.title
--check if current friendlyurl already contains profession
IF NOT CHARINDEX(@profname,@furl)>0
SET @furl = @furl + '-' + @profname
IF NOT CHARINDEX(@city,@furl)>0
SET @furl = @furl + '-' + @city
SET @furl = @furl + '-3'
UPDATE listings set friendlyurl=@furl WHERE id=@listingid