Problems With Bcp Output
Solution 1:
This could be weird but it will work..
Put the entire sql in single line instead of new lines
SET @SQLEXE = 'bcp "SELECT [Entry No_],[Date and Time],[User ID],[Table No_],[Field No_],[Type of Change],[Old Value],[New Value],[Primary Key],[Primary Key Field 1 No_],[Primary Key Field 1 Value],[Primary Key Field 2 Value],[Primary Key Field 3 No_],[Primary Key Field 3 Value],[Record ID] FROM [dbo].[' + @SearchSchema + '] WHERE [Date and Time] BETWEEN '+@period+'" out "C:\Users\Public\Documents\1a_EY_change_log_entry.txt" -o "C:\Users\Public\Documents\1b_EYlog_change_log_entry.log" -d '+quotename(@dname)+' -c -T';
Also this will not work as you are expecting
EXECUTE ('USE [' + @DBName+']');
Use the database parameter(-d) option present in bcp
-d databasenameSolution 2:
Before trying to exec, you should print the command you get: print @SQLEXE;
You have at least 1 logical error: when you execute EXECUTE ('USE [' + @DBName+']'); it changes db context only for a duration of this your dynamic code,
so if you try this: EXECUTE ('USE [' + @DBName+']'); select db_name()
you'll see that you are still in your db, not in @DBName context
Then your @SearchSchema probably remains NULL and this means @SQLEXE also becomes null.
But if your output is basic bcp usage syntax probably your command is not null but contains any other error that only you can see doing print @SQLEXE;.
For example it may be that you exec your code in the context of user default database (that is probably master) because you don't put your dbname in your code: select..from [dbo].[' + @SearchSchema + '] but if you don't precise the database it will be user's default database.
So post here the output of PRINT so we can help you more precisely
Post a Comment for "Problems With Bcp Output"