Reputation: 315
I have a script task in SSIS that executes Dynamic SQL and builds a BCP statement that is executed in the typical manor. But when editing in SSMS as a SQL script, I can execute it within SSMS and it will generate normal output in the Results pane but no files appear. The files should be generated in a C:\Temp\ folder.
If I print out the dynamic sql statements, paste a single one into a cmd shell (removing tic marks) and execute, I get the file as expected.
I can't seem to find the correct keywords to locate any similar issues on S.O. so here I am asking the Wisdom of the Crowds.
Here is an example where I loop through all tables within a schema to generate FMT files:
DECLARE @schema INT ---- SELECT * FROM sys.schemas
SET @schema=5
DECLARE @tablename1 VARCHAR(256)
DECLARE @FQName1 Varchar(128)
DECLARE @cmd1 VARCHAR(max)
DECLARE db_cursor CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR
SELECT db_name() + '.' + b.name + '.' + a.name as FQName1, a.name AS TableName1
FROM sys.objects a inner join sys.schemas b on a.schema_id = b.schema_id
WHERE a.type_desc = 'USER_TABLE'
AND a.schema_id=@schema
AND a.name IN ('Bankruptcy', 'Loan_Own', 'Application', 'Loan_Mod','Payment','Loan')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @FQName1, @tablename1
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmd1 = 'xp_cmdshell ''BCP ' + @FQName1 + ' format nul -f "C:\tmp\' + 'FMT.' + @tablename1 + '" -c -r0x1E -t0x1F -T'''
--PRINT @cmd1
EXEC (@cmd1)
FETCH NEXT FROM db_cursor INTO @FQName1, @tablename1
END
CLOSE db_cursor
DEALLOCATE db_cursor
The output looks like this:
It should be noted that I can execute this same code on my ETL Server without issue. All files are generated as expected. I looked at the xp_cmdshell configuration and it is set up correctly. I am running SSMS with my admin account (same results running it as non-admin).
Please correct my error and teach me the way of the SQL Master.
Upvotes: 0
Views: 20