在这种情况下如何使用 EXEC 或 sp_executeSQL 而不循

How to use EXEC or sp_executeSQL without looping in this case?(在这种情况下如何使用 EXEC 或 sp_executeSQL 而不循环?)
本文介绍了在这种情况下如何使用 EXEC 或 sp_executeSQL 而不循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:SQL Server 2005/2008,pubs数据库

Environment: SQL Server 2005/2008, pubs database

我使用 information_schema 表将一组数据插入到表变量中,如下所示.

I have inserted into a table variable a set of data as shown below using information_schema tables.

现在我想根据在列 dSQL 中执行查询的结果更新标志列.我能够使用循环/游标进行​​更新,然后使用 sp_executeSQL更新列,然后更新标志列.但是有没有一种替代的基于集合的方法来循环通过所有单独的行来做到这一点?

Now I would like to update the flag column based on the result of executing the query in the column dSQL. I was able to update using loops/cursor and then used sp_executeSQL to update the column and then update flag column later. But is there an alternate set-based way to do this without looping through all individual rows?

use pubs
go

declare @dsql Nvarchar(max)='', @tablename varchar(100), @colname varchar(100)
declare @t table (
 TABLE_NAME varchar(100),
 COLUMN_NAME varchar(100)
)

insert into @t
select distinct t.TABLE_NAME, c.COLUMN_NAME
from information_Schema.tables t
inner join
information_Schema.columns c
on t.TABLE_CATALOG = c.TABLE_CATALOG
where t.TABLE_SCHEMA = c.TABLE_SCHEMA
and t.TABLE_TYPE = 'BASE TABLE'
and c.DATA_TYPE = 'varchar'

select *, Dsql = 'select ' + COLUMN_NAME +  ' from ' + TABLE_NAME + ' WHERE '
+ COLUMN_NAME + ' = ''Menlo Park''', '' as Flag
FROM @t
GO   

我有一个想法,创建一个函数并为每一行调用该函数来执行单独的查询语句,但为每条记录调用该函数可能会影响性能.

I had an idea to create a function and call the function for each row to execute individual query statement but calling the function for each record might be a performance hit.

推荐答案

不可能,我之前做了一个类似的脚本.

Not possible, I made a script like it earlier.

declare @searchvalue varchar(100)

set nocount off

set @searchvalue = 'Hello world'


create table #tt (table_name varchar(64), column_name varchar(64), count int)
select * into #t from 
(
select 'select ''' + a.table_name + ''' ''table_name'',''' + a.column_name + ''' ''column_name'', count(*) count from [' + a.table_name +'] where [' +a.column_name+']='''+@searchvalue +'''' + ' group by ['+ a.column_name+']' sqlstring
from INFORMATION_SCHEMA.COLUMNS a
join 
INFORMATION_SCHEMA.TABLES b
on a.table_name = b.table_name
and b.table_type = 'base table'
 where data_type = 'varchar'
) a

--loop cursor
Declare @sqlstring as nvarchar(500)
Declare SqlCursor CURSOR FAST_FORWARD FOR
SELECT sqlstring FROM #t
OPEN SqlCursor
FETCH NEXT FROM SqlCursor
INTO @sqlstring
WHILE @@FETCH_STATUS = 0
BEGIN
insert #tt
exec(@sqlstring)
     FETCH NEXT FROM SqlCursor
     INTO @sqlstring
END
CLOSE SqlCursor
DEALLOCATE SqlCursor
select * from #tt
drop table #tt
drop table #t

随心所欲

这篇关于在这种情况下如何使用 EXEC 或 sp_executeSQL 而不循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)