MSSQL添加变量

时间:2019-05-22 13:08:00

标签: sql-server tsql

我如何在MSSQL中添加一个变量来替换%MV_AUG2019.json%,以便只为所有这些表在一​​个位置上更改它?

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%'

3 个答案:

答案 0 :(得分:3)

我敢打赌你只是在->

之后
NULL

答案 1 :(得分:2)

您必须在以下声明一个变量:

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

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

答案 2 :(得分:1)

您可以动态生成表

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')