programing

SQL Server 2008에서 선택한 쿼리 결과에서 테이블을 생성하는 방법

stoneblock 2023. 4. 9. 21:02

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]

주의하세요, MSQL:"SELECT * INTO NewTable FROM OldTable"

MYSQL과 항상 같은 것은 아닙니다."create table temp AS select.."

(MSQL에서) 이 방법이 새 테이블의 모든 필드가 이전 것과 동일한 유형임을 보증하지 못할 수 있다고 생각합니다.

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

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 NewTable FROM OldTable

SELECT IN...을 사용해 보세요.

SELECT ....
INTO     TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] 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