Insert image to MSSQL using textcopy

Ok, I know this question has been asked dozen of time. I did do some research but I encountered some difficulties. Please help me and I promise all the reward points that I have.

Below is my procedure :
1) I created a stored procedure with the following source from

CREATE PROCEDURE sp_textcopy (
  @srvname     varchar (30),
  @login       varchar (30),
  @password    varchar (30),
  @dbname      varchar (30),
  @tbname      varchar (30),
  @colname     varchar (30),
  @filename    varchar (30),
  @whereclause varchar (40),
  @direction   char(1))
DECLARE @exec_str varchar (255)
SELECT @exec_str =
         'textcopy /S ' + @srvname +
         ' /U ' + @login +
         ' /P ' + @password +
         ' /D ' + @dbname +
         ' /T ' + @tbname +
         ' /C ' + @colname +
         ' /W "' + @whereclause +
         '" /F ' + @filename +
         ' /' + @direction
EXEC master..xp_cmdshell @exec_str

2) Then I go to SQL Query Analyser and select the database where my tables and the stored procedure located.

3) My database is called "paramount", table is 'movie' and has a field called "image" and its type is "image".

4) I issued this command :
sp_textcopy @srvname = 'local',
            @login = '', @password = '',
            @dbname = 'paramountSilver', @tbname = 'movie',
            @colname = 'image',
            @filename = 'd:\paramount\antz.jpg',
            @whereclause = "title = 'antz'",
            @direction = 'I'

5) I got this message from the output panel
'textcopy' is not recognized as an internal or external command,operable program or batch file.

Please tell me what is wrong and how to rectify the error.

Thank you very much.
Who is Participating?
dhensonConnect With a Mentor Commented:

It's possible that the properties on your database isn't allowing sql server registration using a method other than 'Windows Authentication'.

see the above links... let me know if that was it.
The path that the textcopy.exe executable is in must not be in the %Path system variable.  Either preface the textcopy statement with the location of the file on your server i.e.

'c:\mssql7\binn\textcopy /S ' + @srvname +


Change your system path such that the executable would be found.

i_am_diablo2000Author Commented:
Hi, Thanks for the reply.

I guess that is the answer. However, I need to hardcode the full path in the stored procedure.

And my path is "C:\Program Files\Microsoft SQL Server\MSSQL\Binn"

As you see, there is a space in between the path and I encoutered error again while executing the statement.

'C:\Program' is not recognized as an internal or external command,

 How should I escape the white space in the path?

Come on, I want to give you the 200 points.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

i_am_diablo2000Author Commented:
Hello dhenson,

I have another problem. I fixed the above problem by copying the "textcopy.exe" file to a directory without space and it works.

But I encountered the following error when executing.

SQL Server Message 18452: Login failed for user 'null'. Reason: Not associated with a trusted SQL Server connection.

In fact, my database does not set any userid or password so I am not sure what data should I provide in this case.

Could you please enlighten me? Thanks
c:\progra~1\micros~1\mssql\binn (assuming there are no other folders that start with "microsoft" in your program files folder.

a better way would be:

'c:"\program files\microsoft sql server\mssql\binn\textcopy.exe" /S ' + @srvname +

Looking up the password syntax...

When you say:
"In fact, my database does not set any userid or password so I am not sure what data should I provide in this case."

You must be authenticating using Windows Authenitication.  Your passing your windows credentials to Query analyser when you connect.

Create a SQL user account and assign it to the paramountSilver Database.  Then Right click your Movie table and grant the new account 'Insert'.

i_am_diablo2000Author Commented:

Thanks for your answer. I will find it out later. Stay tuned for the points.
i_am_diablo2000Author Commented:

You are right. I am using Windows Authenitication.

So I have created a new login user from Enterprise Manager->Security->Logins->New Logins.

I also set the database to my paramount.

But yet, when I the script from query analyser, I still got the same error message :

SQL Server Message 18452: Login failed for user 'test'. Reason: Not associated with a trusted SQL Server connection.

What have I overlooked?
Please help.
Many thanks

i_am_diablo2000Author Commented:
sp_textcopy @srvname = 'DIABLO',  @login = 'sa', @password = 'sa',    @dbname = 'paramountSilver', @tbname = 'movie',
            @colname = 'image',
            @filename = 'd:\paramount\antz.jpg',
            @whereclause = "title = 'antz'",
            @direction = 'I'

dhenson, thank you for your answer. I tried your second method and it seems login problem is solved. But I got another error.

SQL Server 'DIABLO' Message 170: Line 1: Incorrect syntax near '='. (Concerning line 1)
DB-Library Error 10007: General SQL Server error: Check messages from the SQL Server.

I am desperate. Do you have any idea?

i_am_diablo2000Author Commented:

Your solution works. Thanks. Now I give you the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.