6,7 tạo một bàn mới với dữ liệu từ hiện tại Bàn tôi thường cần để tạo bảng mới từ dữ liệu hiện có. Nếu bảng đã tồn tại, tôi cần phải xóa bảng cũ đầu tiên. | Create a New Table with Data from Existing Tables I often need to create new tables from existing data. If the table already exists I need to delete the old table first. How do I do this using T-SQL Technique To perform these tasks you will create two T-SQL statements and use them in one Command object. Here are the two statements that will be used IF EXISTS SELECT FROM sysobjects WHERE id object_id N Northwind . dbo . MyProdAndCat DROP TABLE Northwind . dbo . MyProdAndCat SELECT INTO MyProdAndCat FROM Categories INNER JOIN ProductsON The first statement checks for the existence of the particular table you will be creating in this case MyProdAndCat. This statement demonstrates a couple of techniques that you can use in T-SQL Using the EXIST statement with a SELECT statement querying the system table called sysobjects Using an IF statement to conditionally execute another command in this case the DROP TABLE statement Tip Now that you have learned this technique you will want to use it repeatedly. Make sure you mark this page The last statement uses an inner join to join two tables displaying the CategoryName and ProductName. The clause that creates the new table is this INTO MyProdAndCat This tells SQL Server to create a new table called MyProdAndCat from the SELECT statement that is specified. Steps Open and run the Visual Basic .NET-Chapter 6 solution. From the main form click on the button with the caption How-To . You will see the SQL string specified in the Technique section displayed in a label. If you click the Execute button the new table is generated a SELECT statement is executed and the results are displayed in the DataGrid object see Figure . 1. Create a Windows Form. Then place the controls listed in Table with the following properties set as displayed in Figure . Table . Control Property Settings for This How-To Object Property Setting Label Text