상세 컨텐츠

본문 제목

[MSSQL] Json 형식으로 데이터 출력 및 파일생성

SQL Server

by 코딩하는 박줄기 2023. 1. 9. 22:16

본문

728x90
반응형

SELECT 문에 FOR JSON 절을 추가하여 SQL Server 데이터 또는 SQL 쿼리 결과를 JSON으로 서식 지정합니다. 

 

사용

   1. JSON 형식으로 데이터 조회

SELECT [ProductID], [Name], [ProductNumber], [Color], [StandardCost]
FROM [AdventureWorksLT2019].[SalesLT].[Product]
FOR JSON PATH;

   2. 파일 생성

* 파일 생성 위치에 접근 권한이 있어야 합니다.

EXEC sp_configure 'show advanced options', 1; 
GO
EXEC Sp_configure 'xp_cmdshell', 1;
Go
Reconfigure;
Go

Exec master..xp_cmdshell 'bcp "SELECT [ProductID], [Name], [ProductNumber], [Color], [StandardCost] FROM [AdventureWorksLT2019].[SalesLT].[Product] FOR JSON PATH" queryout "C:\TEST\test.txt" -S8.8.8.8[서버이름] -Ujinwoo[로그인] -P1234[암호] -c -t'

 

참조

 

728x90
반응형

관련글 더보기

댓글 영역