你是Litware,Inc的数据库开发者。你正在修改该公司的sales数据库结构。该数据库将客户信息储存在Customers的表中。该表包含一个字段Country,其储存了客户所在的国家。你又创建一个新表Country。创建Customer和Country表的语法如下所示:
CREATE TABLE dbo.Country
(
CountryID int IDENTITY(1,1) NOT NULL,
CountryName char(20) NOT NULL,
CONSTRAINT PK_Country PRIMARY KEY CLUSTERED (CountryID)
)
CREATE TABLE dbo.Customers
(
CustomerID int NOT NULL,
CustomerName char(30) NOT NULL,
Country char(20) NULL,
CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (CustomerID)
)
你要尽可能快地将Customer表里头Country的信息转移到新Country表,该选用下面的哪条语句?
单项选择题
INSERT INTO Country (CountryName)
SELECT DISTINCT Country
FROM Customers
SELECT (*) AS ColID, c1.Country
INTO Country
FROM (SELECT DISTINCT Country FROM Customers)AS c1,
(SELECT DISTINCT Country FROM Customers) AS c2,
WHERE c1.Country >=c2.Country
GROUP BY c1.Country ORDER BY 1
DECLARE @Country char (20)
DECLARE cursor_country CURSOR
FOR SELECT Country FROM Customers
OPEN cursor_country
FETCH NEXT FROM cursor_country INTO @Country
WHILE (@@FETCH_STATUS <> -1)
BEGIN
If NOT EXISTS (SELECT CountryID
FROM Country
WHERE CountryName = @Country)
INSERT INTO Country (CountryName) VALUES (@Country)
FETCH NEXT FROM cursor_country INTO @Country
END
CLOSE cursor_country
DEALLOCATE cursor_country
DECLARE @SQL varchar (225)
SELECT @SQL = ‘bcp “SELECT ColID = COUNT(*), c1. Country’ +
‘FROM (SELECT DISTINCT Country FROM Sales..Customers) AS
c1, ' +
(SELECT DISTINCT Country FROM Sales..Customers) AS c2 '
+
WHERE c1.Country >= c2.Country’ +
‘GROUP BY c1.Country ORDER BY 1’ +
‘query out c:\country.txt -c’
EXEC master..xp_cmdshell @SQL, no_output
EXEC master..xp_cmdshell ‘bcp Sales..Country in c:\country. Txt-c’, no_output
答案:C
【原题】:你是Litware,Inc的数据库开发者。你正在修改该公司的sales数据库结构。该数据库将客户信息储存在Customers的表中。该表包含一个字段Country,其储存了客户所在的国家。你又创建一个新表Country。创建Customer和Country表的语法如下所示:
CREATE TABLE dbo.Country
(
CountryID int IDENTITY(1,1) NOT NULL,
CountryName char(20) NOT NULL,
CONSTRAINT PK_Country PRIMARY KEY CLUSTERED (CountryID)
)
CREATE TABLE dbo.Customers
(
CustomerID int NOT NULL,
CustomerName char(30) NOT NULL,
Country char(20) NULL,
CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (CustomerID)
)
你要尽可能快地将Customer表里头Country的信息转移到新Country表,该选用下面的哪条语句?
单项选择题
INSERT INTO Country (CountryName)
SELECT DISTINCT Country
FROM Customers
SELECT (*) AS ColID, c1.Country
INTO Country
FROM (SELECT DISTINCT Country FROM Customers)AS c1,
(SELECT DISTINCT Country FROM Customers) AS c2,
WHERE c1.Country >=c2.Country
GROUP BY c1.Country ORDER BY 1
DECLARE @Country char (20)
DECLARE cursor_country CURSOR
FOR SELECT Country FROM Customers
OPEN cursor_country
FETCH NEXT FROM cursor_country INTO @Country
WHILE (@@FETCH_STATUS <> -1)
BEGIN
If NOT EXISTS (SELECT CountryID
FROM Country
WHERE CountryName = @Country)
INSERT INTO Country (CountryName) VALUES (@Country)
FETCH NEXT FROM cursor_country INTO @Country
END
CLOSE cursor_country
DEALLOCATE cursor_country
DECLARE @SQL varchar (225)
SELECT @SQL = ‘bcp “SELECT ColID = COUNT(*), c1. Country’ +
‘FROM (SELECT DISTINCT Country FROM Sales..Customers) AS
c1, ' +
(SELECT DISTINCT Country FROM Sales..Customers) AS c2 '
+
WHERE c1.Country >= c2.Country’ +
‘GROUP BY c1.Country ORDER BY 1’ +
‘query out c:\country.txt -c’
EXEC master..xp_cmdshell @SQL, no_output
EXEC master..xp_cmdshell ‘bcp Sales..Country in c:\country. Txt-c’, no_output