Is there way to insert the same data into multiple columns is SQL?

66 views Asked by At

Let's say we wanted to put the same data into multiple rows like so.

Column names for this example:

Monday, Tuesday, Wednesday, Thursday, Friday 

INSERT INTO sample 
VALUES ('long string')

This is an example of what I mean so the same value is in each column

Monday      |  Tuesday    | Wednesday   | Thursday    | Friday 
long string | long string | long string | long string | long string

I want that long string within all columns, is there a way to do this without using multiple insert statements? OR is there a way to create a CTE that could automate this to make it easier?

3

There are 3 answers

3
Gordon Linoff On

You have to list all the values:

insert into sample (Monday, Tuesday, Wednesday, Thursday, Friday)
    values ('long string', 'long string', 'long string', 'long string', 'long string');

You don't specify the database, but you could use a subquery to reduce the size of the query:

insert into sample (Monday, Tuesday, Wednesday, Thursday, Friday)
    select val, val, val, val, val
    from (select 'long string' as val) x;
3
Jay Ganesan On

Try

insert into sample (Monday, Tuesday, Wednesday, Thursday, Friday) values select 'long string'

0
twoleggedhorse On

You could expand Gordon's example like this:

DECLARE @valuesToInsert TABLE (val NVARCHAR(MAX))
INSERT INTO @valuesToInsert
VALUES 
('This is a long sentence'), 
('This is another long sentence'), 
('This is yet another long sentence')

INSERT INTO sample (Monday, Tuesday, Wednesday, Thursday, Friday)
SELECT  val, val, val, val, val
FROM    @valuesToInsert

@valuesToInsert is your datasource that you want to use (I've created a table variable just to show multiple data rows