0
T-SQL 스크립트에서 Excel 출력을 동적으로 만드는 방법에 대한 모든 온라인 단일 페이지를 읽은 것 같습니다. 내가 할 수없는 내 인생의 그림을 위해INSERT INTO OPENROWSET 동적 T-SQL을 사용하는 구문
SET @sql = 'INSERT INTO OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',Excel 12.0;Database='[email protected]+';'',''SELECT * FROM [Sheet1$])'' SELECT * FROM dbo.CMLTrial WHERE Officer='''[email protected]+''
:
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO
USE CommercialLending
GO
DECLARE @LoopCounter TINYINT = 1
DECLARE @LoopMaxCount TINYINT = (SELECT COUNT(DISTINCT OFFICER)
FROM CommercialLending.dbo.CMLTrial)
WHILE (1=1)
BEGIN
DECLARE @OfficerName VARCHAR(4000) = (
SELECT OFFICER
FROM (SELECT DISTINCT OFFICER, ROW_NUMBER() OVER (ORDER BY OFFICER) AS rownumber
FROM CommercialLending.dbo.CMLTrial GROUP BY Officer) AS OFFICER
WHERE rownumber = @LoopCounter)
DECLARE @FileName varchar(400) = @OfficerName+ '.xlsx'
DECLARE @FullFileName varchar(400) = 'O:\MIS\Python\Programs\CommercialLending\'[email protected]
DECLARE @CopyFile varchar(800) = 'copy O:\MIS\Python\Programs\CommercialLending\Template.xlsx copy O:\MIS\Python\Programs\CommercialLending\' + @FileName
EXEC xp_cmdshell @CopyFile
DECLARE @sql nvarchar(4000)
SET @sql = 'INSERT INTO OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',Excel 12.0;Database='[email protected]+';'',''SELECT * FROM [Sheet1$])'' SELECT * FROM dbo.CMLTrial WHERE Officer='''[email protected]+''
EXEC (@sql)
SET @LoopCounter = @LoopCounter+1
IF (@LoopCounter > @LoopMaxCount)
BREAK;
END
Use Master
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'ad hoc distributed queries', 0
RECONFIGURE
GO
EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 0
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 0
GO
나는 내 문제는 @Sql 변수에 OPENROWSET 문자열에 INSERT를 할당 라인에 있음을 알고 여기에 내가 무엇을 가지고 내 구문 오류가 어디 있습니다. 누군가가 알아낼 수있게 도와 줄 수 있습니까? 가능하다면 INSERT INTO OPENROWSET을 변수의 문자열로 사용하기위한 표준 구문을 이해하기위한 지침을 제공 할 수 있습니까?
'@ FullFileName'과 @ OfficerName'의 정확한 값을 입력하십시오. 잠재적으로'''기호 나'@ sql'을 올바른 문자열로 부수는 다른 기호를 가질 수 있습니다. – Serg