6

PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Like this:

CREATE TABLE sal_emp 
(
    name            text,
    pay_by_quarter  integer[],
    schedule        text[][]
);

Some INSERT statements are as below

INSERT INTO sal_emp
VALUES ('Bill', '{10000, 10000, 10000, 10000}',
        '{{"meeting", "lunch"}, {"training", "presentation"}}');

INSERT INTO sal_emp
VALUES ('Carol', '{20000, 25000, 25000, 25000}',
        '{{"breakfast", "consulting"}, {"meeting", "lunch"}}');

SELECT * FROM sal_emp;

Output:

 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)

How can we achieve the same thing in SQL Server or any alternative to it?

13
  • There is no equivalent alternative in MS SQL. Only JSON msdn.microsoft.com/en-us/library/dn921897.aspx in latest versions. Commented Nov 21, 2016 at 7:22
  • 1
    @IvanStarostin ...and XML (since SQL Server 2005) :-) Commented Nov 21, 2016 at 7:30
  • 1
    Muahmmad, Even if this might be supported, I would consider this as bad design. Such depending lists should live in 1:n-related side tables... This concept has a lazy smell :-) Commented Nov 21, 2016 at 7:33
  • @Shnugo: It's not necessarily a bad design (and it gets mis-used way too often in Postgres), if a single attribute of an entity is an array by nature then an array is a good choice. But in this case a one-to-many relationship would be a much better choice - even in Postgres Commented Nov 21, 2016 at 7:38
  • 2
    @Shnugo In Postgres this is not done via a "side table". It's single, compressable variable length data structure. Any attribute where you always read and write the entire array (for further processing in your applications) but never need to access individual elements from within SQL is a possible candidate for this (but again: the shown example is not a valid example for a good choice. I completely agree that the information shown should not be stored in array - not even in Postgres) Commented Nov 21, 2016 at 7:49

1 Answer 1

2

There is NO alternative to array datatype in SQL Server.

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.