0

How can I add a variable in SQL Server to replace %MV_AUG2019.json% so that I only have to change it in one spot for all of those tables?

delete from [addresses] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [disabilities] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [MD] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [messages] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [MH] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [MV] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [permits] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [PP] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [registrationOwners] where [uniqueFilename] like '%MV_AUG2019.json%'
delete from [stops] where [uniqueFilename] like '%MV_AUG2019.json%'
1

3 Answers 3

3

I bet you are simply after -->

DECLARE @FileName NVARCHAR(500) = 'MV_AUG2019.json'

delete from [addresses] where [uniqueFilename] like '%'+@FileName+'%'
Sign up to request clarification or add additional context in comments.

2 Comments

What is the purpose of doubling up the wildcards ('%') by concatenating them to a variable which already contains them?
Doh!, The op said filename, I assumed that was part of the filename, updated.
2

You have to declare a variable in the following:

DECLARE @var NVARCHAR(1000) = 'name.json' -- provide file name here

delete from [addresses] where [uniqueFilename] like '%' + @var + '%'

Comments

1

you can generate tables in dynamic

DECLARE @FileName NVARCHAR(500) = 'MV_AUG2019.json'

SELECT 'DELETE FROM '+QUOTENAME(TABLE_NAME) +' WHERE '+ QUOTENAME(COLUMN_NAME)+ ' LIKE '+'''%'+ @FileName+'%'''
FROM  INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME ='uniqueFilename' AND DATA_TYPE IN ('char','nvarchar','varchar')

1 Comment

Tip: The best practice when assembling object names into dynamic SQL statements is to use QuoteName() to avoid problems with odd names, e.g. New Table with a space or reserved words like From.

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.