Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从数据库中删除_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql 从数据库中删除

Sql 从数据库中删除,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我想从数据库中的每个表中删除一个特定属性。例如:我想从数据库中的每个表中删除值为“2”的CustomerID(列名) 我试图删除有customer字段且其值为2的记录,但我得到一个错误,该错误表示关键字delete附近的语法不正确 declare @SearchTerm nvarchar(4000) declare @ColumnName sysname set @SearchTerm = N'2' -- Term to be searched for set @ColumnName = N

我想从数据库中的每个表中删除一个特定属性。例如:我想从数据库中的每个表中删除值为“2”的
CustomerID
(列名)

我试图删除有customer字段且其值为2的记录,但我得到一个错误,该错误表示关键字delete附近的语法不正确

declare @SearchTerm nvarchar(4000) 
declare @ColumnName sysname

set @SearchTerm = N'2' -- Term to be searched for
set @ColumnName = N'customerID' --**column**

 set nocount on

 declare @TabCols table (
      id int not null primary key identity
      , table_schema sysname not null
      , table_name sysname not null
      , column_name sysname not null
      , data_type sysname not null
  )

insert into @TabCols (table_schema, table_name, column_name, data_type)
    select 
        t.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE
    from INFORMATION_SCHEMA.TABLES t
    join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_SCHEMA = c.TABLE_SCHEMA
                                      and t.TABLE_NAME = c.TABLE_NAME
    where 1 = 1
      and t.TABLE_TYPE = 'base table'
      and c.DATA_TYPE not in ('image', 'sql_variant')
      and c.COLUMN_NAME like case when len(@ColumnName) > 0 then @ColumnName else '%' end
    order by c.TABLE_NAME, c.ORDINAL_POSITION

declare
  @table_schema sysname
, @table_name sysname
, @column_name sysname
, @data_type sysname
, @exists nvarchar(4000) -- Can be max for SQL2005+
, @sql nvarchar(4000) -- Can be max for SQL2005+
, @where nvarchar(4000) -- Can be max for SQL2005+
, @run nvarchar(4000) -- Can be max for SQL2005+


while exists (select null from @TabCols) begin

select top 1
      @table_schema = table_schema
    , @table_name = table_name
   -- , @exists = 'select null from [' + table_schema + '].[' +    table_name + '] where 1 = 0'
    , @sql = 'delete''' + '[' + table_schema + '].[' + table_name + ']' + ''' as TABLE_NAME, from [' + table_schema + '].[' + table_name + '] where 1 = 0'
    , @where = ''
from @TabCols
order by id

while exists (select null from @TabCols where table_schema = @table_schema and table_name = @table_name) begin

    select top 1
          @column_name = column_name
        , @data_type = data_type
    from @TabCols
    where table_schema = @table_schema
        and table_name = @table_name
    order by id

    -- Special case for money
    if @data_type in ('money', 'smallmoney') begin
        if isnumeric(@SearchTerm) = 1 begin
            set @where = @where + ' or [' + @column_name + '] = cast(''' + @SearchTerm + ''' as ' + @data_type + ')' -- could also cast the column as varchar for wildcards
        end
    end
    -- Special case for xml
    else if @data_type = 'xml' begin
        set @where = @where + ' or cast([' + @column_name + '] as nvarchar(max)) like ''' + @SearchTerm + ''''
    end
    -- Special case for date
    else if @data_type in ('date', 'datetime', 'datetime2', 'datetimeoffset', 'smalldatetime', 'time') begin
        set @where = @where + ' or convert(nvarchar(50), [' + @column_name + '], 121) like ''' + @SearchTerm + ''''
    end
    -- Search all other types
    else begin
        set @where = @where + ' or [' + @column_name + '] like ''' + @SearchTerm + ''''
    end

    delete from @TabCols where table_schema = @table_schema and table_name = @table_name and column_name = @column_name

end

set @run = 'if exists(' + @exists + @where + ') begin ' + @sql + @where + ' print ''' + @table_name + ''' end'
print @run
exec sp_executesql @run

end

 set nocount off

使用风险自负。您需要取消注释exec行。但是首先运行它而不取消注释,以确保它得到您想要的表

declare @columnName nvarchar(255)
declare @intValue int
set @columnName = 'CustomerId'
set @intValue = 1
declare curs cursor for 
    select object_name(c.object_id) as tableName
      from sys.columns c 
     where c.name = @columnName

declare @tableName nvarchar(255)
open curs
fetch next from curs into @tableName
while @@fetch_status = 0
begin

    declare @sql nvarchar(1000)

    set @sql = 'delete ' + @tableName + ' where ' + @columnName + '=' + convert(varchar(10), @intValue)

    print @sql
    /* Uncomment the line below in order to run */
    --exec sp_executesql @sql
    fetch next from curs into @tableName
end
close curs
deallocate curs

下面是另一个基于ewahner发布的示例的方法。最大的区别是它不使用光标,因为你真的不需要光标

declare @columnName nvarchar(255)
declare @intValue int
set @columnName = 'CustomerId'
set @intValue = 1
declare @DeleteValue varchar(10)
set @DeleteValue = convert(varchar(10), @intValue)

declare @sql nvarchar(max) = ''

select @sql =  @sql + 'delete ' + object_name(c.object_id) + ' where ' + @columnName + ' = ' + @DeleteValue + ';'
from sys.columns c 
where c.name = @columnName

select @sql

/* Uncomment the line below in order to run */
--exec sp_executesql @sql

您的问题是什么?为什么不删除我在数据库中所有表中设置的值?您应该使用sys.tables和sys.columns,而不是传统的信息\u架构视图。您正在尝试更新列值还是删除行?这里当然不需要循环。你怎么可能不知道你在寻找什么样的数据类型呢?您是否建议CustomerID更改每个表中的数据类型?忘记代码吧。你所说的目标不明确。您是否正在查找存在customerId字段且其值为2的删除记录?如果要执行其他操作,请尽可能清楚地指定它是什么。它告诉我它找不到存储过程这里没有存储过程。这只是一个脚本。您可以放置一个
使用您的\u数据库\u名称
和一个
转到
,然后在我使用此代码时执行此脚本。它打印一条消息,表示表中的行已删除但未删除。您认为它打印消息是什么意思?你是说你看到delete语句,但如果你查看表格,它仍然在那里吗?你看到底部的部分了吗?说取消注释下面这行的注释???它会被注释掉,直到你满意它会做你想做的事。是的,我这样做了,收到了上面说的错误。找不到存储过程………错误消息:“找不到存储过程‘删除CustomerId=2的客户;删除CustomerId=2的订单’。”