我需要一些帮助与SQL状态人。 我有不同的架构和相同的表名。 用下面的选择我能得到的所有表:SQL选择行并从动态多个表中删除/更新
select name from sys.tables where QUOTENAME(name) = '[Table_1]'
现在我想遍历所有的表格,并有一些类似的查询到这一点:
delete from tablename where condition1 > condition2
我怎样才能做到这一点?
我需要一些帮助与SQL状态人。 我有不同的架构和相同的表名。 用下面的选择我能得到的所有表:SQL选择行并从动态多个表中删除/更新
select name from sys.tables where QUOTENAME(name) = '[Table_1]'
现在我想遍历所有的表格,并有一些类似的查询到这一点:
delete from tablename where condition1 > condition2
我怎样才能做到这一点?
试试这个
declare @count int, @i int = 1, @sql nvarchar(max), @tablename varchar(1000)
select @count = count(*) from sys.tables where QUOTENAME(name) = '[Table_1]'
create table #temp(id int identity(1,1), table varchar(1000))
insert into #temp
select name from sys.tables where QUOTENAME(name) = '[Table_1]'
while(@i<[email protected])
begin
select @tablename = (select table from #temp where id = @i)
set @sql = 'delete from '[email protected]+' where condition1>condition2'
execute sp_executesql @sql
set @i = @i+1
end
第一个错误是, 消息156,级别15,状态1,行5 关键字'表'附近的语法不正确。 因此,我将表更改为tbl 第二个错误是: 消息207,级别16,状态1,行12 无效的列名称'名称'。 – user3057678 2014-09-30 11:54:01
你现在可以检查 – Azar 2014-09-30 11:59:41
之后,我有消息207,级别16,状态1,行12 无效的列名称'名称'。在该行(从#temp中选择名称,其中id = @i) – user3057678 2014-09-30 11:59:57
这'DBMS'?! – 2014-09-30 11:37:30
你可能需要这样的东西http://stackoverflow.com/a/25780596/3682599 – 2014-09-30 11:39:17