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 7540831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:44:09+00:00 2026-05-30T07:44:09+00:00

For a insert … select statement, is there a way to ignore a variable

  • 0

For a insert ... select statement, is there a way to ignore a variable that is required for the select statement but should be inserted?

I’m using a rank function to select which data to insert. Unfortunately the rank function can’t be called in the where clause.

insert into target_table(v1
                        ,v2
                        ,v3
                        )
select v1
      ,v2
      ,v3
      ,RANK() over (partition by group_col
                    order by order_col desc
                   ) as my_rank
from source_table
where my_rank > 1

The result is the following error:

Msg 121, Level 15, State 1, Line 1 The select list for the INSERT
statement contains more items than the insert list. The number of
SELECT values must match the number of INSERT columns.

I know I can do this using a temporary table but would like to keep it in a single statement if possible.

  • 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-30T07:44:11+00:00Added an answer on May 30, 2026 at 7:44 am

    Wrap your main query inside a subquery.

    INSERT INTO target_table
        (v1, v2, v3)
        SELECT q.v1, q.v2, q.v3
            FROM (SELECT v1, v2, v3, 
                         RANK() OVER (PARTITION BY group_col ORDER BY order_col DESC) AS my_rank
                      FROM source_table) q
            WHERE q.my_rank > 1;
    

    For SQL Server 2005+, you could also use a CTE:

    WITH cteRank AS (
        SELECT v1, v2, v3, 
               RANK() OVER (PARTITION BY group_col ORDER BY order_col DESC) AS my_rank
            FROM source_table
    )
    INSERT INTO target_table
        (v1, v2, v3)
        SELECT v1, v2, v3
            FROM cteRank
            WHERE my_rank > 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

INSERT IGNORE INTO `PREFIX_tab_lang` (`id_tab`, `id_lang`, `name`) (SELECT `id_tab`, id_lang, (SELECT tl.`name` FROM `PREFIX_tab_lang`
insert ignore into table1 select 'value1',value2 from table2 where table2.type = 'ok' When I
INSERT IGNORE INTO table3 (id1, id2) VALUES SELECT id1, id2 FROM table1, table2; What's
INSERT INTO files (fileUID, filename) WITH fileUIDS(fileUID) AS ( VALUES(1) UNION ALL SELECT fileUID+1
INSERT INTO [Tasks] ([LoginName] ,[Type] ,[Filter] ,[Dictionary] ,[Description]) Select N'Anonymous',4,'SomeTable.targetcode in (select Code from
INSERT INTO item_quotation (item_id, quotation_id,name_searched,item_name,other_name,selling_price,discounted_price) SELECT DISTINCT I.item_id, . $quotation_id . ,T.item_name, I.name,I.other_name, INV.selling_price,
Like: insert into table (col) values (N'multilingual unicode strings') I'm using SQL Server 2008
string.insert in c# doesn't overwrite the character that is in the startindex does it?
I am trying to INSERT INTO a table using the input from another table.
insert into keyword_history ( keyword_id, searchengine, location, 4, curdate() ) select keyword_id, searchengine, location

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.