1

I have a SQL Server table which contains a NVARCHAR(N) column with URLs. Unfortunatelly the URLs are absolute. A domain has changed and now links are broken (404). This table is really huge (milions of records). I want to replace the domain or try to make the links relative to fix this issue. How to do it as fast as it is possible with no lock on the table?

[EDIT]

I have also tried UPDATE and it works only a little bit longer than INSERT INTO/RENAME.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

UPDATE [TBL] 
SET [TBL].[Url] = REPLACE([TBL].[Url], 'str1', 'str2')
FROM [MyTable] [TBL] WITH(NOLOCK)

It could be done in this way during server maintenance :) Thanks for help!

2
  • also consider splitting the url into multiple columns. Commented Dec 17, 2015 at 10:40
  • Its worth noting that the sql you have written will take shared locks. so the nolock is pointless also is the uncommitted isolation level :) Commented Dec 18, 2015 at 12:16

1 Answer 1

3

Create a new table with the replacement result.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT [TBL].[OTHERCOLUMNS], REPLACE([TBL].[URL],'foo.com','bar.org')
INTO ReplacementTBL
FROM [SOURCETBL] [TBL]

GO
SP_RENAME 'schema.SOURCETBL', SOURCETBL_OLD
GO
SP_RENAME 'schema.ReplacementTBL', SOURCETBL

done

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.