SQL Server 2005 Virtual Labs
Simple Query
Training Spot
SQL Book
Database Normalization
Jul 10, 2009
Jul 9, 2009
T-SQL Split function of comma delimited input param
Check this Split function found on Kris' Blog
Basically, pass in the stored proc a comma delimited list and it will return a temp table holding the id's
Basically, pass in the stored proc a comma delimited list and it will return a temp table holding the id's
Jul 8, 2009
T-SQL Full Outer Join Example
DECLARE @Table1 TABLE ( id INT, name NVARCHAR(20) )
DECLARE @Table2 TABLE ( id INT, name NVARCHAR(20) )
INSERT INTO @Table1 VALUES (1, 'A')
INSERT INTO @Table1 VALUES (2, 'B')
INSERT INTO @Table1 VALUES (3, 'C')
INSERT INTO @Table2 VALUES (2, 'B')
INSERT INTO @Table2 VALUES (3, 'C')
INSERT INTO @Table2 VALUES (4, 'D')
SELECT a.id, a.name FROM @Table1 a LEFT OUTER JOIN @Table2 b ON a.name = b.name
WHERE b.id IS NULL UNION ALL SELECT a.id, a.name FROM @Table2 a LEFT OUTER JOIN
@Table1 b ON a.name = b.name WHERE b.id IS NULL
SELECT Coalesce(t1.id, t2.id) AS id, Coalesce(t1.name, t2.name) AS nume FROM
@Table1 t1 FULL OUTER JOIN @Table2 t2 ON t1.id = t2.id WHERE t2.id IS NULL OR
t1.id IS NULL
Results:
Id Name
----------- --------------------
1 A
4 D
(2 row(s) affected)
Id Nume
----------- --------------------
1 A
4 D
So, use full outer join when you want unmatched records from both tables.
Subscribe to:
Posts (Atom)