Can I specify an input sql file with bcp?

Legend picture Legend · May 13, 2012 · Viewed 24.1k times · Source

How can I specify an input sql file with a long query when using bcp? I tried using the -i option but it keeps complaining about a command-line error with no extra information. Is this possible?

Answer

LarryDavid picture LarryDavid · May 16, 2013

I had this problem today and found a convenient workaround, at least in an ad-hoc situation.

Temporary tables can be created by any user with connect permissions. This means you can also create GLOBAL temporary tables.

Just run your query in enterprise manager (or sql cmd or whatever) using SELECT ...INTO with a global temporary table e.g.

SELECT * 
INTO ##mytemptable
FROM SomeTable 
WHERE [massive where clause, for example] 

You can then use the temporary table in the BCP query with a simple

SELECT * FROM ##mytemptable

Then drop the temp table through enterprise manager

DROP TABLE ##mytemptable