MSSQL Server 2005

July 23rd, 2008

I’m currently working with MSSQL Server 2005 and why does Microsoft have to make everything difficult? With MySQL, a multiple insert is as simple as INSERT INTO table (columns) VALUES (array of values), (another array of values), … (last array of values). This way works in MS SQL 2008, but that’s another expense.

My current assignment in the office is to export and import MS Access data into a completely different schema in MSS. I will pose a problem at the end of this entry and if someone has a solution, I would be grateful to you.

Multiple insert in MSSQL 2005 Thanks to Pinal Dave for this

USE database_name
GO
INSERT INTO table_name (column1, column2, ... columnN)
SELECT 'value1a', 'value2a', ... 'valueNa'
UNION ALL
SELECT 'value1b', 'value2b', ... 'valueNb'
...
UNION ALL
SELECT 'value1n', value2n', ... 'valueNn'

Problem: How would I do a SELECT…INSERT in the middle of an insert because one or more columns are foreign keys. That way I don’t have to verify my data before inserting. Have the machine do it for me. For instance:

USE database_name
GO
INSERT INTO table_name (column1, column2, ... columnN)
SELECT 'value1a', [SELECT...INSERT], ... 'valueNa'
UNION ALL
SELECT 'value1b', [SELECT...INSERT], ... 'valueNb'
...
UNION ALL
SELECT 'value1n', [SELECT...INSERT], ... 'valueNn'

Related Posts

Entry Filed under: Software, Tutorials

Leave a Comment

Required

Required, hidden

*
To prove that you're not a bot, enter this code
Anti-Spam Image

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Pages

Popular Tags

Calendar

July 2008
M T W T F S S
« Jun   Aug »
 123456
78910111213
14151617181920
21222324252627
28293031  

Recent Comments

Associated Web Stop

Blogroll

Miscellaneous Hyperlinks

Shirtshop

Feed On

Enter your email address:

Visitors Online

Advertisement

Categories