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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:36:41+00:00 2026-05-28T06:36:41+00:00

I have MSSQL table having columns, serves as storage for items for user with

  • 0

I have MSSQL table having columns, serves as storage for items for user with certain name. Each user have one record in table Items that looks like that:

Name(string)  1(string)  2(string)  3(string)  4(string)  5(string) 

When inserting data, I would need to use the column (“packet”) ona by one. Meaning if “1” contains data, use column “2”. If “1” is empty, use “1”. If only “3” is empty, use “3”/
In other words, I always need to select the lowest empty column to write the data in. If none of these 5 is empty, then do not write data at all.
Could anyone help me with that query? Thank you

  • 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-28T06:36:42+00:00Added an answer on May 28, 2026 at 6:36 am

    The correct way to model this in SQL would be:

    CREATE TABLE Tab (
        Name varchar(20) not null, --20? Who knows, not specced in question
        Idx int not null,
        Value varchar(20) not null, --20? Who knows, not specced in question
        constraint PK_Tab PRIMARY KEY (Name,Idx),
        constraint CK_Tab_Indexes CHECK (Idx between 1 and 5)
    )
    

    You could then write your insert as:

    INSERT INTO Tab (Name,Idx,Value)
    SELECT @Name,COALESCE((select MAX(Idx)+1 from Tab where Name=@Name),1),@Value
    

    where @Name and @Value are the supplied values to insert. If there are already 5 items in the table for a particular name, you’ll get a check constraint error and the insert will fail.

    This also makes querying for particular values possible, without having to search 5 different columns, and it means that extending to more items in the future far easier (by just changing the check constraint)


    I’d had the impression that this was insert only. If deletions are occurring, and you want to reuse items, then the insert query would be:

    ;WITH Nums(n) as (select 1 union all select 2 union all select 3 union all select 4 union all select 5)
    INSERT INTO Tab (Name,Idx,Value)
    SELECT @Name,(select MIN(n) from Nums where not exists (select * from Tab where Name=@Name and Idx = n)),@Value
    

    which will now fail because of a not-null constraint once you’re up at 5 items. It could be simpler if you already have a numbers table in your database, but I’ve assumed not above.

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

Sidebar

Related Questions

Say I have an MSSQL table with two columns: an int ID column that's
I have an MSSQL 2000 table that has a lot of duplicate entries. Each
If I have an mssql varchar[1024] that is always empty in a table, how
I have a table in a MSSQL database that looks like this: Timestamp (datetime)
I have a MySQL table with approximately 3000 rows per user. One of the
I have a MySQL database table with two columns that interest me. Individually they
I have a table that stores all the volunteers, and each volunteer will be
I have a MySQL table which has about 30 columns. One column has empty
I have a table that stores user information in a table in a MySQL
I have a table with several hundred million rows. One of the columns is

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.