IT

SQL Server 2008의 쿼리 선택 결과에서 테이블을 만드는 방법

lottoking 2020. 5. 21. 08:23
반응형

SQL Server 2008의 쿼리 선택 결과에서 테이블을 만드는 방법 [중복]


SQL Server의 쿼리 선택 결과에서 테이블을 만들고 싶습니다.

create table temp AS select.....

하지만 오류가 발생했습니다

키워드 'AS'근처에 잘못된 구문


다음 구문을 사용하여 SQL Server 2008의 이전 테이블에서 새 테이블을 만듭니다.

Select * into new_table  from  old_table 

사용하다 SELECT...INTO

SELECT INTO 문은 새 테이블을 작성하고 SELECT 문의 결과 세트로 채 웁니다. SELECT INTO를 사용하면 여러 테이블 또는 뷰의 데이터를 하나의 테이블로 결합 할 수 있습니다. 또한 연결된 서버에서 선택한 데이터가 포함 된 새 테이블을 만드는 데 사용할 수도 있습니다.

예,

SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM   tablename

표준 구문,

SELECT  col1, ....., col@      -- <<== select as many columns as you want
        INTO [New tableName]
FROM    [Source Table Name]

MSSQL을 조심하십시오 : "SELECT * INTO NewTable FROM OldTable"

항상 MYSQL과 동일하지는 않습니다. "create table temp AS select.."

MSSQL에서 이것이 새 테이블의 모든 필드가 이전과 동일한 유형임을 보장하지 않는 경우가 있다고 생각합니다.

예를 들면 다음과 같습니다.

create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable

항상 항복하지는 않습니다 :

create table newTable (field1 varchar(10), field2 integer, field3 float)

그러나 아마도:

create table newTable (field1 varchar(10), field2 integer, field3 integer)

SELECT INTO ...를 사용해보십시오.

SELECT ....
INTO     TABLE_NAME(table you want to create)
FROM source_table

시도하십시오 :

SELECT * INTO NewTable FROM OldTable

Select [Field Names] into [New Table] from [Source Table]

참고 URL : https://stackoverflow.com/questions/16683758/how-to-create-a-table-from-select-query-result-in-sql-server-2008

반응형