Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert Statement Questions

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-01-27 : 11:45:14
I have a simple one column tempt table. I am inserting data into this table as shown below. In trying to reduce the number of lines of code, can I add the records all at once without having a separate insert statement for each record? Basically, something like insert into #Yaks (YakName) values('21a'); values('21b'); values('21c');

CREATE TABLE #Yaks (
YakName nvarchar(3) )

-- insert symbols supplied via Bob
insert into #Yaks (YakName)
values('21B')
insert into #Yaks (YakName)
values('21C')
insert into #Yaks (YakName)
values('46W')
insert into #Yaks (YakName)
values('B22')
insert into #Yaks (YakName)
values('D70')
insert into #Yaks (YakName)
values('D72')
insert into #Yaks (YakName)
values('D73')
insert into #Yaks (YakName)
values('D78')
insert into #Yaks (YakName)
values('D81')
insert into #Yaks (YakName)
values('F52')
insert into #Yaks (YakName)
values('G77')
insert into #Yaks (YakName)
values('G85')
insert into #Yaks (YakName)
values('G86')
insert into #Yaks (YakName)
values('G87')

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-27 : 11:51:21
you can


insert into #Yaks (YakName)
select '21B' union all
select '21C' union all
...
select 'G87'
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-27 : 11:52:47
Would this sort of syntax do?


CREATE TABLE #Yaks (
YakName nvarchar(3) )

-- insert symbols supplied via Bob
insert into #Yaks (YakName)
SELECT '21B'
UNION ALL SELECT '21C'
UNION ALL SELECT '46W'
UNION ALL SELECT 'B22'
UNION ALL SELECT 'D70'
UNION ALL SELECT 'D72'
UNION ALL SELECT 'D73'
UNION ALL SELECT 'D78'
UNION ALL SELECT 'D81'
UNION ALL SELECT 'F52'
UNION ALL SELECT 'G77'
UNION ALL SELECT 'G85'
UNION ALL SELECT 'G86'
UNION ALL SELECT 'G87'
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-27 : 11:53:32
or if sql 2008

insert into #Yaks (YakName)
values ('21B'),('21C'),...,('G87')
Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-01-27 : 11:56:58
Thanks, the Union All will be fine.

visakh16, I wish I had 2008. The syntax in your example is what I would like to be using.

Oh well....
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-27 : 11:58:45
1m26s

I had to write a macro to convert all the INSERTS into SELECTS, I can lend it to you it you like?
Go to Top of Page
   

- Advertisement -