Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add filestream to an existing table column

I have the following table column:

[Content] [varbinary](max) NULL

And I want to make it a filestream column so I tried:

alter table dbo.Files
  alter column Content add filestream

But I get the error:

Incorrect syntax near 'filestream'.  

I also tried

alter table dbo.Files
  alter column Content varbinary(max) filestream not null

But I got the error:

Cannot alter column 'Content' in table 'Files' to add or remove the FILESTREAM column attribute.

How can I add filestream to an existing column?

like image 541
Miguel Moura Avatar asked Mar 23 '16 16:03

Miguel Moura


People also ask

How do I add Filestream to an existing database?

Enabling FILESTREAMRight-click the instance, and then click Properties. In the SQL Server Properties dialog box, click the FILESTREAM tab. Select the Enable FILESTREAM for Transact-SQL access check box. If you want to read and write FILESTREAM data from Windows, click Enable FILESTREAM for file I/O streaming access.

What is the difference between Filestream and FileTable?

FileStream and FileTable are features of SQL Server for storing unstructured data in SQL Server alongside other data. The FileStream feature stores unstructured data in the file system and keeps a pointer of the data in the database, whereas FileTable extends this feature even further allowing non-transactional access.

How do I use SQL Server Filestream?

You must enable FILESTREAM by using SQL Server Configuration Manager and SQL Server Management Studio. To use FILESTREAM, you must create or modify a database to contain a special type of filegroup. Then, create or modify a table so that it contains a varbinary(max) column with the FILESTREAM attribute.


1 Answers

you'll need to do the following (sourced from here):

/* rename the varbinary(max) column
eg. FileData to xxFileData */
sp_RENAME '<TableName>.<ColumnName>', 'xx<ColumnName>' , 'COLUMN'
GO

/* create a new varbinary(max) FILESTREAM column */
ALTER TABLE <TableName>
ADD <ColumnName> varbinary(max) FILESTREAM NULL
GO

/* move the contents of varbinary(max) column to varbinary(max) FILESTREAM column */
UPDATE <TableName>
SET <ColumnName> = xx<ColumnName>
GO

/* drop the xx<ColumnName> column */
ALTER TABLE <TableName>
DROP COLUMN xx<ColumnName>
GO
like image 131
Fuzzy Avatar answered Oct 09 '22 02:10

Fuzzy