3

I use ms sql and i need to create a table with a array column of nvarchar. What is the correct query ?

13
  • 5
    Not possible. SQL Server does not support arrays - but that's a bad idea to begin with. Why don't you properly normalize your data model? Commented Jul 8, 2019 at 9:08
  • Ttere are no array columns. In SQL, the language, fields should contain single values. It could be a single complex value like a coordinate but it's still a single value that's treated as one item. Even in databases that have arrays, like Oracle, they are treated as a single item and their contents can't be queried individually. What are you trying to do? Commented Jul 8, 2019 at 9:08
  • i need to create a table that contanis so many column, but 60 of that are similar and i want to insert that into an array. There is a limit for the column of a table? Commented Jul 8, 2019 at 9:09
  • 1
    @PanagiotisKanavos: actually the SQL standard does define arrays (and Oracle does not have arrays as a datatype in SQL, only in PL/SQL) Commented Jul 8, 2019 at 9:10
  • 1
    @AttilioIurlaro: you need additional tables to store the one-to-many relationships. Please read up on database normalization Commented Jul 8, 2019 at 9:14

1 Answer 1

1

You don't want an array column. You want a separate table (or perhaps a JSON/XML column, but I won't focus on that).

The normal method would be:

create table main (
    main_id int identity primary key,
    . . .
);

create table element (
    element_id int identity primary key,
    position int,
    value varchar(255),
    . . .
);

create table main_elements (
    main_element_id int identity primary key,
    main_id int references main(main_id),
    element_id int references elements(element_id)
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.