Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6099805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:18:54+00:00 2026-05-23T13:18:54+00:00

Environment: SQL Server 2005/2008, pubs database I have inserted into a table variable a

  • 0

Environment: SQL Server 2005/2008,
pubs database

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

Sample Data

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:18:55+00:00Added an answer on May 23, 2026 at 1:18 pm

    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
    

    Use what you want

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Environment: Team Foundation Server 2005 Visual Studio 2008 I have a reasonably large project
My dev environment has SQL Server 2008 installed. I have an asp.net 1.1 site
Environment : SQL Server 2005 I have this type of request SELECT * FROM
I have a datagrid view which shows table data from a database Environment: -
Working environment is SQL Server 2000. I have a table with no indexes, no
I have two tables in a SQL Server 2008 environment with the following structure
I have two instances of SQL Server: Server1 (SQL Server 2008) Server2 (SQL Server
All - I have recently upgraded our production environment from Sql Server Reporting Services
I'm setting up a dev. environment and my production environment has SQL Server 2005.
I'm trying to move my database.mdf file from a development environment to a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.