New INSERT syntax in SQL Server 2008

Published On: 2008-04-03By:

One of the very cool new feature which SQL Server 2008 gives us is an change to the INSERT statement.  Now you can specify multiple rows to insert into a table from a single insert command.

The syntax is:
CREATE TABLE TableName (Column1 INT, Column2 VARCHAR(10))
INSERT INTO TableName
(Column1, Column2)
VALUES
(1, 'test1'), (2, 'test2'), (3, 'test4')

I see this as being a very handy especially when doing an initial data load into a table as you can now load lots of data without having to run a lot of seperate insert statements.

Denny


Contact the Author | Contact DCAC

New Article: Tips for scheduling and testing SQL Server backups

Published On: 2008-04-01By:

Back To Basics: The UPDATE Statement

Published On: 2008-03-31By:

After you’ve inserted the data into the table, it’s time to update the data.  We do this by using the UPDATE statement.  The update statment can be used in two ways.  The first is to update a record or set of records in a single table, by simply filtering the data in the table by using values in the table.

UPDATE TableName
SET Column1 = 'Value'
WHERE AnotherColumn = 'AnotherValue'

A more complex update uses another table as the source of the data. This makes the UPDATE statement look like a combination of the UPDATE statement and the SELECT statement.

UPDATE TableName
SET Column2 = AnotherTable.Column3
FROM AnotherTable
WHERE TableName.Column1 = AnotherTable.Column1

We can add joins into this as well, so that we can update more than one column from different tables at the same time.

UPDATE TableName
SET Column2 = AnotherTable.Column3,
Column3 = ThirdTable.Column2
FROM AnotherTable
JOIN ThirdTable ON AnotherTable.Column5 = ThirdTable.Column4
WHERE TableName.Column1 = ThirdTable.Column1

I hope that you find this post useful. I encourage everyone to open up Books OnLine and read through the information on the UPDATE statement. It includes more examples, and some of the other options which are available to you.

Denny


Contact the Author | Contact DCAC

Joins vs. Exists vs. IN: Not all filters are created the same.

Published On: 2008-03-27By:

Not all filter commands are created equal.  Different filtering operations should be used at different times to get the best performance our of your SQL Server.

While the JOIN, EXISTS and IN filters can give you the same results the way that SQL Server gets to the data is very different and can lead to poor system performance.  Also when doing a select vs. a delete these same operators will give different execution plans.

I’ll refer you do this file which will provide you with some sample code which can be run against the AdventureWorksDW sample database.  Run each query with the execution plan being displayed.  You’ll see that the IN and EXISTS both produce the same plan, while the JOIN produces a better plan when it comes to selecting data.  However when it comes to deleting the data the EXISTS and IN produce a better plan than the JOIN command does.  (Don’t worry, these delete scripts won’t actually remove any data from the table.  The data these scripts try to delete doesn’t actually exist.  We are looking for execution plans here, not actual deletes).

Denny


Contact the Author | Contact DCAC
1 415 416 417 418 419 435

Video

Globally Recognized Expertise

As Microsoft MVP’s and Partners as well as VMware experts, we are summoned by companies all over the world to fine-tune and problem-solve the most difficult architecture, infrastructure and network challenges.

And sometimes we’re asked to share what we did, at events like Microsoft’s PASS Summit 2015.

Awards & Certifications

Microsoft Partner    Microsoft MVP    Microsoft Certified Master    VMWare Partner    VMWare vExpert
   Best Full-Service Cloud Technology Consulting Company    Insights Sccess Award    Technology Headlines Award    Golden Bridge Gold Award    CIO Review Top 20 Azure Solutions Providers
Share via
Copy link