Free Newsletters:
DatabaseJournal  
DBANews
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Best Price
Cell Phones
Boat Donations
Calling Cards
KVM Switches
Promos and Premiums
Home Improvement
Prepaid Phone Card
Shop Online
Promote Your Website
Phone Cards
Build a Server Rack
Logo Design
Baby Photo Contest




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Solaris 8 Migration Assistant
Rapidly move your Solaris 8 application environments to new systems running Solaris 10 with the Solaris 8 Migration Assistant. Reduce migration risk while taking advantage of increased performance, reliability and security of the latest SPARC hardware platforms and Solaris 10 OS. »

 
Sun Eco Innovation: Good for Business, Good for the Environment
A complete solution to help you optimize and refresh your datacenter while properly recycling equipment and eliminating eWaste, including money-saving promotions to lower hardware acquisition costs. »

 
Sun Eco Innovation: Power Calculators
Power consumption has increasingly become a priority in customer's minds when purchasing new systems or storage. Sun's Power Calculators provide data on power consumption of Sun products allowing IT managers to better plan the power requirements in the datacenter to achieve better energy and cost savings. »

 
Optimize the Web Tier: Consolidate to Get More Performance in Less Space and Lower Power Consumption
Expansion in the Web tier is generally accomplished by adding more servers whenever extra capacity is needed. As the pool of servers grows larger, however, the complexity of the environment can grow exponentially. »

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MS SQL
May 6, 2008
Row Value Constructor in SQL Server 2008
By Muthusamy Anantha Kumar aka The MAK

We all know how to use Data Manipulation Language to Insert, Delete and Update data on a SQL Server table. Sometimes we need to insert too much data using insert statements. There are traditional and cumbersome methods to insert huge amounts of data. Now, SQL Server 2008 provides a new method to insert data to SQL Server tables, which simplifies the data insertion. This article is going to illustrate different methods to insert data to a table, including the new Row Value Constructor.

Traditionally we have three methods to insert data. They are as shown below.

Method 1

Let us assume that we have a database MyTestDB and we have a table MyTest1 as shown below.

USE [master]
GO
/****** Object:  Database [MyTestDB]    
  Script Date: 04/12/2008 00:46:37 ******/
IF  EXISTS (SELECT name FROM sys.databases 
  WHERE name = N'MyTestDB')
DROP DATABASE [MyTestDB]
GO
Create database MyTestDB
Go
Use [MyTestDB]
Go
IF  EXISTS (SELECT * FROM sys.objects 
  WHERE object_id = OBJECT_ID(N'[dbo].[MyTest1]') 
  AND type in (N'U'))
DROP TABLE [dbo].[MyTest1]
GO
USE [MyTestDB]
GO
/****** Object:  Table [dbo].[MyTest1]    
  Script Date: 04/12/2008 00:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MyTest1](
 [Id] [int] NULL,
 [Fname] [varchar](100) NULL,
 [Lname] [varchar](100) NULL,
 [salary] [money] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Now let’s add 5 rows of data using a traditional ANSI insert SQL statement as shown below. Here we are going to use the INSERT SQL Statement with a VALUE clause to insert data.

insert into MyTest1 (id ,fname ,lname , salary) values (1 , 'John' , 'Smith' , 150000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (2 , 'Hillary' , 'Swank' , 250000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (3 , 'Elisa' , 'Smith' , 120000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (4 , 'Liz' , 'Carleno' , 151000.00)
insert into MyTest1 (id ,fname ,lname , salary) values (5 , 'Tony' , 'Mcnamara' , 150300.00)

Result

(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)

Method 2

Let’s assume that we have a table MyTest2 on the above database MyTestDB as shown below.

USE [MyTestDB]
GO
/****** Object:  Table [dbo].[MyTest2]    Script Date: 04/12/2008 00:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
/****** Object:  Table [dbo].[MyTest2]    Script Date: 04/12/2008 00:48:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MyTest2]') AND type in (N'U'))
DROP TABLE [dbo].[MyTest2]
GO
CREATE TABLE [dbo].[MyTest2](
 [Id] [int] NULL,
 [Fname] [varchar](100) NULL,
 [Lname] [varchar](100) NULL,
 [salary] [money] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Now let’s add 5 rows of data using a traditional insert SQL statement as shown below. Here we are going to use the INSERT SQL Statement with a SELECT clause to insert data.

insert into MyTest2 select 1 , 'John' , 'Smith' , 150000.00
insert into MyTest2 select 2 , 'Hillary' , 'Swank' , 250000.00
insert into MyTest2 select 3 , 'Elisa' , 'Smith' , 120000.00
insert into MyTest2 select 4 , 'Liz' , 'Carleno' , 151000.00
insert into MyTest2 select 5 , 'Tony' , 'Mcnamara' , 150300.00

Result

(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)

Method 3

Let’s assume that we have a table MyTest3 on the above database MyTestDB as shown below.

USE [MyTestDB]
GO
/****** Object:  Table [dbo].[MyTest3]    Script Date: 04/12/2008 00:48:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MyTest3]') AND type in (N'U'))
DROP TABLE [dbo].[MyTest3]
GO
USE [MyTestDB]
GO
/****** Object:  Table [dbo].[MyTest3]    Script Date: 04/12/2008 00:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MyTest3](
 [Id] [int] NULL,
 [Fname] [varchar](100) NULL,
 [Lname] [varchar](100) NULL,
 [salary] [money] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Now let’s add 5 rows of data using a traditional insert SQL statement as shown below. Here we are going to use the INSERT SQL Statement with a SELECT and UNION clause to insert data.

insert into MyTest3 
select 1 , 'John' , 'Smith' , 150000.00
union select 2 , 'Hillary' , 'Swank' , 250000.00
union select 3 , 'Elisa' , 'Smith' , 120000.00
union select 4 , 'Liz' , 'Carleno' , 151000.00
union select 5 , 'Tony' , 'Mcnamara' , 150300.00

Result:

(5 row(s) affected)

Method 4

Let’s assume that we have a table MyTest4 on the above database MyTestDB as shown below.

USE [MyTestDB]
GO
/****** Object:  Table [dbo].[MyTest4]    Script Date: 04/12/2008 00:48:04 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MyTest4]') AND type in (N'U'))
DROP TABLE [dbo].[MyTest4]
GO
USE [MyTestDB]
GO
/****** Object:  Table [dbo].[MyTest4]    Script Date: 04/12/2008 00:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MyTest4](
 [Id] [int] NULL,
 [Fname] [varchar](100) NULL,
 [Lname] [varchar](100) NULL,
 [salary] [money] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

Now let’s add 5 rows of data using the new Row Value Constructor insert SQL statement as shown below. Here we are going to use the INSERT SQL Statement with a Row Value Constructor.

insert into MyTest4 (id ,fname ,lname , salary) values 
(1 , 'John' , 'Smith' , 150000.00),
(2 , 'Hillary' , 'Swank' , 250000.00),
(3 , 'Elisa' , 'Smith' , 120000.00),
(4 , 'Liz' , 'Carleno' , 151000.00),
(5 , 'Tony' , 'Mcnamara' , 150300.00)

Result:

(5 row(s) affected)

Conclusion

This article illustrated different methods to insert data to a table, including the new Row Value Constructor that simplifies the bulk data insertion.

» See All Articles by Columnist MAK

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS SQL Archives

What's The Future Of IT? Find Out By Reading "IT in 2018" Now. Free Registration Required.
IT in 2018: Download Free eBook By The Author Of "Does IT Matter?" Simple Registration Is Required.
Five Trends for Application Development. Download Your Complimentary Report. Exclusive. Act Now.
Download: SQL Backup & DBA Best Practices eBook.
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications


Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
How To Transfer Access Data Records To SQL ?? ankurdjariwala 1 May 8th, 12:24 PM
problem with federated server linking majidkhan 1 April 29th, 10:00 AM
"SELECT rowguidcol" from tables on linked servers? brentbordelon 1 April 25th, 04:12 PM
"SELECT rowguidcol" vs. "SELECT <actual name>" rgarrison 9 April 16th, 03:46 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES