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

  • Home
  • SEARCH
  • 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 6087053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:49:34+00:00 2026-05-23T11:49:34+00:00

DECLARE tableList CURSOR FOR SELECT t.name FROM sys.tables t INNER JOIN sys.columns c ON

  • 0
DECLARE tableList CURSOR FOR    
    SELECT  t.name 
    FROM sys.tables t
        INNER JOIN sys.columns c
        ON t.object_id = c.object_id
    WHERE
            t.name NOT LIKE 'z%'
        AND t.name NOT LIKE '%delete%'
        AND t.name <> 'tblUsers'
        AND t.name <> 'tblUserLogins'
        AND t.name <> 'searchR'
        AND t.name <> 'tblUserPortfolio'
        AND t.name <> 'alerts_User'
        AND c.name LIKE 'userid'
        OR  c.name LIKE 'user_id'
    ORDER BY name
OPEN tableList
FETCH NEXT FROM tableList
INTO @tablename

Above is the query for building the cursor, and it is using quite a few LIKE, NOT LIKE operations, which I assume might be expensive to run.

So I am asking if there is any better approach to build up a query without using too many LIKE/NOT LIKE and make it more optimal.

Thanks.

EDIT:

The intention of using cursor here is to Loop Through all matched tables so we then can insert/update/delete records dynamically.

There are more than 150 tables in the database that I am using, so I thought that sql server might as well do the dirty work.

  • 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-23T11:49:35+00:00Added an answer on May 23, 2026 at 11:49 am

    I would:

    1) make the SQL more readable (IMO) by using NOT IN instead of multiple “… and t.name <> …”
    2) changed c.name LIKEs to a single IN clause as you appear to want an exact match, not needing LIKE

    SELECT  t.name 
    FROM sys.tables t
        INNER JOIN sys.columns c ON t.object_id = c.object_id
    WHERE t.name NOT LIKE 'z%' AND t.name NOT LIKE  '%delete%' 
        AND t.name NOT IN  ('tblUsers','tblUserLogins','searchR','tblUserPortfolio','alerts_User')
        AND c.name IN ('userid','user_id') 
    ORDER BY name
    

    This actually isn’t likely to make a great deal of different tbh performance-wise, but I think makes it more readable/maintainable.

    But, the main point I would raise is whether you actually need a cursor at all – I’d definitely look to remove that in favour of a set-based approach. Though would need to know what you are doing in the cursor in order to suggest an alternative.

    Edit:
    You could try this kind of approach which dynamically generates the SQL and executes it in one go (simple example, assuming you want to insert the same row into each table which each has the same structure). Difficult to know if this would really suit your exact scenario (or whether it would actually make a worthwhile difference), but can be useful to know this technique.

    DECLARE @nSQL NVARCHAR(MAX)
    SELECT @nSQL = COALESCE(@nSQL, '')  + 
    'INSERT ' + QUOTENAME(t.name) + '([SomeCol]) VALUES (@ValueToInsert);' + CHAR(10)
    FROM sys.tables t
        INNER JOIN sys.columns c ON....
    ...{rest of current SELECT)
    
    -- comment out PRINT, and uncomment EXECUTE statement to actually run the SQL
    PRINT @nSQL
    -- EXECUTE sp_executesql @nSQL, N'@ValueToInsert VARCHAR(10)', 'NewValue'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

declare v_date date; CURSOR abc is select a_date from abc where part_id ='E00000001'; begin
DECLARE @result varchar(MAX) SELECT NAME FROM Table WHERE ID = @Id I need to
declare fName varchar2(255 char); begin SELECT x.constraint_name into fName FROM all_constraints x JOIN all_cons_columns
declare @top int set @top = 5 select top @top * from tablename Is
declare begin for i in (select * from emp) loop if i.sal=1300 then update
DECLARE @OrdersTemp TABLE ( OrderId UNIQUEIDENTIFIER ) INSERT INTO @OrdersTemp SELECT ord.Id FROM Orders
Declare @count nvarchar(max) set @count ='select COUNT(*) from '+ @tablename+'' if( @count =0 )
DECLARE vote_id INT; select id from votes where votes.question_id = question_id and vote_type_id =
declare @node int = 9044; DECLARE @sqlCommand NVARCHAR(MAX) = ( 'SELECT * FROM [@node].[database_name].dbo.table_name'
DECLARE @TotalMaxQty int SET @TotalMaxQty = 1000 SELECT PKId ,Qty FROM dbo.Sales PKId Qty

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.