Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:05:49+00:00 2026-06-14T17:05:49+00:00

I have the following SQL script. As you can see I manually set the

  • 0

I have the following SQL script. As you can see I manually set the @listingid value to 30653.

But this script should be executed for all records in the [listings] table where @listingid is assigned the value of the [listings].id column.

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:05:49+00:00Added an answer on June 14, 2026 at 5:05 pm

    You can use a cursor to loop over every row in a result set:

    declare cur cursor for
    select distinct id from listings
    
    declare @listingid int
    open cur
    fetch next from cur into @listingid 
    
    while @@FETCH_STATUS = 0
    BEGIN
        -- your code from above goes here
    
        fetch next from cur into @listingid 
    END
    

    That being said, I agree with Tim’s comment above. Rewrite it to work in one set-based operation if at all possible. I think this will work, but I haven’t tested it:

    ;WITH vars AS (
        SELECT id, profname, furl, city
        FROM (
            SELECT l.id,
                REPLACE(LOWER(pn.title),' ','-') as profname,
                l.friendlyurl as furl,
                REPLACE(REPLACE(LOWER(l.city),'''',''),' ','-') as city,
                ROW_NUMBER() OVER (PARTITION BY l.id ORDER BY pn.title) as rnk
            FROM healthprof_professionnames hpn
            INNER JOIN professionname pn ON pn.id=hpn.professionnameid
            INNER JOIN listings l on l.id=hpn.healthprofid
        ) A
        WHERE A.rnk = 1
    ),
    vars2 AS (
        SELECT id,
            CASE WHEN NOT CHARINDEX(profname, furl) > 0
                THEN furl + '-' + profname ELSE furl END as furl,
            city
        FROM vars
    ),
    vars3 as (
        SELECT id,
            CASE WHEN NOT CHARINDEX(city, furl) > 0
                THEN furl + '-' + city ELSE furl END as furl
        FROM vars2
    )
    UPDATE listings SET friendlyurl = vars3.furl + '-3'
    FROM listings INNER JOIN vars3 on vars3.id = listings.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL script: DECLARE @temp table ( ID int IDENTITY(1, 1),
I am using SQL Server 2008 and I have the following SQL script: Select
Have a Sql Script file name spCreation.sql with the following contents. CREATE PROCEDURE dbo.usp_select_customers
I have the following call in my RoR script: Foo.where(event = 'Login').group('user_id').order('user_id ASC').count() This
Hi all i have the following merge sql script which works fine for a
I have written this T-SQL script to roll up duplicate rows in a database
I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
Hi I have following sql statement that gives me the the first_name and total
I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.