Chapter 5 ADVANCED DATA RETRIEVAL AND MODIFICATION 269

Chapter 5 ADVANCED DATA RETRIEVAL AND MODIFICATION 269 INSERT INTO Table1 VALUES (1, 3.14, 42 , Bogus ) INSERT INTO Table1 VALUES (2, 2.1828, 93 , Data ) INSERT INTO Table2 VALUES (123.45, 3, 16) INSERT INTO Table2 VALUES (456.78, 4, 29) SELECT A, B, C FROM Table1 UNION SELECT First, Second, Third FROM Table2 Notice that the column names are specified. If you use SELECT * with both, they have an inconsistent number of columns, and you ll get an error, All queries in an SQL statement containing a UNION operator must have an equal number of expressions in their target lists. This returns successfully, and the data types are a numeric(5,2), a float, and an int. The numeric(5,2) is the compromise between an int and a numeric(5,2); the float is the compromise between a float and an int; and the data is converted to int because all the varchar values in Table1 can convert to int. If there had been a string that couldn t convert to an int, SQL Server would have thrown an error message. If it can t convert a string to a numeric value, it throws an error. Three additional notes on UNION. First, if you want to sort a UNION, you put the ORDER BY after the last SELECT statement, like this: SELECT A, B, C FROM Table1 UNION SELECT First, Second, Third FROM Table2 ORDER BY 1 Next, if you want to do a SELECT…INTO operation, you need to do it as follows: SELECT A, B, C INTO #UnionOutput FROM Table1 UNION SELECT First, Second, Third FROM Table2 Finally, the column names returned are taken from the first query in the set of UNION operators. So, in this example the columns would be named A, B, and C.

If you looking for unlimited one inclusive web hosting plan please check cheap web hosting website.

Comments are closed.