-- Populate sample table
CREATE TABLE Foo (
keycol INT PRIMARY KEY,
datacol VARCHAR(30));
INSERT INTO Foo VALUES (1, 'Books');
INSERT INTO Foo VALUES (2, 'CDs');
INSERT INTO Foo VALUES (3, 'DVDs');
-- or
INSERT INTO Foo (keycol, datacol)
SELECT 1, 'Books'
UNION ALL
SELECT 2, 'CDs'
UNION ALL
SELECT 3, 'DVDs';
-- or using on the fly
SELECT keycol, datacol
FROM ( SELECT 1, 'Books'
UNION ALL
SELECT 2, 'CDs'
UNION ALL
SELECT 3, 'DVDs') AS Foo (keycol, datacol);
INSERT INTO Foo (keycol, datacol)
VALUES (1, 'Books'), (2, 'CDs'), (3, 'DVDs');
-- or using on the fly
SELECT keycol, datacol
FROM ( VALUES (1, 'Books'),
(2, 'CDs'),
(3, 'DVDs') ) AS Foo (keycol, datacol);
-- and CTE version
WITH Foo (keycol, datacol)
AS( SELECT *
FROM ( VALUES (1, 'Books'),
(2, 'CDs'),
(3, 'DVDs') ) AS F (keycol, datacol))
SELECT keycol, datacol
FROM Foo;
INSERT INTO Foo (keycol, datacol)
VALUES ((SELECT MAX(keycol) + 1 FROM Foo), 'Tapes');
SELECT keycol, datacol
FROM Foo
WHERE (keycol, datacol) IN (SELECT keycol, datacol FROM Foo2);
-- or
UPDATE Foo
SET (keycol, datacol) = (SELECT keycol, datacol FROM Foo2);
Labels: sql server 2008, t-sql programming